1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-25 14:59:21 +00:00

Automatially delete old posts

This commit is contained in:
Pierre HUBERT 2021-02-14 19:02:38 +01:00
parent 2dbf7ee850
commit ee69145228
2 changed files with 26 additions and 3 deletions

View File

@ -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)?;
}

View File

@ -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<Post> {
let user_id = if res.get_u64("ID_amis")? == 0 {