From 7876c37330976858c7ff94ca04e1a788c8404a3d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 14 Feb 2021 16:56:48 +0100 Subject: [PATCH] Automatically clean old notifications --- src/cleanup_thread.rs | 5 +++-- src/helpers/notifications_helper.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/cleanup_thread.rs b/src/cleanup_thread.rs index e8d3b2e..3db903a 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, user_helper, likes_helper}; +use crate::helpers::{account_helper, likes_helper, notifications_helper, user_helper}; /// Start the maintenance thread pub fn start() -> Res { @@ -44,7 +44,8 @@ fn do_clean() -> Res { // Clean old likes likes_helper::clean_old_user_likes(&user)?; - + // Clean old notifications + notifications_helper::clean_old_user_notifications(&user)?; } diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index ff07419..712ce7a 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -5,15 +5,16 @@ use std::collections::HashMap; use crate::constants::database_tables_names::NOTIFICATIONS_TABLE; -use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::group_id::GroupID; use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification}; use crate::data::post::{PostID, PostPageKind, PostVisibilityLevel}; -use crate::data::user::UserID; +use crate::data::user::{User, UserID}; use crate::helpers::{database, events_helper, groups_helper, posts_helper}; use crate::helpers::events_helper::Event; use crate::helpers::friends_helper::GetFriendsQuery; use crate::utils::date_utils; +use crate::utils::date_utils::time; /// Create post notification pub fn create_post_notification(from_user: &UserID, post_id: u64, action: NotifEventType) -> ResultBoxError { @@ -272,6 +273,19 @@ pub fn delete_all_related_with_user(user_id: &UserID) -> ResultBoxError { Ok(()) } +/// Delete all the old notifications of a user +pub fn clean_old_user_notifications(user: &User) -> Res { + if user.delete_notifications_after < 1 { + return Ok(()); + } + + database::DeleteQuery::new(NOTIFICATIONS_TABLE) + .cond_user_id("dest_user_id", &user.id) + .set_custom_where("time_create < ?") + .add_custom_where_arg_u64(time() - user.delete_notifications_after) + .exec() +} + /// Delete all the notifications related with a group pub fn delete_all_related_with_group(group_id: &GroupID) -> ResultBoxError { delete(&PartialNotification::new()