FIX: 'undo flag' marks the flag as disagreed

This commit is contained in:
Régis Hanol 2014-07-30 23:35:42 +02:00
parent 87ca49e26e
commit 220f9e21e3
4 changed files with 50 additions and 19 deletions

View file

@ -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

View file

@ -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

View file

@ -112,8 +112,7 @@ 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")
else

View file

@ -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