From f44e5103cad9a28abe7ed136ea1fc44ab918920a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 14 Feb 2021 19:15:04 +0100 Subject: [PATCH] Automatically delete old conversation messages --- src/cleanup_thread.rs | 5 ++++- src/helpers/conversations_helper.rs | 23 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/cleanup_thread.rs b/src/cleanup_thread.rs index b50fc1c..d311666 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, posts_helper, user_helper}; +use crate::helpers::{account_helper, comments_helper, conversations_helper, likes_helper, notifications_helper, posts_helper, user_helper}; /// Start the maintenance thread pub fn start() -> Res { @@ -55,6 +55,9 @@ fn do_clean() -> Res { // Clean old posts posts_helper::clean_old_posts(&user)?; + + // Clean old conversation messages + conversations_helper::clean_old_messages(&user)?; } diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 9351868..03515d6 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -5,11 +5,11 @@ use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE}; use crate::data::conversation::Conversation; use crate::data::conversation_message::ConversationMessage; -use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; use crate::data::unread_conversation::UnreadConversation; -use crate::data::user::UserID; +use crate::data::user::{User, UserID}; use crate::helpers::{database, events_helper}; use crate::helpers::database::InsertQuery; use crate::helpers::events_helper::Event; @@ -261,6 +261,25 @@ pub fn export_all_user_messages(user_id: &UserID) -> ResultBoxError Res { + if user.delete_conversation_messages_after < 1 { + return Ok(()); + } + + let messages = database::QueryInfo::new(CONV_MESSAGES_TABLE) + .cond_user_id("user_id", &user.id) + .set_custom_where("time_insert < ?") + .add_custom_where_argument_u64(time() - user.delete_conversation_messages_after) + .exec(db_to_conversation_message)?; + + for message in messages { + delete_message(&message)?; + } + + Ok(()) +} + /// Delete all the messages of a given user pub fn delete_all_user_messages(user_id: &UserID) -> ResultBoxError { for msg in &export_all_user_messages(user_id)? {