Skip to content

A platform for community discussion. Free, open, simple.

FIX: Empty post reviewable ignore bundle causing client errors (#29932)

Martin Brennanc7e471d35a0d
diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rb
index 6fb8e89700b..1c870b53840 100644
--- a/app/models/reviewable_flagged_post.rb
+++ b/app/models/reviewable_flagged_post.rb
@@ -109,6 +109,13 @@ class ReviewableFlaggedPost < Reviewable
build_action(actions, :disagree, icon: "thumbs-down")
end
+ post_visible_or_system_user = !post.hidden? || guardian.user.is_system_user?
+ can_delete_post_or_topic = guardian.can_delete_post_or_topic?(post)
+
+ # We must return early in this case otherwise we can end up with a bundle
+ # with no associated actions, which is not valid on the client.
+ return if !can_delete_post_or_topic && !post_visible_or_system_user
+
ignore =
actions.add_bundle(
"#{id}-ignore",
@@ -116,10 +123,10 @@ class ReviewableFlaggedPost < Reviewable
label: "reviewables.actions.ignore.title",
)
- if !post.hidden? || guardian.user.is_system_user?
+ if post_visible_or_system_user
build_action(actions, :ignore_and_do_nothing, icon: "up-right-from-square", bundle: ignore)
end
- if guardian.can_delete_post_or_topic?(post)
+ if can_delete_post_or_topic
build_action(actions, :delete_and_ignore, icon: "trash-can", bundle: ignore)
if post.reply_count > 0
build_action(
diff --git a/spec/models/reviewable_flagged_post_spec.rb b/spec/models/reviewable_flagged_post_spec.rb
index c556a3b1ed6..bbfcae30d2b 100644
--- a/spec/models/reviewable_flagged_post_spec.rb
+++ b/spec/models/reviewable_flagged_post_spec.rb
@@ -83,6 +83,17 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(reviewable.actions_for(guardian).has?(:agree_and_suspend)).to eq(false)
end
+ it "doesn't end up with an empty ignore bundle when the post is already hidden and deleted" do
+ post.update!(hidden: true)
+ post.topic.trash!
+ post.trash!
+ expect(reviewable.actions_for(guardian).has?(:ignore_and_do_nothing)).to eq(false)
+ expect(reviewable.actions_for(guardian).has?(:delete_and_ignore)).to eq(false)
+ expect(
+ reviewable.actions_for(guardian).bundles.find { |bundle| bundle.id.include?("-ignore") },
+ ).to be_blank
+ end
+
context "when flagged as potential_spam" do
before { reviewable.update!(potential_spam: true) }