From 007326d3bd17985604fa3fe0850990d8d5d6ac04 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 29 Dec 2015 16:59:26 -0500 Subject: [PATCH] FIX: hidden posts that are edited by the author and unhidden could not be flagged by the same users again --- app/models/post.rb | 2 +- app/models/post_action.rb | 15 +++++++++------ lib/post_destroyer.rb | 2 +- lib/post_revisor.rb | 8 +++++--- spec/models/post_action_spec.rb | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 7b3a0b3c8..ade5e6525 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -346,7 +346,7 @@ class Post < ActiveRecord::Base end def unhide! - self.update_attributes(hidden: false, hidden_at: nil, hidden_reason_id: nil) + self.update_attributes(hidden: false) self.topic.update_attributes(visible: true) if is_first_post? save(validate: false) publish_change_to_clients!(:acted) diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 7a50f3146..943dbc8eb 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -483,7 +483,7 @@ SQL old_flags, new_flags = PostAction.flag_counts_for(post.id) if new_flags >= SiteSetting.flags_required_to_hide_post - hide_post!(post, post_action_type, guess_hide_reason(old_flags)) + hide_post!(post, post_action_type, guess_hide_reason(post)) end end end @@ -492,11 +492,14 @@ SQL return if post.hidden unless reason - old_flags,_ = PostAction.flag_counts_for(post.id) - reason = guess_hide_reason(old_flags) + reason = guess_hide_reason(post) end - Post.where(id: post.id).update_all(["hidden = true, hidden_at = ?, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Time.now, reason]) + post.hidden = true + post.hidden_at = Time.zone.now + post.hidden_reason_id = reason + post.save + Topic.where("id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)", topic_id: post.topic_id).update_all(visible: false) # inform user @@ -510,8 +513,8 @@ SQL end end - def self.guess_hide_reason(old_flags) - old_flags > 0 ? + def self.guess_hide_reason(post) + post.hidden_at ? Post.hidden_reasons[:flag_threshold_reached_again] : Post.hidden_reasons[:flag_threshold_reached] end diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 394b6ccc4..f0b188082 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -5,7 +5,7 @@ class PostDestroyer def self.destroy_old_hidden_posts - Post.where(deleted_at: nil) + Post.where(deleted_at: nil, hidden: true) .where("hidden_at < ?", 30.days.ago) .find_each do |post| PostDestroyer.new(Discourse.system_user, post).destroy diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index e404640cc..e08186420 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -232,7 +232,7 @@ class PostRevisor @post.word_count = @fields[:raw].scan(/\w+/).size if @fields.has_key?(:raw) @post.self_edits += 1 if self_edit? - clear_flags_and_unhide_post + remove_flags_and_unhide_post @post.extract_quoted_post_numbers @@ -269,9 +269,11 @@ class PostRevisor @editor == @post.user end - def clear_flags_and_unhide_post + def remove_flags_and_unhide_post return unless editing_a_flagged_and_hidden_post? - PostAction.clear_flags!(@post, Discourse.system_user) + @post.post_actions.where(post_action_type_id: PostActionType.flag_types.values).each do |action| + action.remove_act!(Discourse.system_user) + end @post.unhide! end diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 752a54cfd..b5f60617a 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -345,8 +345,8 @@ describe PostAction do post.reload expect(post.hidden).to eq(false) - expect(post.hidden_reason_id).to eq(nil) - expect(post.hidden_at).to be_blank + expect(post.hidden_reason_id).to eq(Post.hidden_reasons[:flag_threshold_reached]) # keep most recent reason + expect(post.hidden_at).to be_present # keep the most recent hidden_at time expect(post.topic.visible).to eq(true) PostAction.act(eviltrout, post, PostActionType.types[:spam])