1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Can get data conservation policy

This commit is contained in:
Pierre HUBERT 2021-02-15 17:38:25 +01:00
parent 376bd0477e
commit 3e73a0376b
13 changed files with 84 additions and 30 deletions

View File

@ -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<u64>,
notification_lifetime: Option<u64>,
comments_lifetime: Option<u64>,
posts_lifetime: Option<u64>,
conversation_messages_lifetime: Option<u64>,
likes_lifetime: Option<u64>,
}
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,
}
}
}

View File

@ -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;

View File

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

View File

@ -123,12 +123,12 @@ pub struct User {
pub security_answer_2: Option<String>,
/// 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<u64>,
pub delete_notifications_after: Option<u64>,
pub delete_comments_after: Option<u64>,
pub delete_posts_after: Option<u64>,
pub delete_conversation_messages_after: Option<u64>,
pub delete_likes_after: Option<u64>,
}
impl User {

View File

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

View File

@ -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 {

View File

@ -263,14 +263,15 @@ pub fn export_all_user_messages(user_id: &UserID) -> ResultBoxError<Vec<Conversa
/// Clean old user conversation messages
pub fn clean_old_messages(user: &User) -> 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 {

View File

@ -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<Option<u64>> {
Ok(match self.get_optional_u64(name)? {
None | Some(0) => None,
Some(val) => Some(val)
})
}
pub fn get_u32(&self, name: &str) -> ResultBoxError<u32> {
let value = self.row.get_opt(self.find_col(name)?);

View File

@ -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

View File

@ -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()
}

View File

@ -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 {

View File

@ -79,12 +79,12 @@ fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
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")?,
})
}

View File

@ -174,6 +174,7 @@ pub fn get_routes() -> Vec<Route> {
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