From 220f9e21e374c00e7cf6491a9f77cf39aab80f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 30 Jul 2014 23:35:42 +0200 Subject: [PATCH] FIX: 'undo flag' marks the flag as disagreed --- app/models/post_action.rb | 28 ++++++++++--------- ...d_at_and_disagreed_by_id_to_post_action.rb | 27 ++++++++++++++++++ lib/flag_query.rb | 5 ++-- spec/models/post_action_spec.rb | 9 ++++-- 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20140730203029_add_disagreed_at_and_disagreed_by_id_to_post_action.rb diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 0f09378c5..05ac185a0 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -18,22 +18,22 @@ class PostAction < ActiveRecord::Base scope :spam_flags, -> { where(post_action_type_id: PostActionType.types[:spam]) } scope :flags, -> { where(post_action_type_id: PostActionType.notify_flag_type_ids) } scope :publics, -> { where(post_action_type_id: PostActionType.public_type_ids) } - scope :active, -> { where(defered_at: nil, agreed_at: nil, deleted_at: nil) } + scope :active, -> { where(disagreed_at: nil, defered_at: nil, agreed_at: nil, deleted_at: nil) } after_save :update_counters after_save :enforce_rules after_commit :notify_subscribers def disposed_by_id - deleted_by_id || agreed_by_id || defered_by_id + disagreed_by_id || agreed_by_id || defered_by_id end def disposed_at - deleted_at || agreed_at || defered_at + disagreed_at || agreed_at || defered_at end def disposition - return :disagreed if deleted_at + return :disagreed if disagreed_at return :agreed if agreed_at return :defered if defered_at nil @@ -60,7 +60,7 @@ class PostAction < ActiveRecord::Base return {} if collection.blank? collection_ids = collection.map(&:id) - user_id = user.present? ? user.id : 0 + user_id = user.try(:id) || 0 post_actions = PostAction.where(post_id: collection_ids, user_id: user_id) @@ -107,8 +107,8 @@ class PostAction < ActiveRecord::Base .where(post_action_type_id: action_type_ids) actions.each do |action| - action.deleted_at = Time.zone.now - action.deleted_by_id = moderator.id + action.disagreed_at = Time.zone.now + action.disagreed_by_id = moderator.id # so callback is called action.save action.add_moderator_post_if_needed(moderator, :disagreed) @@ -276,6 +276,7 @@ class PostAction < ActiveRecord::Base .where(post_id: post_id) .where(post_action_type_id: post_action_type_ids) .where(deleted_at: nil) + .where(disagreed_at: nil) .where(targets_topic: targets_topic) .exists? end @@ -284,19 +285,20 @@ class PostAction < ActiveRecord::Base # can weigh flags differently. def self.flag_counts_for(post_id) flag_counts = exec_sql("SELECT SUM(CASE - WHEN pa.deleted_at IS NULL AND (pa.staff_took_action) THEN :flags_required_to_hide_post - WHEN pa.deleted_at IS NULL AND (NOT pa.staff_took_action) THEN 1 + WHEN pa.disagreed_at IS NULL AND pa.staff_took_action THEN :flags_required_to_hide_post + WHEN pa.disagreed_at IS NULL AND NOT pa.staff_took_action THEN 1 ELSE 0 END) AS new_flags, SUM(CASE - WHEN pa.deleted_at IS NOT NULL AND (pa.staff_took_action) THEN :flags_required_to_hide_post - WHEN pa.deleted_at IS NOT NULL AND (NOT pa.staff_took_action) THEN 1 + WHEN pa.disagreed_at IS NOT NULL AND pa.staff_took_action THEN :flags_required_to_hide_post + WHEN pa.disagreed_at IS NOT NULL AND NOT pa.staff_took_action THEN 1 ELSE 0 END) AS old_flags FROM post_actions AS pa INNER JOIN users AS u ON u.id = pa.user_id - WHERE pa.post_id = :post_id AND - pa.post_action_type_id IN (:post_action_types)", + WHERE pa.post_id = :post_id + AND pa.post_action_type_id IN (:post_action_types) + AND pa.deleted_at IS NULL", post_id: post_id, post_action_types: PostActionType.auto_action_flag_types.values, flags_required_to_hide_post: SiteSetting.flags_required_to_hide_post).first diff --git a/db/migrate/20140730203029_add_disagreed_at_and_disagreed_by_id_to_post_action.rb b/db/migrate/20140730203029_add_disagreed_at_and_disagreed_by_id_to_post_action.rb new file mode 100644 index 000000000..4b9aefa89 --- /dev/null +++ b/db/migrate/20140730203029_add_disagreed_at_and_disagreed_by_id_to_post_action.rb @@ -0,0 +1,27 @@ +class AddDisagreedAtAndDisagreedByIdToPostAction < ActiveRecord::Migration + def up + add_column :post_actions, :disagreed_at, :datetime + add_column :post_actions, :disagreed_by_id, :integer + + execute <<-SQL + UPDATE post_actions + SET disagreed_at = deleted_at, + disagreed_by_id = deleted_by_id, + deleted_at = NULL, + deleted_by_id = NULL + WHERE deleted_by_id != user_id + SQL + end + + def down + execute <<-SQL + UPDATE post_actions + SET deleted_at = disagreed_at, + deleted_by_id = disagreed_by_id + WHERE disagreed_by_id != user_id + SQL + + remove_column :post_actions, :disagreed_at + remove_column :post_actions, :disagreed_by_id + end +end diff --git a/lib/flag_query.rb b/lib/flag_query.rb index 76809c652..4b47103ad 100644 --- a/lib/flag_query.rb +++ b/lib/flag_query.rb @@ -112,10 +112,9 @@ module FlagQuery .joins("INNER JOIN topics ON topics.id = posts.topic_id") if filter == "old" - post_actions.with_deleted - .where("post_actions.deleted_at IS NOT NULL OR + post_actions.where("post_actions.disagreed_at IS NOT NULL OR post_actions.defered_at IS NOT NULL OR - post_actions.agreed_at IS NOT NULL") + post_actions.agreed_at IS NOT NULL") else post_actions.active .where("posts.deleted_at" => nil) diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index a7aacdd53..19209ec50 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -281,12 +281,13 @@ describe PostAction do post.reload - post.hidden.should.should be_true - post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached] + post.hidden.should be_true post.hidden_at.should be_present + post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached] post.topic.visible.should be_false post.revise(post.user, post.raw + " ha I edited it ") + post.reload post.hidden.should be_false @@ -300,8 +301,9 @@ describe PostAction do post.reload post.hidden.should be_true + post.hidden_at.should be_present post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached_again] - post.hidden_at.should be_true + post.topic.visible.should be_false post.revise(post.user, post.raw + " ha I edited it again ") @@ -310,6 +312,7 @@ describe PostAction do post.hidden.should be_true post.hidden_at.should be_true post.hidden_reason_id.should == Post.hidden_reasons[:flag_threshold_reached_again] + post.topic.visible.should be_false end it "can flag the topic instead of a post" do