mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Replace all those deleted_ats with acts_as_paranoid
This commit is contained in:
@@ -6,19 +6,20 @@ class PostAction < ActiveRecord::Base
|
|||||||
|
|
||||||
include RateLimiter::OnCreateRecord
|
include RateLimiter::OnCreateRecord
|
||||||
|
|
||||||
attr_accessible :deleted_at, :post_action_type_id, :post_id, :user_id, :post, :user, :post_action_type, :message
|
attr_accessible :post_action_type_id, :post_id, :user_id, :post, :user, :post_action_type, :message
|
||||||
|
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :post_action_type
|
belongs_to :post_action_type
|
||||||
|
|
||||||
|
acts_as_paranoid
|
||||||
|
|
||||||
rate_limit :post_action_rate_limiter
|
rate_limit :post_action_rate_limiter
|
||||||
|
|
||||||
def self.update_flagged_posts_count
|
def self.update_flagged_posts_count
|
||||||
|
|
||||||
posts_flagged_count = PostAction.joins(post: :topic)
|
posts_flagged_count = PostAction.joins(post: :topic)
|
||||||
.where('post_actions.post_action_type_id' => PostActionType.FlagTypes,
|
.where('post_actions.post_action_type_id' => PostActionType.FlagTypes,
|
||||||
'post_actions.deleted_at' => nil,
|
|
||||||
'posts.deleted_at' => nil,
|
'posts.deleted_at' => nil,
|
||||||
'topics.deleted_at' => nil).count('DISTINCT posts.id')
|
'topics.deleted_at' => nil).count('DISTINCT posts.id')
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ class PostAction < ActiveRecord::Base
|
|||||||
|
|
||||||
user_id = user.present? ? user.id : 0
|
user_id = user.present? ? user.id : 0
|
||||||
|
|
||||||
result = PostAction.where(post_id: collection_ids, user_id: user_id, deleted_at: nil)
|
result = PostAction.where(post_id: collection_ids, user_id: user_id)
|
||||||
user_actions = {}
|
user_actions = {}
|
||||||
result.each do |r|
|
result.each do |r|
|
||||||
user_actions[r.post_id] ||= {}
|
user_actions[r.post_id] ||= {}
|
||||||
@@ -58,7 +59,7 @@ class PostAction < ActiveRecord::Base
|
|||||||
moderator_id == -1 ? PostActionType.AutoActionFlagTypes : PostActionType.FlagTypes
|
moderator_id == -1 ? PostActionType.AutoActionFlagTypes : PostActionType.FlagTypes
|
||||||
end
|
end
|
||||||
|
|
||||||
PostAction.update_all({deleted_at: Time.now, deleted_by: moderator_id}, {post_id: post.id, deleted_at: nil, post_action_type_id: actions})
|
PostAction.update_all({deleted_at: Time.now, deleted_by: moderator_id}, {post_id: post.id, post_action_type_id: actions})
|
||||||
|
|
||||||
r = PostActionType.Types.invert
|
r = PostActionType.Types.invert
|
||||||
f = actions.map{|t| ["#{r[t]}_count", 0]}
|
f = actions.map{|t| ["#{r[t]}_count", 0]}
|
||||||
@@ -79,18 +80,10 @@ class PostAction < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.remove_act(user, post, post_action_type_id)
|
def self.remove_act(user, post, post_action_type_id)
|
||||||
if action = self.where(post_id: post.id, user_id: user.id, post_action_type_id: post_action_type_id, deleted_at: nil).first
|
if action = self.where(post_id: post.id, user_id: user.id, post_action_type_id: post_action_type_id).first
|
||||||
|
action.destroy
|
||||||
transaction do
|
action.deleted_at = Time.now
|
||||||
d = DateTime.now
|
action.run_callbacks(:save)
|
||||||
count = PostAction.update_all({deleted_at: d},{id: action.id, deleted_at: nil})
|
|
||||||
|
|
||||||
if(count == 1)
|
|
||||||
action.deleted_at = DateTime.now
|
|
||||||
action.run_callbacks(:save)
|
|
||||||
action.run_callbacks(:destroy)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -123,12 +116,10 @@ class PostAction < ActiveRecord::Base
|
|||||||
before_create do
|
before_create do
|
||||||
raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id,
|
raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id,
|
||||||
post_id: post_id,
|
post_id: post_id,
|
||||||
post_action_type_id: PostActionType.FlagTypes,
|
post_action_type_id: PostActionType.FlagTypes).exists?
|
||||||
deleted_at: nil).exists?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after_save do
|
after_save do
|
||||||
|
|
||||||
# Update denormalized counts
|
# Update denormalized counts
|
||||||
post_action_type = PostActionType.Types.invert[post_action_type_id]
|
post_action_type = PostActionType.Types.invert[post_action_type_id]
|
||||||
column = "#{post_action_type.to_s}_count"
|
column = "#{post_action_type.to_s}_count"
|
||||||
|
|||||||
Reference in New Issue
Block a user