1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Automatically clean old likes

This commit is contained in:
Pierre HUBERT 2021-02-14 16:45:00 +01:00
parent dcceee5048
commit 664824ff48
5 changed files with 134 additions and 55 deletions

View File

@ -6,7 +6,7 @@
use crate::constants::CLEAN_UP_INTERVAL;
use crate::data::error::Res;
use crate::helpers::account_helper;
use crate::helpers::{account_helper, user_helper, likes_helper};
/// Start the maintenance thread
pub fn start() -> Res {
@ -38,5 +38,15 @@ fn do_clean() -> Res {
// Clean old login tokens
account_helper::clean_up_old_access_tokens()?;
// Automatic account cleanup
for user in user_helper::get_all_users()? {
// Clean old likes
likes_helper::clean_old_user_likes(&user)?;
}
Ok(())
}

View File

@ -120,6 +120,14 @@ pub struct User {
pub security_answer_1: Option<String>,
pub security_question_2: Option<String>,
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,
}
impl User {

View File

@ -662,6 +662,8 @@ pub fn insert(query: InsertQuery) -> ResultBoxError<Option<u64>> {
pub struct DeleteQuery {
table: String,
conditions: HashMap<String, Value>,
custom_where: Option<String>,
custom_where_args: Vec<mysql::Value>,
}
impl DeleteQuery {
@ -670,9 +672,28 @@ impl DeleteQuery {
DeleteQuery {
table: table.to_string(),
conditions: HashMap::new(),
custom_where: None,
custom_where_args: vec![],
}
}
pub fn set_custom_where(mut self, cond: &str) -> Self {
self.custom_where = Some(cond.to_string());
self
}
/// Add custom WHERE argument
pub fn add_custom_where_arg_u64(mut self, arg: u64) -> Self {
self.custom_where_args.push(mysql::Value::UInt(arg));
self
}
/// Add custom WHERE argument
pub fn add_custom_where_arg_str(mut self, arg: &str) -> Self {
self.custom_where_args.push(mysql::Value::from(arg));
self
}
/// Add batch conditions
pub fn add_conditions(mut self, conditions: HashMap<String, mysql::Value>) -> Self {
self.conditions.extend(conditions.into_iter());
@ -732,7 +753,7 @@ pub fn delete(query: DeleteQuery) -> ResultBoxError<()> {
return Err(ExecError::boxed_new("DELETE without WHERE condition blocked for security reasons!"));
}
let query_sql = format!(
let mut query_sql = format!(
"DELETE FROM {} WHERE {}",
query.table,
query.conditions.keys()
@ -741,10 +762,23 @@ pub fn delete(query: DeleteQuery) -> ResultBoxError<()> {
.join(" AND ")
);
get_connection()?.exec_drop(
query_sql,
query.conditions.values().collect::<Vec<&Value>>(),
)?;
let mut values = query.conditions.values().collect::<Vec<&Value>>();
if let Some(custom_where) = &query.custom_where {
if !query.conditions.is_empty() {
query_sql.push_str(" AND ");
}
query_sql.push_str("(");
query_sql.push_str(custom_where);
query_sql.push_str(")");
for value in &query.custom_where_args {
values.push(value);
}
}
get_connection()?.exec_drop(query_sql, values)?;
Ok(())
}

View File

@ -3,12 +3,12 @@
//! Module dedicated to likes management
use crate::constants::database_tables_names::LIKES_TABLE;
use crate::data::error::ResultBoxError;
use crate::data::user::UserID;
use crate::data::error::{Res, ResultBoxError};
use crate::data::user::{User, UserID};
use crate::data::user_like::UserLike;
use crate::helpers::database;
use crate::helpers::database::QueryInfo;
use crate::utils::date_utils::mysql_date;
use crate::utils::date_utils::{mysql_date, time, time_to_mysql_date};
#[derive(Copy, Clone)]
pub enum LikeType {
@ -96,6 +96,19 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError {
.exec()
}
/// Delete old user likes
pub fn clean_old_user_likes(user: &User) -> Res {
if user.delete_likes_after < 1 {
return Ok(());
}
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()
}
/// Turn a database entry into a like entry
fn db_to_user_like(r: &database::RowResult) -> ResultBoxError<UserLike> {
Ok(UserLike {

View File

@ -1,5 +1,5 @@
use crate::constants::database_tables_names::USERS_TABLE;
use crate::data::error::ResultBoxError;
use crate::data::error::{Res, ResultBoxError};
use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus};
use crate::data::user::UserPageStatus::PUBLIC;
use crate::helpers::{database, friends_helper};
@ -11,25 +11,34 @@ use crate::helpers::friends_helper::are_friend;
/// Get & return information about a user based on its ID
pub fn find_user_by_id(id: &UserID) -> ResultBoxError<User> {
exec_get_user_query(
database::QueryInfo::new(USERS_TABLE).cond_user_id("ID", id))
database::QueryInfo::new(USERS_TABLE)
.cond_user_id("ID", id)
.query_row(db_to_user)
}
/// Get & return information about a user based on his email
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
exec_get_user_query(
database::QueryInfo::new(USERS_TABLE).cond("mail", email))
database::QueryInfo::new(USERS_TABLE)
.cond("mail", email)
.query_row(db_to_user)
}
/// Get & return information about a user based on his virtual directory
pub fn find_user_by_virtual_directory(dir: &str) -> ResultBoxError<User> {
exec_get_user_query(
database::QueryInfo::new(USERS_TABLE).cond("sous_repertoire", dir))
database::QueryInfo::new(USERS_TABLE)
.cond("sous_repertoire", dir)
.query_row(db_to_user)
}
/// Get the entire list of Comunic users
pub fn get_all_users() -> Res<Vec<User>> {
database::QueryInfo::new(USERS_TABLE)
.exec(db_to_user)
}
/// Execute query & return result
fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
database::query_row(query, |res| {
fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
// Page status
let page_status = if res.get_int64("pageouverte")? == 1 {
@ -69,7 +78,12 @@ fn exec_get_user_query(query: database::QueryInfo) -> 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")?,
})
}