Skip to content

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

FIX: Prevent Flag Score Inflation in PostAction (#42233)

Isaac Janzena6e2ca8d537d
diff --git a/lib/post_action_destroyer.rb b/lib/post_action_destroyer.rb
index a44d7e8f2ba..f9b4476f1d6 100644
--- a/lib/post_action_destroyer.rb
+++ b/lib/post_action_destroyer.rb
@@ -53,6 +53,7 @@ class PostActionDestroyer
).performed!
post_action.remove_act!(@destroyed_by)
+ remove_reviewable_score if post_action.is_flag?
if post_action.staff_took_action
post_action.post.acting_user = @destroyed_by
post_action.post.unhide!
@@ -96,6 +97,23 @@ class PostActionDestroyer
end
end
+ def remove_reviewable_score
+ reviewable = @post.reviewable_flag
+ return if reviewable.blank?
+
+ deleted_count =
+ ReviewableScore
+ .pending
+ .where(
+ reviewable: reviewable,
+ user: @destroyed_by,
+ reviewable_score_type: @post_action_type_id,
+ )
+ .delete_all
+
+ reviewable.recalculate_score if deleted_count > 0
+ end
+
def guardian
@guardian ||= Guardian.new(@destroyed_by)
end
diff --git a/spec/lib/post_action_creator_spec.rb b/spec/lib/post_action_creator_spec.rb
index 934557624c1..13e6d3d443a 100644
--- a/spec/lib/post_action_creator_spec.rb
+++ b/spec/lib/post_action_creator_spec.rb
@@ -217,6 +217,25 @@ RSpec.describe PostActionCreator do
)
end
+ it "does not auto-hide a post when the same TL2 user repeatedly retracts and recreates a flag" do
+ Reviewable.set_priorities(high: 12.5)
+ SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:medium]
+ attacker = Fabricate(:user, trust_level: TrustLevel[2], refresh_auto_groups: true)
+
+ 2.times do
+ expect(PostActionCreator.inappropriate(attacker, post)).to be_success
+ expect(PostActionDestroyer.destroy(attacker, post, :inappropriate)).to be_success
+ end
+
+ result = PostActionCreator.inappropriate(attacker, post)
+ reviewable = result.reviewable.reload
+
+ expect(result).to be_success
+ expect(post.reload).not_to be_hidden
+ expect(reviewable.score).to eq(ReviewableScore.user_flag_score(attacker))
+ expect(reviewable.reviewable_scores.pending.count).to eq(1)
+ end
+
describe "Auto hide spam flagged posts" do
before do
user.trust_level = TrustLevel[3]