A platform for community discussion. Free, open, simple.
Adjusts the `minimum_flag_threshold` for TL3/TL4 actions
Robin Ward5eaf3cb10474
diff --git a/app/models/post_action.rb b/app/models/post_action.rbindex 8efa750b536..1e533b0a9b8 100644--- a/app/models/post_action.rb+++ b/app/models/post_action.rb@@ -52,6 +52,26 @@ class PostAction < ActiveRecord::Base.countend+ # Forums can choose to apply a minimum number of flags required before it shows up in+ # the admin interface. One exception is posts hidden by tl3/tl4 - we want those to+ # show up even if the minimum visibility is not met.+ def self.apply_minimum_visibility(relation)+ return relation unless SiteSetting.min_flags_staff_visibility > 1++ params = {+ min_flags: SiteSetting.min_flags_staff_visibility,+ hidden_reasons: Post.hidden_reasons.only(:flagged_by_tl3_user, :flagged_by_tl4_user).values+ }++ relation.having(<<~SQL, params)+ (COUNT(*) >= :min_flags) OR+ (SUM(CASE+ WHEN posts.hidden_reason_id IN (:hidden_reasons) THEN 1+ ELSE 0+ END) > 0)+ SQL+ end+def self.update_flagged_posts_countflagged_relation = PostAction.active.flags@@ -61,10 +81,7 @@ class PostAction < ActiveRecord::Base.where('posts.user_id > 0').group("posts.id")- if SiteSetting.min_flags_staff_visibility > 1- flagged_relation = flagged_relation- .having("count(*) >= ?", SiteSetting.min_flags_staff_visibility)- end+ flagged_relation = apply_minimum_visibility(flagged_relation)posts_flagged_count = flagged_relation.pluck("posts.id")@@ -638,6 +655,7 @@ class PostAction < ActiveRecord::Basemessage_type: hiding_again ? :post_hidden_again : :post_hidden,message_options: options)end+ update_flagged_posts_countenddef self.guess_hide_reason(post)diff --git a/lib/flag_query.rb b/lib/flag_query.rbindex f3ef77ec0f2..cd0ed150892 100644--- a/lib/flag_query.rb+++ b/lib/flag_query.rb@@ -35,8 +35,8 @@ module FlagQuery.group(:post_id).order('MIN(post_actions.created_at) DESC')- if opts[:filter] != "old" && SiteSetting.min_flags_staff_visibility > 1- post_ids_relation = post_ids_relation.having("count(*) >= ?", SiteSetting.min_flags_staff_visibility)+ if opts[:filter] != "old"+ post_ids_relation = PostAction.apply_minimum_visibility(post_ids_relation)endpost_ids = post_ids_relation.pluck(:post_id).uniqdiff --git a/spec/components/flag_query_spec.rb b/spec/components/flag_query_spec.rbindex 428a5aa319f..aee160780e7 100644--- a/spec/components/flag_query_spec.rb+++ b/spec/components/flag_query_spec.rb@@ -114,11 +114,11 @@ describe FlagQuery doit "respects `min_flags_staff_visibility`" doadmin = Fabricate(:admin)- moderator = Fabricate(:moderator)+ flagger = Fabricate(:user)post = create_post- PostAction.act(moderator, post, PostActionType.types[:spam])+ PostAction.act(flagger, post, PostActionType.types[:spam])SiteSetting.min_flags_staff_visibility = 2posts, topics, users = FlagQuery.flagged_posts_report(admin)@@ -133,5 +133,33 @@ describe FlagQuery doexpect(users).to be_presentend+ it "respects `min_flags_staff_visibility` for tl3 hidden spam" do+ admin = Fabricate(:admin)+ tl3 = Fabricate(:user, trust_level: 3)+ post = create_post++ post.user.update_column(:trust_level, 0)+ PostAction.act(tl3, post, PostActionType.types[:spam])++ SiteSetting.min_flags_staff_visibility = 2+ posts, topics, users = FlagQuery.flagged_posts_report(admin)+ expect(posts).to be_present+ expect(topics).to be_present+ expect(users).to be_present+ end++ it "respects `min_flags_staff_visibility` for tl4 hidden posts" do+ admin = Fabricate(:admin)+ tl4 = Fabricate(:user, trust_level: 4)+ post = create_post+ PostAction.act(tl4, post, PostActionType.types[:spam])++ SiteSetting.min_flags_staff_visibility = 2+ posts, topics, users = FlagQuery.flagged_posts_report(admin)+ expect(posts).to be_present+ expect(topics).to be_present+ expect(users).to be_present+ end+endenddiff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rbindex a99ecc795e8..823128d1a89 100644--- a/spec/models/post_action_spec.rb+++ b/spec/models/post_action_spec.rb@@ -160,6 +160,26 @@ describe PostAction doexpect(PostAction.flagged_posts_count).to eq(1)end+ it "tl3 hidden posts will supersede min_flags_staff_visibility" do+ SiteSetting.min_flags_staff_visibility = 2+ expect(PostAction.flagged_posts_count).to eq(0)++ codinghorror.update_column(:trust_level, 3)+ post.user.update_column(:trust_level, 0)+ PostAction.act(codinghorror, post, PostActionType.types[:spam])+ expect(PostAction.flagged_posts_count).to eq(1)+ end++ it "tl4 hidden posts will supersede min_flags_staff_visibility" do+ SiteSetting.min_flags_staff_visibility = 2+ expect(PostAction.flagged_posts_count).to eq(0)++ codinghorror.update_column(:trust_level, 4)+ PostAction.act(codinghorror, post, PostActionType.types[:off_topic])++ expect(PostAction.flagged_posts_count).to eq(1)+ end+it "should reset counts when a topic is deleted" doPostAction.act(codinghorror, post, PostActionType.types[:off_topic])post.topic.trash!