diff --git a/src/api_data/data_conservation_settings_api.rs b/src/api_data/data_conservation_settings_api.rs new file mode 100644 index 0000000..3f81b33 --- /dev/null +++ b/src/api_data/data_conservation_settings_api.rs @@ -0,0 +1,29 @@ +//! # Data conservation settings API object +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::user::User; + +#[derive(Serialize)] +pub struct DataConservationSettingsAPI { + inactive_account_lifetime: Option, + notification_lifetime: Option, + comments_lifetime: Option, + posts_lifetime: Option, + conversation_messages_lifetime: Option, + likes_lifetime: Option, +} + +impl DataConservationSettingsAPI { + pub fn new(user: &User) -> Self { + Self { + inactive_account_lifetime: user.delete_account_after.clone(), + notification_lifetime: user.delete_notifications_after, + comments_lifetime: user.delete_comments_after, + posts_lifetime: user.delete_posts_after, + conversation_messages_lifetime: user.delete_conversation_messages_after, + likes_lifetime: user.delete_likes_after, + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 10e5c8e..8b7b5c4 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -55,6 +55,7 @@ pub mod general_settings_api; pub mod language_settings_api; pub mod security_settings_api; pub mod account_image_settings_api; +pub mod data_conservation_settings_api; pub mod res_create_custom_emoji; pub mod res_get_ws_token; pub mod user_calls_config; diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index b2bb101..d9b3d31 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -3,12 +3,12 @@ //! @author Pierre Hubert use crate::api_data::account_image_settings_api::AccountImageSettingsAPI; +use crate::api_data::data_conservation_settings_api::DataConservationSettingsAPI; use crate::api_data::general_settings_api::GeneralSettingsAPI; use crate::api_data::language_settings_api::LanguageSettingsAPI; use crate::api_data::res_create_custom_emoji::ResCreateCustomEmoji; use crate::api_data::security_settings_api::SecuritySettingsAPI; use crate::constants::SUPPORTED_LANGUAGES; -use crate::routes::RequestResult; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::general_settings::GeneralSettings; use crate::data::http_request_handler::HttpRequestHandler; @@ -18,6 +18,7 @@ use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; use crate::data::user::{AccountImageVisibility, UserPageStatus}; use crate::helpers::{account_helper, custom_emojies_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; +use crate::routes::RequestResult; /// Get the general settings of the user pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult { @@ -202,4 +203,11 @@ pub fn delete_custom_emoji(r: &mut HttpRequestHandler) -> RequestResult { custom_emojies_helper::delete(&emoji)?; r.success("Emoji deleted.") +} + +/// Get data conservation policy +pub fn get_data_conservation_policy(r: &mut HttpRequestHandler) -> RequestResult { + let user = user_helper::find_user_by_id(r.user_id_ref()?)?; + + r.set_response(DataConservationSettingsAPI::new(&user)) } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 96e0c79..fd15af5 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -123,12 +123,12 @@ pub struct User { pub security_answer_2: Option, /// Automatically delete the account after a period of inactivity. 0 = feature disabled - pub delete_account_after: u64, - pub delete_notifications_after: u64, - pub delete_comments_after: u64, - pub delete_posts_after: u64, - pub delete_conversation_messages_after: u64, - pub delete_likes_after: u64, + pub delete_account_after: Option, + pub delete_notifications_after: Option, + pub delete_comments_after: Option, + pub delete_posts_after: Option, + pub delete_conversation_messages_after: Option, + pub delete_likes_after: Option, } impl User { diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 00614b0..a61cf55 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -358,11 +358,13 @@ pub fn delete(user_id: &UserID) -> ResultBoxError { /// Automatically delete the account, if it have been inactive for a too long time pub fn remove_if_inactive_for_too_long_time(user: &User) -> Res { - if user.delete_account_after < 1 { + let timeout = user.delete_account_after.unwrap_or(0); + + if timeout < 1 { return Ok(()); } - if user.last_activity < time() - user.delete_account_after { + if user.last_activity < time() - timeout { delete(&user.id)?; } diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index 1bc73c0..6eb336b 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -123,14 +123,15 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { /// Clean old user comments pub fn clean_old_comments(user: &User) -> Res { - if user.delete_comments_after < 1 { + let lifetime = user.delete_comments_after.unwrap_or(0); + if lifetime < 1 { return Ok(()); } let comments = database::QueryInfo::new(COMMENTS_TABLE) .cond_user_id("ID_personne", &user.id) .set_custom_where("time_insert < ?") - .add_custom_where_argument_u64(time() - user.delete_comments_after) + .add_custom_where_argument_u64(time() - lifetime) .exec(db_to_comment)?; for comment in comments { diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 03515d6..145d928 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -263,14 +263,15 @@ pub fn export_all_user_messages(user_id: &UserID) -> ResultBoxError Res { - if user.delete_conversation_messages_after < 1 { + let lifetime = user.delete_conversation_messages_after.unwrap_or(0); + if lifetime < 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) + .add_custom_where_argument_u64(time() - lifetime) .exec(db_to_conversation_message)?; for message in messages { diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 460bce5..ba29ad8 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -319,6 +319,14 @@ impl<'a> RowResult<'a> { } } + /// Get an optional unsigned number => Set to None if value is null / empty / 0 + pub fn get_optional_positive_u64(&self, name: &str) -> ResultBoxError> { + Ok(match self.get_optional_u64(name)? { + None | Some(0) => None, + Some(val) => Some(val) + }) + } + pub fn get_u32(&self, name: &str) -> ResultBoxError { let value = self.row.get_opt(self.find_col(name)?); diff --git a/src/helpers/likes_helper.rs b/src/helpers/likes_helper.rs index 6b7ebc6..f345e17 100644 --- a/src/helpers/likes_helper.rs +++ b/src/helpers/likes_helper.rs @@ -98,15 +98,16 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { /// Delete old user likes pub fn clean_old_user_likes(user: &User) -> Res { - if user.delete_likes_after < 1 { - return Ok(()); + if let Some(lifetime) = user.delete_likes_after + { + database::DeleteQuery::new(LIKES_TABLE) + .cond_user_id("ID_personne", &user.id) + .set_custom_where("Date_envoi < ?") + .add_custom_where_arg_str(&time_to_mysql_date(time() - lifetime)) + .exec()?; } - database::DeleteQuery::new(LIKES_TABLE) - .cond_user_id("ID_personne", &user.id) - .set_custom_where("Date_envoi < ?") - .add_custom_where_arg_str(&time_to_mysql_date(time() - user.delete_likes_after)) - .exec() + Ok(()) } /// Turn a database entry into a like entry diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index 712ce7a..b6f21b4 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -275,14 +275,15 @@ pub fn delete_all_related_with_user(user_id: &UserID) -> ResultBoxError { /// Delete all the old notifications of a user pub fn clean_old_user_notifications(user: &User) -> Res { - if user.delete_notifications_after < 1 { + let lifetime = user.delete_notifications_after.unwrap_or(0); + if lifetime < 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) + .add_custom_where_arg_u64(time() - lifetime) .exec() } diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 1e47a77..4c14af5 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -471,7 +471,8 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { /// Clean the old posts of a user pub fn clean_old_posts(user: &User) -> Res { - if user.delete_posts_after < 1 { + let lifetime = user.delete_posts_after.unwrap_or(0); + if lifetime < 1 { return Ok(()); } @@ -479,7 +480,7 @@ pub fn clean_old_posts(user: &User) -> Res { .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) + .add_custom_where_argument_u64(time() - lifetime) .exec(db_to_post)?; for post in posts { diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index cbb3d3b..af1875c 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -79,12 +79,12 @@ fn db_to_user(res: &database::RowResult) -> ResultBoxError { security_answer_1: res.get_optional_str("reponse1")?, security_question_2: res.get_optional_str("question2")?, security_answer_2: res.get_optional_str("reponse2")?, - delete_account_after: res.get_u64("delete_account_after")?, - delete_notifications_after: res.get_u64("delete_notifications_after")?, - delete_comments_after: res.get_u64("delete_comments_after")?, - delete_posts_after: res.get_u64("delete_posts_after")?, - delete_conversation_messages_after: res.get_u64("delete_conversation_messages_after")?, - delete_likes_after: res.get_u64("delete_likes_after")?, + delete_account_after: res.get_optional_positive_u64("delete_account_after")?, + delete_notifications_after: res.get_optional_positive_u64("delete_notifications_after")?, + delete_comments_after: res.get_optional_positive_u64("delete_comments_after")?, + delete_posts_after: res.get_optional_positive_u64("delete_posts_after")?, + delete_conversation_messages_after: res.get_optional_positive_u64("delete_conversation_messages_after")?, + delete_likes_after: res.get_optional_positive_u64("delete_likes_after")?, }) } diff --git a/src/routes.rs b/src/routes.rs index dd7bb83..24b3919 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -174,6 +174,7 @@ pub fn get_routes() -> Vec { Route::post("/settings/set_account_image_visibility", Box::new(settings_controller::set_account_image_visibility)), Route::post("/settings/upload_custom_emoji", Box::new(settings_controller::upload_custom_emoji)), Route::post("/settings/delete_custom_emoji", Box::new(settings_controller::delete_custom_emoji)), + Route::post("/settings/get_data_conservation_policy", Box::new(settings_controller::get_data_conservation_policy)), // Friends controller