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

We ran into an edge case where it was possible for a
ReviewableFlaggedPost to end up in a state where it was hidden
and the topic was already deleted. This meant that the Ignore
action bundle for the reviewable ended up empty, with no associated
actions.

This commit fixes the server-side issue where this was ending up
empty. A further commit will aim to make the client more resilient
to these issues by gracefully failing if a reviewable action bundle
is detected with no associated actions.
This commit is contained in:
Martin Brennan 2024-11-26 16:18:32 +10:00 committed by GitHub
parent 15a61a0c1f
commit c7e471d35a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View File

@ -109,6 +109,13 @@ class ReviewableFlaggedPost < Reviewable
build_action(actions, :disagree, icon: "thumbs-down") build_action(actions, :disagree, icon: "thumbs-down")
end 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 = ignore =
actions.add_bundle( actions.add_bundle(
"#{id}-ignore", "#{id}-ignore",
@ -116,10 +123,10 @@ class ReviewableFlaggedPost < Reviewable
label: "reviewables.actions.ignore.title", 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) build_action(actions, :ignore_and_do_nothing, icon: "up-right-from-square", bundle: ignore)
end 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) build_action(actions, :delete_and_ignore, icon: "trash-can", bundle: ignore)
if post.reply_count > 0 if post.reply_count > 0
build_action( build_action(

View File

@ -83,6 +83,17 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
expect(reviewable.actions_for(guardian).has?(:agree_and_suspend)).to eq(false) expect(reviewable.actions_for(guardian).has?(:agree_and_suspend)).to eq(false)
end 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 context "when flagged as potential_spam" do
before { reviewable.update!(potential_spam: true) } before { reviewable.update!(potential_spam: true) }