From fbeb66f70e7d41eb57c47a680994f3926480f144 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 17 Mar 2022 17:47:25 +0100 Subject: [PATCH] Automatically delete reports on associated resources deletion --- src/helpers/account_helper.rs | 4 ++++ src/helpers/comments_helper.rs | 12 ++++++++---- src/helpers/conversations_helper.rs | 9 ++++++++- src/helpers/groups_helper.rs | 6 +++++- src/helpers/posts_helper.rs | 6 +++++- src/helpers/reports_helper.rs | 11 ++++++++++- 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 9c8a341..854e66e 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -11,6 +11,7 @@ use crate::data::lang_settings::LangSettings; use crate::data::new_account::NewAccount; use crate::data::new_data_conservation_policy::NewDataConservationPolicy; use crate::data::new_notifications_settings::NewNotificationsSettings; +use crate::data::report::ReportTarget; use crate::data::security_settings::SecuritySettings; use crate::data::user::{AccountImageVisibility, User, UserID, UserPageVisibility}; use crate::data::user_token::{PushNotificationToken, UserAccessToken}; @@ -408,6 +409,9 @@ pub async fn delete(user_id: &UserID) -> ResultBoxError { // Delete all the reports of the user reports_helper::delete_all_from_user(user_id)?; + // Delete all the reports targeting the user + reports_helper::delete_all_for_target(ReportTarget::User(user_id.clone()))?; + // Delete all forez presences forez_presence_helper::delete_all_user(user_id)?; diff --git a/src/helpers/comments_helper.rs b/src/helpers/comments_helper.rs index 450124c..1370f64 100644 --- a/src/helpers/comments_helper.rs +++ b/src/helpers/comments_helper.rs @@ -5,8 +5,9 @@ use crate::constants::database_tables_names::COMMENTS_TABLE; use crate::data::comment::Comment; use crate::data::error::{ExecError, Res, ResultBoxError}; +use crate::data::report::ReportTarget; use crate::data::user::{User, UserID}; -use crate::helpers::{database, events_helper, likes_helper}; +use crate::helpers::{database, events_helper, likes_helper, reports_helper}; use crate::helpers::events_helper::Event; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::{mysql_date, time}; @@ -81,6 +82,12 @@ pub async fn edit(comment_id: u64, new_content: &str) -> ResultBoxError { /// Delete a single comment pub async fn delete(c: &Comment) -> ResultBoxError { + // Delete associated reports + reports_helper::delete_all_for_target(ReportTarget::Comment(c.id))?; + + // Remove the likes associated with the comment + likes_helper::delete_all(c.id, LikeType::COMMENT)?; + // Delete associated image (if any) if let Some(image) = &c.image_path { let path = user_data_path(image.as_ref()); @@ -89,9 +96,6 @@ pub async fn delete(c: &Comment) -> ResultBoxError { } } - // 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) diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index f72739a..7b83ca5 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -10,8 +10,9 @@ use crate::data::group_id::GroupID; use crate::data::group_member::GroupMembershipLevel; use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; +use crate::data::report::ReportTarget; use crate::data::user::{User, UserID}; -use crate::helpers::{database, events_helper, groups_helper}; +use crate::helpers::{database, events_helper, groups_helper, reports_helper}; use crate::helpers::database::{InsertQuery, UpdateInfo}; use crate::helpers::events_helper::Event; use crate::utils::date_utils::time; @@ -407,6 +408,9 @@ pub async fn update_message_content(msg_id: u64, new_content: &str) -> ResultBox /// Remove a message from a conversation pub async fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> { + // Delete associated reports + reports_helper::delete_all_for_target(ReportTarget::ConversationMessage(msg.id))?; + // Delete associated files if let Some(file) = &msg.file { delete_user_data_file_if_exists(&file.path)?; @@ -531,6 +535,9 @@ pub async fn update_members_list_for_group_conversation(conv_id: ConvID) -> Res /// Remove permanently a conversation pub async fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> { + // Delete associated reports + reports_helper::delete_all_for_target(ReportTarget::Conversation(conv.id))?; + // Delete all the messages of the conversations for message in get_all_messages(conv.id)? { delete_message(&message).await?; diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index f020f62..c5999fc 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -8,8 +8,9 @@ use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, Group use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::new_group::NewGroup; +use crate::data::report::ReportTarget; use crate::data::user::UserID; -use crate::helpers::{conversations_helper, database, forez_presence_helper, likes_helper, notifications_helper, posts_helper}; +use crate::helpers::{conversations_helper, database, forez_presence_helper, likes_helper, notifications_helper, posts_helper, reports_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::time; @@ -490,6 +491,9 @@ pub fn can_user_create_posts(group_id: &GroupID, user_id: &UserID) -> ResultBoxE /// Delete a group pub async fn delete(group_id: &GroupID) -> ResultBoxError { + // Delete associated reports + reports_helper::delete_all_for_target(ReportTarget::Group(group_id.clone()))?; + // Delete all likes of the group likes_helper::delete_all(group_id.id(), LikeType::GROUP)?; diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index ae8cad9..259b73c 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -10,8 +10,9 @@ use crate::data::group_id::GroupID; use crate::data::group_member::GroupMembershipLevel; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; use crate::data::post::PostKind::{POST_KIND_COUNTDOWN, POST_KIND_IMAGE, POST_KIND_PDF, POST_KIND_SURVEY, POST_KIND_WEBLINK, POST_KIND_YOUTUBE}; +use crate::data::report::ReportTarget; use crate::data::user::{User, UserID}; -use crate::helpers::{comments_helper, database, friends_helper, groups_helper, likes_helper, notifications_helper, survey_helper, user_helper}; +use crate::helpers::{comments_helper, database, friends_helper, groups_helper, likes_helper, notifications_helper, reports_helper, survey_helper, user_helper}; use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::{mysql_date, time}; use crate::utils::user_data_utils::user_data_path; @@ -422,6 +423,9 @@ pub async fn delete(p: &Post) -> ResultBoxError { // Delete all the comments associated to the post comments_helper::delete_all(p.id).await?; + // Delete associated reports + reports_helper::delete_all_for_target(ReportTarget::Post(p.id))?; + // Delete associated file / resource (if any) match &p.kind { diff --git a/src/helpers/reports_helper.rs b/src/helpers/reports_helper.rs index 4322d43..28302f7 100644 --- a/src/helpers/reports_helper.rs +++ b/src/helpers/reports_helper.rs @@ -1,6 +1,6 @@ use crate::constants::database_tables_names::REPORTS_TABLE; use crate::data::error::Res; -use crate::data::report::{Report, ReportID}; +use crate::data::report::{Report, ReportID, ReportTarget}; use crate::data::user::UserID; use crate::helpers::database; @@ -35,4 +35,13 @@ pub fn delete_all_from_user(user: &UserID) -> Res { database::DeleteQuery::new(REPORTS_TABLE) .cond_user_id("user_id", user) .exec() +} + +/// Delete all the reports targeting a specific resource +pub fn delete_all_for_target(target: ReportTarget) -> Res { + let (target_type, target_id) = target.to_db(); + database::DeleteQuery::new(REPORTS_TABLE) + .cond_str("target_type", target_type) + .cond_u64("target_id", target_id) + .exec() } \ No newline at end of file