1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-03-13 17:32:38 +00:00
comunicapiv3/src/helpers/likes_helper.rs

122 lines
3.5 KiB
Rust
Raw Normal View History

2020-06-01 10:17:32 +02:00
//! # Likes helper
//!
//! Module dedicated to likes management
use crate::constants::database_tables_names::LIKES_TABLE;
2021-02-14 16:45:00 +01:00
use crate::data::error::{Res, ResultBoxError};
use crate::data::user::{User, UserID};
2020-07-13 19:26:19 +02:00
use crate::data::user_like::UserLike;
2020-07-09 10:37:06 +02:00
use crate::helpers::database;
use crate::helpers::database::QueryInfo;
2021-02-14 16:45:00 +01:00
use crate::utils::date_utils::{mysql_date, time, time_to_mysql_date};
2020-06-01 10:17:32 +02:00
2020-07-10 10:24:39 +02:00
#[derive(Copy, Clone)]
2020-06-01 10:17:32 +02:00
pub enum LikeType {
USER,
POST,
COMMENT,
2020-07-09 10:37:06 +02:00
GROUP,
2020-06-01 10:17:32 +02:00
}
impl LikeType {
/// Get matching database type
pub fn to_db_type(&self) -> String {
match self {
LikeType::USER => "page".to_string(),
LikeType::POST => "texte".to_string(),
LikeType::COMMENT => "commentaire".to_string(),
LikeType::GROUP => "group".to_string(),
}
}
}
/// Count the number of likes over an element
pub fn count(id: u64, kind: LikeType) -> ResultBoxError<usize> {
QueryInfo::new(LIKES_TABLE)
.cond_u64("ID_type", id)
.cond("type", kind.to_db_type().as_ref())
.exec_count()
}
/// Check if a user likes an element or not
2020-06-25 10:08:34 +02:00
pub fn is_liking(user_id: &UserID, id: u64, kind: LikeType) -> ResultBoxError<bool> {
if !user_id.is_valid() {
return Ok(false);
}
Ok(QueryInfo::new(LIKES_TABLE)
.cond_u64("ID_type", id)
2020-06-25 10:08:34 +02:00
.cond_user_id("ID_personne", user_id)
.cond("type", kind.to_db_type().as_ref())
.exec_count()? > 0)
2020-07-09 10:37:06 +02:00
}
2020-07-10 10:24:39 +02:00
/// Update like status
pub fn update(user_id: &UserID, liking: bool, id: u64, kind: LikeType) -> ResultBoxError {
if !liking {
return database::DeleteQuery::new(LIKES_TABLE)
.cond_user_id("ID_personne", user_id)
.cond_u64("ID_type", id)
.cond_str("type", &kind.to_db_type())
.exec();
}
if is_liking(user_id, id, kind)? {
return Ok(());
}
database::InsertQuery::new(LIKES_TABLE)
.add_user_id("ID_personne", user_id)
.add_u64("ID_type", id)
.add_str("Date_envoi", &mysql_date())
.add_str("type", &kind.to_db_type())
.insert_drop_result()
}
2020-07-09 10:37:06 +02:00
/// Delete all the likes associated with a post
pub fn delete_all(id: u64, kind: LikeType) -> ResultBoxError {
database::DeleteQuery::new(LIKES_TABLE)
.cond_u64("ID_type", id)
.cond_str("type", &kind.to_db_type())
.exec()
2020-07-13 19:26:19 +02:00
}
/// Export all the likes mention of the user
pub fn export_all_user(user_id: &UserID) -> ResultBoxError<Vec<UserLike>> {
database::QueryInfo::new(LIKES_TABLE)
.cond_user_id("ID_personne", user_id)
.exec(db_to_user_like)
}
2021-01-21 18:26:18 +01:00
/// Delete all the likes created by a user
pub fn delete_all_user(user_id: &UserID) -> ResultBoxError {
database::DeleteQuery::new(LIKES_TABLE)
.cond_user_id("ID_personne", user_id)
.exec()
}
2021-02-14 16:45:00 +01:00
/// Delete old user likes
pub fn clean_old_user_likes(user: &User) -> Res {
2021-02-15 17:38:25 +01:00
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()?;
2021-02-14 16:45:00 +01:00
}
2021-02-15 17:38:25 +01:00
Ok(())
2021-02-14 16:45:00 +01:00
}
2020-07-13 19:26:19 +02:00
/// Turn a database entry into a like entry
fn db_to_user_like(r: &database::RowResult) -> ResultBoxError<UserLike> {
Ok(UserLike {
id: r.get_u64("ID")?,
user_id: r.get_user_id("ID_personne")?,
time_sent: r.get_date_as_time("Date_envoi")?,
elem_type: r.get_str("type")?,
elem_id: r.get_u64("ID_type")?,
})
2020-06-01 10:17:32 +02:00
}