FIX: Resolve issue where deleted spam topics marked as Not Spam were not being recovered (#10322)

FIX: Resolve issue where deleted spam topics marked as Not Spam were not being recovered (#10322)

If a user posted a topic and Akismet decided it was spam, the topic gets deleted and put into the review queue. If a category moderator for that category marked the post/topic as “Not Spam” the topic did not get recovered correctly because Guardian.new(@user).can_review_topic?(@post.topic) returned false incorrectly because the topic was deleted.

diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index ff7240b..4e969f9 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -214,7 +214,8 @@ class PostDestroyer
   private
 
   def post_is_reviewable?
-    Guardian.new(@user).can_review_topic?(@post.topic) && Reviewable.exists?(target: @post)
+    topic = @post.topic || Topic.with_deleted.find(@post.topic_id)
+    Guardian.new(@user).can_review_topic?(topic) && Reviewable.exists?(target: @post)
   end
 
   # we need topics to change if ever a post in them is deleted or created
diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb
index 75eb149..99a8bd3 100644
--- a/spec/components/post_destroyer_spec.rb
+++ b/spec/components/post_destroyer_spec.rb
@@ -289,7 +289,7 @@ describe PostDestroyer do
               ReviewableFlaggedPost.needs_review!(target: @reply, created_by: Fabricate(:user))
             end
 
-            it "changes deleted_at to nil" do
+            def changes_deleted_at_to_nil
               PostDestroyer.new(Discourse.system_user, @reply).destroy
               @reply.reload
               expect(@reply.user_deleted).to eq(false)
@@ -299,6 +299,19 @@ describe PostDestroyer do
               @reply.reload
               expect(@reply.deleted_at).to eq(nil)
             end
+
+            it "changes deleted_at to nil" do
+              changes_deleted_at_to_nil
+            end
+
+            context "when the topic is deleted" do
+              before do
+                @reply.topic.trash!
+              end
+              it "changes deleted_at to nil" do
+                changes_deleted_at_to_nil
+              end
+            end
           end
 
           context "when the post does not have a Reviewable record" do

GitHub sha: 2e5b2d20

1 Like

This commit appears in #10322 which was merged by martin.