1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Automatically delete old conversation messages

This commit is contained in:
Pierre HUBERT 2021-02-14 19:15:04 +01:00
parent ee69145228
commit f44e5103ca
2 changed files with 25 additions and 3 deletions

View File

@ -6,7 +6,7 @@
use crate::constants::{CLEAN_UP_INTERVAL, INITIAL_REFRESH_LOAD_INTERVAL}; use crate::constants::{CLEAN_UP_INTERVAL, INITIAL_REFRESH_LOAD_INTERVAL};
use crate::data::error::Res; 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 /// Start the maintenance thread
pub fn start() -> Res { pub fn start() -> Res {
@ -55,6 +55,9 @@ fn do_clean() -> Res {
// Clean old posts // Clean old posts
posts_helper::clean_old_posts(&user)?; posts_helper::clean_old_posts(&user)?;
// Clean old conversation messages
conversations_helper::clean_old_messages(&user)?;
} }

View File

@ -5,11 +5,11 @@
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE}; use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MESSAGES_TABLE, CONV_USERS_TABLE};
use crate::data::conversation::Conversation; use crate::data::conversation::Conversation;
use crate::data::conversation_message::ConversationMessage; 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::NewConversation;
use crate::data::new_conversation_message::NewConversationMessage; use crate::data::new_conversation_message::NewConversationMessage;
use crate::data::unread_conversation::UnreadConversation; 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, events_helper};
use crate::helpers::database::InsertQuery; use crate::helpers::database::InsertQuery;
use crate::helpers::events_helper::Event; use crate::helpers::events_helper::Event;
@ -261,6 +261,25 @@ pub fn export_all_user_messages(user_id: &UserID) -> ResultBoxError<Vec<Conversa
.exec(db_to_conversation_message) .exec(db_to_conversation_message)
} }
/// Clean old user conversation messages
pub fn clean_old_messages(user: &User) -> 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 /// Delete all the messages of a given user
pub fn delete_all_user_messages(user_id: &UserID) -> ResultBoxError { pub fn delete_all_user_messages(user_id: &UserID) -> ResultBoxError {
for msg in &export_all_user_messages(user_id)? { for msg in &export_all_user_messages(user_id)? {