]> Untitled Git - lemmy.git/commitdiff
Check if post or comment are deleted first. Fixes #1864 (#1867)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 3 Nov 2021 17:47:24 +0000 (13:47 -0400)
committerGitHub <noreply@github.com>
Wed, 3 Nov 2021 17:47:24 +0000 (17:47 +0000)
* Check if post or comment are deleted first. Fixes #1864

* Refactoring delete apub.

* Revert "Refactoring delete apub."

This reverts commit ba2c3d06cfb870efe792f4b2541036265b425156.

crates/api_crud/src/comment/delete.rs
crates/api_crud/src/post/delete.rs
crates/apub/src/activities/deletion/delete.rs
crates/apub/src/activities/deletion/mod.rs
crates/apub/src/activities/deletion/undo_delete.rs
crates/apub/src/objects/comment.rs
crates/apub/src/objects/post.rs

index 19851f0c0556a093f1d50ab0739182301fca244c..f55119f72a9812fefef4ebed32bfedb65ad71dc9 100644 (file)
@@ -44,6 +44,11 @@ impl PerformCrud for DeleteComment {
     })
     .await??;
 
+    // Dont delete it if its already been deleted.
+    if orig_comment.comment.deleted == data.deleted {
+      return Err(ApiError::err_plain("couldnt_update_comment").into());
+    }
+
     check_community_ban(
       local_user_view.person.id,
       orig_comment.community.id,
index a701e835560e86b2061e34a9e8331fdd718d1f90..af69993480b5e6f2e3b46d77510c6bc8fc9f1462 100644 (file)
@@ -36,6 +36,11 @@ impl PerformCrud for DeletePost {
     let post_id = data.post_id;
     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
 
+    // Dont delete it if its already been deleted.
+    if orig_post.deleted == data.deleted {
+      return Err(ApiError::err_plain("couldnt_update_post").into());
+    }
+
     check_community_ban(
       local_user_view.person.id,
       orig_post.community_id,
index 672fca52a53aa80e9fe804318a000141e21f9287..a4112a0af5550f173a9fdb2985b256f0e4a535f9 100644 (file)
@@ -33,12 +33,7 @@ use lemmy_websocket::{
 use crate::{
   activities::{
     community::{announce::GetCommunity, send_to_community},
-    deletion::{
-      receive_delete_action,
-      verify_delete_activity,
-      DeletableObjects,
-      WebsocketMessages,
-    },
+    deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
     generate_activity_id,
     verify_activity,
     verify_is_public,
@@ -87,19 +82,7 @@ impl ActivityHandler for Delete {
       };
       receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
     } else {
-      receive_delete_action(
-        &self.object,
-        &self.actor,
-        WebsocketMessages {
-          community: UserOperationCrud::DeleteCommunity,
-          post: UserOperationCrud::DeletePost,
-          comment: UserOperationCrud::DeleteComment,
-        },
-        true,
-        context,
-        request_counter,
-      )
-      .await
+      receive_delete_action(&self.object, &self.actor, true, context, request_counter).await
     }
   }
 }
index b9c11291fc0e61e2875d5c3e06ede4d8706139ea..3e9a7a3a2c21b0aa0f9a93c1e2772842600b5146 100644 (file)
@@ -143,19 +143,12 @@ async fn verify_delete_activity_post_or_comment(
   Ok(())
 }
 
-struct WebsocketMessages {
-  community: UserOperationCrud,
-  post: UserOperationCrud,
-  comment: UserOperationCrud,
-}
-
 /// Write deletion or restoring of an object to the database, and send websocket message.
 /// TODO: we should do something similar for receive_remove_action(), but its much more complicated
 ///       because of the mod log
 async fn receive_delete_action(
   object: &Url,
   actor: &ObjectId<ApubPerson>,
-  ws_messages: WebsocketMessages,
   deleted: bool,
   context: &LemmyContext,
   request_counter: &mut i32,
@@ -172,21 +165,44 @@ async fn receive_delete_action(
         Community::update_deleted(conn, community.id, deleted)
       })
       .await??;
-      send_community_ws_message(community.id, ws_messages.community, None, None, context).await?;
+      send_community_ws_message(
+        community.id,
+        UserOperationCrud::DeleteCommunity,
+        None,
+        None,
+        context,
+      )
+      .await?;
     }
     DeletableObjects::Post(post) => {
-      let deleted_post = blocking(context.pool(), move |conn| {
-        Post::update_deleted(conn, post.id, deleted)
-      })
-      .await??;
-      send_post_ws_message(deleted_post.id, ws_messages.post, None, None, context).await?;
+      if deleted != post.deleted {
+        let deleted_post = blocking(context.pool(), move |conn| {
+          Post::update_deleted(conn, post.id, deleted)
+        })
+        .await??;
+        send_post_ws_message(
+          deleted_post.id,
+          UserOperationCrud::DeletePost,
+          None,
+          None,
+          context,
+        )
+        .await?;
+      }
     }
     DeletableObjects::Comment(comment) => {
-      let deleted_comment = blocking(context.pool(), move |conn| {
-        Comment::update_deleted(conn, comment.id, deleted)
-      })
-      .await??;
-      send_comment_ws_message_simple(deleted_comment.id, ws_messages.comment, context).await?;
+      if deleted != comment.deleted {
+        let deleted_comment = blocking(context.pool(), move |conn| {
+          Comment::update_deleted(conn, comment.id, deleted)
+        })
+        .await??;
+        send_comment_ws_message_simple(
+          deleted_comment.id,
+          UserOperationCrud::DeleteComment,
+          context,
+        )
+        .await?;
+      }
     }
   }
   Ok(())
index c1689119088e09f3eb197d59cd8e04cc84d2160b..2de4aefd3e8268a0baf6cd6ba260325e7b2bea30 100644 (file)
@@ -18,12 +18,7 @@ use lemmy_websocket::{
 use crate::{
   activities::{
     community::{announce::GetCommunity, send_to_community},
-    deletion::{
-      receive_delete_action,
-      verify_delete_activity,
-      DeletableObjects,
-      WebsocketMessages,
-    },
+    deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
     generate_activity_id,
     verify_activity,
     verify_is_public,
@@ -69,11 +64,6 @@ impl ActivityHandler for UndoDelete {
       receive_delete_action(
         &self.object.object,
         &self.actor,
-        WebsocketMessages {
-          community: UserOperationCrud::EditCommunity,
-          post: UserOperationCrud::EditPost,
-          comment: UserOperationCrud::EditComment,
-        },
         false,
         context,
         request_counter,
index e5ffb7ca2e8269012626a8962ab1150119d6075c..08a55bf9cd8bdbd603bcaa89be4b41f0f578ca16 100644 (file)
@@ -81,10 +81,12 @@ impl ApubObject for ApubComment {
   }
 
   async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
-    blocking(context.pool(), move |conn| {
-      Comment::update_deleted(conn, self.id, true)
-    })
-    .await??;
+    if !self.deleted {
+      blocking(context.pool(), move |conn| {
+        Comment::update_deleted(conn, self.id, true)
+      })
+      .await??;
+    }
     Ok(())
   }
 
index c19c62779fa31a18d5a5d1c56167bb31724ddf4b..b835f812eaecdb9aa1b1fc789e82a3e1c42f3724 100644 (file)
@@ -76,10 +76,12 @@ impl ApubObject for ApubPost {
   }
 
   async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
-    blocking(context.pool(), move |conn| {
-      Post::update_deleted(conn, self.id, true)
-    })
-    .await??;
+    if !self.deleted {
+      blocking(context.pool(), move |conn| {
+        Post::update_deleted(conn, self.id, true)
+      })
+      .await??;
+    }
     Ok(())
   }