2020-07-05 19:32:28 +02:00
|
|
|
//! # Comments helper
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::constants::database_tables_names::COMMENTS_TABLE;
|
|
|
|
use crate::data::comment::Comment;
|
2021-02-14 18:49:47 +01:00
|
|
|
use crate::data::error::{ExecError, Res, ResultBoxError};
|
|
|
|
use crate::data::user::{User, UserID};
|
|
|
|
use crate::helpers::{database, events_helper, likes_helper};
|
|
|
|
use crate::helpers::events_helper::Event;
|
2020-07-09 10:48:44 +02:00
|
|
|
use crate::helpers::likes_helper::LikeType;
|
2021-02-14 18:49:47 +01:00
|
|
|
use crate::utils::date_utils::{mysql_date, time};
|
2020-07-09 10:48:44 +02:00
|
|
|
use crate::utils::user_data_utils::user_data_path;
|
2020-07-05 19:32:28 +02:00
|
|
|
|
2020-07-10 08:21:40 +02:00
|
|
|
/// Create a new comment. In case of success, this function returns the ID of the created comment
|
|
|
|
pub fn create(c: &Comment) -> ResultBoxError<u64> {
|
|
|
|
let comment_id = database::InsertQuery::new(COMMENTS_TABLE)
|
|
|
|
.add_u64("ID_texte", c.post_id)
|
|
|
|
.add_user_id("ID_personne", &c.user_id)
|
|
|
|
.add_str("date_envoi", &mysql_date())
|
|
|
|
.add_u64("time_insert", c.time_sent)
|
|
|
|
.add_str("commentaire", &c.content)
|
|
|
|
.add_opt_str("image_commentaire", c.image_path.as_ref())
|
|
|
|
.insert()?
|
|
|
|
.ok_or(ExecError::new("No ID returned after comment creation!"))?;
|
|
|
|
|
2021-02-06 11:37:56 +01:00
|
|
|
// Emit an event
|
|
|
|
events_helper::propagate_event(&Event::NewComment(&get_single(comment_id)?))?;
|
2021-02-05 15:14:47 +01:00
|
|
|
|
2020-07-10 08:21:40 +02:00
|
|
|
Ok(comment_id)
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:32:28 +02:00
|
|
|
/// Get the comments of a post
|
|
|
|
pub fn get(post_id: u64) -> ResultBoxError<Vec<Comment>> {
|
|
|
|
database::QueryInfo::new(COMMENTS_TABLE)
|
|
|
|
.cond_u64("ID_texte", post_id)
|
|
|
|
.set_order("ID")
|
|
|
|
.exec(db_to_comment)
|
|
|
|
}
|
|
|
|
|
2020-07-10 08:45:53 +02:00
|
|
|
/// Get information about a single comment
|
|
|
|
pub fn get_single(comment_id: u64) -> ResultBoxError<Comment> {
|
|
|
|
database::QueryInfo::new(COMMENTS_TABLE)
|
|
|
|
.cond_u64("ID", comment_id)
|
|
|
|
.query_row(db_to_comment)
|
|
|
|
}
|
|
|
|
|
2020-07-13 19:12:39 +02:00
|
|
|
/// Export all the comments of a given user
|
|
|
|
pub fn export_all_user(user_id: &UserID) -> ResultBoxError<Vec<Comment>> {
|
|
|
|
database::QueryInfo::new(COMMENTS_TABLE)
|
|
|
|
.cond_user_id("ID_personne", user_id)
|
|
|
|
.exec(db_to_comment)
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:32:28 +02:00
|
|
|
|
|
|
|
/// Turn a database entry into a comment object
|
|
|
|
fn db_to_comment(row: &database::RowResult) -> ResultBoxError<Comment> {
|
|
|
|
Ok(Comment {
|
|
|
|
id: row.get_u64("ID")?,
|
|
|
|
time_sent: row.get_u64("time_insert").unwrap_or(0),
|
|
|
|
user_id: row.get_user_id("ID_personne")?,
|
|
|
|
post_id: row.get_u64("ID_texte")?,
|
|
|
|
content: row.get_str("commentaire")?,
|
2020-07-14 09:17:03 +02:00
|
|
|
image_path: row.get_optional_str("image_commentaire")?
|
|
|
|
.map(|f| f.replace("file:", "")),
|
2020-07-05 19:32:28 +02:00
|
|
|
})
|
2020-07-09 10:48:44 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 08:57:46 +02:00
|
|
|
/// Update comment content
|
|
|
|
pub fn edit(comment_id: u64, new_content: &str) -> ResultBoxError {
|
|
|
|
database::UpdateInfo::new(COMMENTS_TABLE)
|
|
|
|
.cond_u64("ID", comment_id)
|
|
|
|
.set_str("commentaire", new_content)
|
2021-02-05 15:14:47 +01:00
|
|
|
.exec()?;
|
|
|
|
|
2021-02-06 11:37:56 +01:00
|
|
|
// Emit an event
|
|
|
|
events_helper::propagate_event(&Event::UpdatedComment(&get_single(comment_id)?))?;
|
2021-02-05 15:14:47 +01:00
|
|
|
|
|
|
|
Ok(())
|
2020-07-10 08:57:46 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 10:48:44 +02:00
|
|
|
/// Delete a single comment
|
|
|
|
pub fn delete(c: &Comment) -> ResultBoxError {
|
|
|
|
// Delete associated image (if any)
|
|
|
|
if let Some(image) = &c.image_path {
|
|
|
|
let path = user_data_path(image.as_ref());
|
|
|
|
if path.exists() {
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the likes associated with the comment
|
|
|
|
likes_helper::delete_all(c.id, LikeType::COMMENT)?;
|
|
|
|
|
|
|
|
// Remove the comment from the database
|
|
|
|
database::DeleteQuery::new(COMMENTS_TABLE)
|
|
|
|
.cond_u64("ID", c.id)
|
|
|
|
.exec()?;
|
|
|
|
|
2021-02-06 11:37:56 +01:00
|
|
|
// Emit an event
|
|
|
|
events_helper::propagate_event(&Event::DeletedComment(c))?;
|
2021-02-05 15:14:47 +01:00
|
|
|
|
2020-07-09 10:48:44 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete all the comments associated to a post
|
|
|
|
pub fn delete_all(post_id: u64) -> ResultBoxError {
|
|
|
|
for c in &get(post_id)? {
|
|
|
|
delete(c)?;
|
|
|
|
}
|
|
|
|
|
2021-01-21 18:13:01 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Delete all the comments created by a user
|
|
|
|
pub fn delete_all_user(user_id: &UserID) -> ResultBoxError {
|
|
|
|
for comment in &export_all_user(user_id)? {
|
|
|
|
delete(comment)?;
|
|
|
|
}
|
|
|
|
|
2021-02-14 18:49:47 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clean old user comments
|
|
|
|
pub fn clean_old_comments(user: &User) -> Res {
|
2021-02-15 17:38:25 +01:00
|
|
|
let lifetime = user.delete_comments_after.unwrap_or(0);
|
|
|
|
if lifetime < 1 {
|
2021-02-14 18:49:47 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let comments = database::QueryInfo::new(COMMENTS_TABLE)
|
|
|
|
.cond_user_id("ID_personne", &user.id)
|
|
|
|
.set_custom_where("time_insert < ?")
|
2021-02-15 17:38:25 +01:00
|
|
|
.add_custom_where_argument_u64(time() - lifetime)
|
2021-02-14 18:49:47 +01:00
|
|
|
.exec(db_to_comment)?;
|
|
|
|
|
|
|
|
for comment in comments {
|
|
|
|
delete(&comment)?;
|
|
|
|
}
|
|
|
|
|
2020-07-09 10:48:44 +02:00
|
|
|
Ok(())
|
2020-07-05 19:32:28 +02:00
|
|
|
}
|