]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/deletion/delete.rs
Automatically resolve report when post/comment is removed (#3850)
[lemmy.git] / crates / apub / src / activities / deletion / delete.rs
index 1bb3b6c417df43f9a299e904c6cf247c007cb9e1..fa0a44aa38bcf1b7c2099fef7cc9a96435bf867a 100644 (file)
@@ -3,15 +3,16 @@ use crate::{
     deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
     generate_activity_id,
   },
-  insert_activity,
+  insert_received_activity,
   objects::person::ApubPerson,
   protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
 };
 use activitypub_federation::{config::Data, kinds::activity::DeleteType, traits::ActivityHandler};
-use lemmy_api_common::{context::LemmyContext, websocket::UserOperationCrud};
+use lemmy_api_common::{context::LemmyContext, utils::sanitize_html_opt};
 use lemmy_db_schema::{
   source::{
     comment::{Comment, CommentUpdateForm},
+    comment_report::CommentReport,
     community::{Community, CommunityUpdateForm},
     moderator::{
       ModRemoveComment,
@@ -22,10 +23,11 @@ use lemmy_db_schema::{
       ModRemovePostForm,
     },
     post::{Post, PostUpdateForm},
+    post_report::PostReport,
   },
-  traits::Crud,
+  traits::{Crud, Reportable},
 };
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::error::{LemmyError, LemmyErrorType};
 use url::Url;
 
 #[async_trait::async_trait]
@@ -43,13 +45,13 @@ impl ActivityHandler for Delete {
 
   #[tracing::instrument(skip_all)]
   async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
+    insert_received_activity(&self.id, context).await?;
     verify_delete_activity(self, self.summary.is_some(), context).await?;
     Ok(())
   }
 
   #[tracing::instrument(skip_all)]
   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
-    insert_activity(&self.id, &self, false, false, context).await?;
     if let Some(reason) = self.summary {
       // We set reason to empty string if it doesn't exist, to distinguish between delete and
       // remove. Here we change it back to option, so we don't write it to db.
@@ -105,13 +107,12 @@ pub(in crate::activities) async fn receive_remove_action(
   reason: Option<String>,
   context: &Data<LemmyContext>,
 ) -> Result<(), LemmyError> {
-  use UserOperationCrud::*;
+  let reason = sanitize_html_opt(&reason);
+
   match DeletableObjects::read_from_db(object, context).await? {
     DeletableObjects::Community(community) => {
       if community.local {
-        return Err(LemmyError::from_message(
-          "Only local admin can remove community",
-        ));
+        return Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?;
       }
       let form = ModRemoveCommunityForm {
         mod_person_id: actor.id,
@@ -120,55 +121,54 @@ pub(in crate::activities) async fn receive_remove_action(
         reason,
         expires: None,
       };
-      ModRemoveCommunity::create(context.pool(), &form).await?;
-      let deleted_community = Community::update(
-        context.pool(),
+      ModRemoveCommunity::create(&mut context.pool(), &form).await?;
+      Community::update(
+        &mut context.pool(),
         community.id,
-        &CommunityUpdateForm::builder().removed(Some(true)).build(),
+        &CommunityUpdateForm {
+          removed: Some(true),
+          ..Default::default()
+        },
       )
       .await?;
-
-      context
-        .send_community_ws_message(&RemoveCommunity, deleted_community.id, None, None)
-        .await?;
     }
     DeletableObjects::Post(post) => {
+      PostReport::resolve_all_for_object(&mut context.pool(), post.id, actor.id).await?;
       let form = ModRemovePostForm {
         mod_person_id: actor.id,
         post_id: post.id,
         removed: Some(true),
         reason,
       };
-      ModRemovePost::create(context.pool(), &form).await?;
-      let removed_post = Post::update(
-        context.pool(),
+      ModRemovePost::create(&mut context.pool(), &form).await?;
+      Post::update(
+        &mut context.pool(),
         post.id,
-        &PostUpdateForm::builder().removed(Some(true)).build(),
+        &PostUpdateForm {
+          removed: Some(true),
+          ..Default::default()
+        },
       )
       .await?;
-
-      context
-        .send_post_ws_message(&RemovePost, removed_post.id, None, None)
-        .await?;
     }
     DeletableObjects::Comment(comment) => {
+      CommentReport::resolve_all_for_object(&mut context.pool(), comment.id, actor.id).await?;
       let form = ModRemoveCommentForm {
         mod_person_id: actor.id,
         comment_id: comment.id,
         removed: Some(true),
         reason,
       };
-      ModRemoveComment::create(context.pool(), &form).await?;
-      let removed_comment = Comment::update(
-        context.pool(),
+      ModRemoveComment::create(&mut context.pool(), &form).await?;
+      Comment::update(
+        &mut context.pool(),
         comment.id,
-        &CommentUpdateForm::builder().removed(Some(true)).build(),
+        &CommentUpdateForm {
+          removed: Some(true),
+          ..Default::default()
+        },
       )
       .await?;
-
-      context
-        .send_comment_ws_message_simple(&RemoveComment, removed_comment.id)
-        .await?;
     }
     DeletableObjects::PrivateMessage(_) => unimplemented!(),
   }