From a537dbcd4d42653ee4865ac1e30077bea701461a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 14 Feb 2021 18:49:47 +0100 Subject: [PATCH] Automatically delete old user comments --- src/cleanup_thread.rs | 5 ++++- src/helpers/comments_helper.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/cleanup_thread.rs b/src/cleanup_thread.rs index 3db903a..9d6e573 100644 --- a/src/cleanup_thread.rs +++ b/src/cleanup_thread.rs @@ -6,7 +6,7 @@ use crate::constants::CLEAN_UP_INTERVAL; use crate::data::error::Res; -use crate::helpers::{account_helper, likes_helper, notifications_helper, user_helper}; +use crate::helpers::{account_helper, comments_helper, likes_helper, notifications_helper, user_helper}; /// Start the maintenance thread pub fn start() -> Res { @@ -46,6 +46,9 @@ fn do_clean() -> Res { // Clean old notifications notifications_helper::clean_old_user_notifications(&user)?; + + // Clean old comments + comments_helper::clean_old_comments(&user)?; } diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index f6d3446..1bc73c0 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -4,13 +4,13 @@ use crate::constants::database_tables_names::COMMENTS_TABLE; use crate::data::comment::Comment; -use crate::data::error::{ExecError, ResultBoxError}; -use crate::data::user::UserID; -use crate::helpers::{database, likes_helper, events_helper}; -use crate::helpers::likes_helper::LikeType; -use crate::utils::date_utils::mysql_date; -use crate::utils::user_data_utils::user_data_path; +use crate::data::error::{ExecError, Res, ResultBoxError}; +use crate::data::user::{User, UserID}; +use crate::helpers::{database, events_helper, likes_helper}; use crate::helpers::events_helper::Event; +use crate::helpers::likes_helper::LikeType; +use crate::utils::date_utils::{mysql_date, time}; +use crate::utils::user_data_utils::user_data_path; /// Create a new comment. In case of success, this function returns the ID of the created comment pub fn create(c: &Comment) -> ResultBoxError { @@ -118,5 +118,24 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { delete(comment)?; } + Ok(()) +} + +/// Clean old user comments +pub fn clean_old_comments(user: &User) -> Res { + if user.delete_comments_after < 1 { + return Ok(()); + } + + let comments = database::QueryInfo::new(COMMENTS_TABLE) + .cond_user_id("ID_personne", &user.id) + .set_custom_where("time_insert < ?") + .add_custom_where_argument_u64(time() - user.delete_comments_after) + .exec(db_to_comment)?; + + for comment in comments { + delete(&comment)?; + } + Ok(()) } \ No newline at end of file