From 70884d24362df00e3b324c669f9f7c7505396c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 1 Oct 2014 18:53:17 +0200 Subject: [PATCH] FEATURE: automatically hide posts made by TL0 users when flagged by a TL3 user --- app/models/post.rb | 7 ++++++- app/models/post_action.rb | 14 ++++++++++---- spec/models/post_action_spec.rb | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 6dab76e33..0d311af67 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -62,7 +62,12 @@ class Post < ActiveRecord::Base delegate :username, to: :user def self.hidden_reasons - @hidden_reasons ||= Enum.new(:flag_threshold_reached, :flag_threshold_reached_again, :new_user_spam_threshold_reached) + @hidden_reasons ||= Enum.new( + :flag_threshold_reached, + :flag_threshold_reached_again, + :new_user_spam_threshold_reached, + :flagged_by_tl3_user + ) end def self.types diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 3eb3160a7..3e93c59f3 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -372,7 +372,7 @@ class PostAction < ActiveRecord::Base def enforce_rules post = Post.with_deleted.where(id: post_id).first - PostAction.auto_hide_if_needed(post, post_action_type_key) + PostAction.auto_hide_if_needed(user, post, post_action_type_key) SpamRulesEnforcer.enforce!(post.user) if post_action_type_key == :spam end @@ -382,11 +382,17 @@ class PostAction < ActiveRecord::Base end end - def self.auto_hide_if_needed(post, post_action_type) + def self.auto_hide_if_needed(acting_user, post, post_action_type) return if post.hidden - if PostActionType.auto_action_flag_types.include?(post_action_type) && - SiteSetting.flags_required_to_hide_post > 0 + if post_action_type == :spam && + acting_user.trust_level == TrustLevel[3] && + post.user.trust_level == TrustLevel[0] + + hide_post!(post, post_action_type, Post.hidden_reasons[:flagged_by_tl3_user]) + + elsif PostActionType.auto_action_flag_types.include?(post_action_type) && + SiteSetting.flags_required_to_hide_post > 0 old_flags, new_flags = PostAction.flag_counts_for(post.id) diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 0e5136fc3..811694349 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -326,6 +326,21 @@ describe PostAction do post.topic.visible.should == false end + it "hide tl0 posts that are flagged as spam by a tl3 user" do + newuser = Fabricate(:newuser) + post = create_post(user: newuser) + + Discourse.stubs(:site_contact_user).returns(admin) + + PostAction.act(Fabricate(:leader), post, PostActionType.types[:spam]) + + post.reload + + post.hidden.should == true + post.hidden_at.should be_present + post.hidden_reason_id.should == Post.hidden_reasons[:flagged_by_tl3_user] + end + it "can flag the topic instead of a post" do post1 = create_post post2 = create_post(topic: post1.topic)