From ee69145228ba2e53b7d549a2a4cc86dcd01c55c3 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 14 Feb 2021 19:02:38 +0100 Subject: [PATCH] Automatially delete old posts --- src/cleanup_thread.rs | 5 ++++- src/helpers/posts_helper.rs | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/cleanup_thread.rs b/src/cleanup_thread.rs index db23f57..b50fc1c 100644 --- a/src/cleanup_thread.rs +++ b/src/cleanup_thread.rs @@ -6,7 +6,7 @@ use crate::constants::{CLEAN_UP_INTERVAL, INITIAL_REFRESH_LOAD_INTERVAL}; use crate::data::error::Res; -use crate::helpers::{account_helper, comments_helper, likes_helper, notifications_helper, user_helper}; +use crate::helpers::{account_helper, comments_helper, likes_helper, notifications_helper, posts_helper, user_helper}; /// Start the maintenance thread pub fn start() -> Res { @@ -52,6 +52,9 @@ fn do_clean() -> Res { // Clean old comments comments_helper::clean_old_comments(&user)?; + + // Clean old posts + posts_helper::clean_old_posts(&user)?; } diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 2b3ccd2..1e47a77 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -5,12 +5,12 @@ use crate::constants::database_tables_names::POSTS_TABLE; -use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::group_id::GroupID; use crate::data::group_member::GroupMembershipLevel; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE}; -use crate::data::user::UserID; +use crate::data::user::{User, UserID}; use crate::helpers::{comments_helper, database, friends_helper, groups_helper, likes_helper, notifications_helper, survey_helper, user_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::{mysql_date, time}; @@ -469,6 +469,26 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { Ok(()) } +/// Clean the old posts of a user +pub fn clean_old_posts(user: &User) -> Res { + if user.delete_posts_after < 1 { + return Ok(()); + } + + let posts = database::QueryInfo::new(POSTS_TABLE) + .set_custom_where("(ID_personne = ? OR ID_amis = ?) AND time_insert < ?") + .add_custom_where_argument_user_id(&user.id) + .add_custom_where_argument_user_id(&user.id) + .add_custom_where_argument_u64(time() - user.delete_posts_after) + .exec(db_to_post)?; + + for post in posts { + delete(&post)?; + } + + Ok(()) +} + /// Turn a post into a database entry fn db_to_post(res: &database::RowResult) -> ResultBoxError { let user_id = if res.get_u64("ID_amis")? == 0 {