2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'rate_limiter'
|
|
|
|
require_dependency 'system_message'
|
2013-05-07 12:39:01 +08:00
|
|
|
require_dependency 'trashable'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class PostAction < ActiveRecord::Base
|
2013-05-04 08:52:45 +08:00
|
|
|
class AlreadyActed < StandardError; end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
include RateLimiter::OnCreateRecord
|
2013-05-07 12:39:01 +08:00
|
|
|
include Trashable
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post_action_type
|
2013-05-30 04:49:34 +08:00
|
|
|
belongs_to :related_post, class_name: 'Post'
|
2014-02-06 06:54:16 +08:00
|
|
|
belongs_to :target_user, class_name: 'User'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
rate_limit :post_action_rate_limiter
|
|
|
|
|
2013-05-31 23:41:40 +08:00
|
|
|
scope :spam_flags, -> { where(post_action_type_id: PostActionType.types[:spam]) }
|
|
|
|
|
2013-08-15 11:44:30 +08:00
|
|
|
after_save :update_counters
|
|
|
|
after_save :enforce_rules
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.update_flagged_posts_count
|
2013-02-09 04:54:28 +08:00
|
|
|
posts_flagged_count = PostAction.joins(post: :topic)
|
2013-06-20 15:42:15 +08:00
|
|
|
.where('defer = false or defer IS NULL')
|
2013-05-30 04:49:34 +08:00
|
|
|
.where('post_actions.post_action_type_id' => PostActionType.notify_flag_type_ids,
|
2013-02-09 04:54:28 +08:00
|
|
|
'posts.deleted_at' => nil,
|
2013-05-04 08:52:45 +08:00
|
|
|
'topics.deleted_at' => nil)
|
|
|
|
.count('DISTINCT posts.id')
|
2013-02-09 04:54:28 +08:00
|
|
|
|
|
|
|
$redis.set('posts_flagged_count', posts_flagged_count)
|
2013-05-18 03:11:37 +08:00
|
|
|
user_ids = User.staff.pluck(:id)
|
2013-05-02 13:15:17 +08:00
|
|
|
MessageBus.publish('/flagged_counts', { total: posts_flagged_count }, { user_ids: user_ids })
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.flagged_posts_count
|
|
|
|
$redis.get('posts_flagged_count').to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.counts_for(collection, user)
|
|
|
|
return {} if collection.blank?
|
|
|
|
|
|
|
|
collection_ids = collection.map {|p| p.id}
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
user_id = user.present? ? user.id : 0
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-09 05:14:53 +08:00
|
|
|
result = PostAction.where(post_id: collection_ids, user_id: user_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
user_actions = {}
|
|
|
|
result.each do |r|
|
|
|
|
user_actions[r.post_id] ||= {}
|
|
|
|
user_actions[r.post_id][r.post_action_type_id] = r
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
user_actions
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-19 02:27:22 +08:00
|
|
|
def self.count_per_day_for_type(sinceDaysAgo = 30, post_action_type)
|
2013-05-01 00:52:12 +08:00
|
|
|
unscoped.where(post_action_type_id: post_action_type).where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
2013-03-18 01:53:00 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 12:15:48 +08:00
|
|
|
def self.clear_flags!(post, moderator_id, action_type_id = nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
# -1 is the automatic system cleary
|
2013-02-07 12:15:48 +08:00
|
|
|
actions = if action_type_id
|
2013-02-07 23:45:24 +08:00
|
|
|
[action_type_id]
|
2013-02-07 12:15:48 +08:00
|
|
|
else
|
2013-03-01 20:07:44 +08:00
|
|
|
moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values
|
2013-02-07 12:15:48 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-07-10 03:20:18 +08:00
|
|
|
PostAction.where({ post_id: post.id, post_action_type_id: actions }).update_all({ deleted_at: Time.zone.now, deleted_by_id: moderator_id })
|
2013-03-01 20:07:44 +08:00
|
|
|
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post.id).with_deleted.update_all(Hash[*f.flatten])
|
2013-02-06 03:16:51 +08:00
|
|
|
update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-06-20 15:42:15 +08:00
|
|
|
def self.defer_flags!(post, moderator_id)
|
|
|
|
actions = PostAction.where(
|
|
|
|
defer: nil,
|
|
|
|
post_id: post.id,
|
|
|
|
post_action_type_id:
|
|
|
|
PostActionType.flag_types.values,
|
|
|
|
deleted_at: nil
|
|
|
|
)
|
|
|
|
|
|
|
|
actions.each do |a|
|
|
|
|
a.defer = true
|
|
|
|
a.defer_by = moderator_id
|
|
|
|
# so callback is called
|
|
|
|
a.save
|
|
|
|
end
|
|
|
|
|
|
|
|
update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-08-19 19:14:26 +08:00
|
|
|
def self.create_message_for_post_action(user, post, post_action_type_id, opts)
|
|
|
|
post_action_type = PostActionType.types[post_action_type_id]
|
2013-04-12 15:55:45 +08:00
|
|
|
|
2013-08-19 19:14:26 +08:00
|
|
|
return unless opts[:message] && [:notify_moderators, :notify_user].include?(post_action_type)
|
|
|
|
|
2013-09-06 16:03:30 +08:00
|
|
|
# this is a hack to allow a PM with no reciepients, we should think through
|
|
|
|
# a cleaner technique, a PM with myself is valid for flagging
|
|
|
|
target_usernames = post_action_type == :notify_user ? post.user.username : "x"
|
2013-06-01 05:38:28 +08:00
|
|
|
|
2013-08-19 19:14:26 +08:00
|
|
|
title = I18n.t("post_action_types.#{post_action_type}.email_title",
|
|
|
|
title: post.topic.title)
|
|
|
|
body = I18n.t("post_action_types.#{post_action_type}.email_body",
|
|
|
|
message: opts[:message],
|
|
|
|
link: "#{Discourse.base_url}#{post.url}")
|
|
|
|
|
|
|
|
subtype = post_action_type == :notify_moderators ? TopicSubtype.notify_moderators : TopicSubtype.notify_user
|
|
|
|
|
2013-09-06 12:07:23 +08:00
|
|
|
if target_usernames.present?
|
2013-08-19 19:14:26 +08:00
|
|
|
PostCreator.new(user,
|
|
|
|
target_usernames: target_usernames,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
subtype: subtype,
|
|
|
|
title: title,
|
|
|
|
raw: body
|
|
|
|
).create.id
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-19 19:14:26 +08:00
|
|
|
def self.act(user, post, post_action_type_id, opts={})
|
|
|
|
|
|
|
|
related_post_id = create_message_for_post_action(user,post,post_action_type_id,opts)
|
|
|
|
|
|
|
|
create( post_id: post.id,
|
|
|
|
user_id: user.id,
|
|
|
|
post_action_type_id: post_action_type_id,
|
|
|
|
message: opts[:message],
|
|
|
|
staff_took_action: opts[:take_action] || false,
|
2014-02-06 06:54:16 +08:00
|
|
|
related_post_id: related_post_id,
|
|
|
|
targets_topic: !!opts[:flag_topic] )
|
2013-08-19 19:14:26 +08:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# can happen despite being .create
|
|
|
|
# since already bookmarked
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.remove_act(user, post, post_action_type_id)
|
2013-06-26 13:57:22 +08:00
|
|
|
if action = where(post_id: post.id,
|
|
|
|
user_id: user.id,
|
2013-11-06 02:04:47 +08:00
|
|
|
post_action_type_id: post_action_type_id).first
|
2013-08-15 11:44:30 +08:00
|
|
|
action.remove_act!(user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-13 09:48:01 +08:00
|
|
|
def remove_act!(user)
|
2013-07-10 03:20:18 +08:00
|
|
|
trash!(user)
|
2013-08-15 11:54:48 +08:00
|
|
|
run_callbacks(:save)
|
2013-05-13 09:48:01 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def is_bookmark?
|
2013-03-01 20:07:44 +08:00
|
|
|
post_action_type_id == PostActionType.types[:bookmark]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def is_like?
|
2013-03-01 20:07:44 +08:00
|
|
|
post_action_type_id == PostActionType.types[:like]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_flag?
|
2013-03-01 20:07:44 +08:00
|
|
|
PostActionType.flag_types.values.include?(post_action_type_id)
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-12 15:55:45 +08:00
|
|
|
def is_private_message?
|
|
|
|
post_action_type_id == PostActionType.types[:notify_user] ||
|
|
|
|
post_action_type_id == PostActionType.types[:notify_moderators]
|
|
|
|
end
|
2013-05-11 04:58:23 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# A custom rate limiter for this model
|
|
|
|
def post_action_rate_limiter
|
2013-03-01 02:54:12 +08:00
|
|
|
return unless is_flag? || is_bookmark? || is_like?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
return @rate_limiter if @rate_limiter.present?
|
|
|
|
|
|
|
|
%w(like flag bookmark).each do |type|
|
|
|
|
if send("is_#{type}?")
|
2013-02-07 23:45:24 +08:00
|
|
|
@rate_limiter = RateLimiter.new(user, "create_#{type}:#{Date.today.to_s}", SiteSetting.send("max_#{type}s_per_day"), 1.day.to_i)
|
2013-02-06 03:16:51 +08:00
|
|
|
return @rate_limiter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-09 05:55:40 +08:00
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
before_create do
|
2013-05-04 08:52:45 +08:00
|
|
|
post_action_type_ids = is_flag? ? PostActionType.flag_types.values : post_action_type_id
|
|
|
|
raise AlreadyActed if PostAction.where(user_id: user_id,
|
|
|
|
post_id: post_id,
|
|
|
|
post_action_type_id: post_action_type_ids,
|
|
|
|
deleted_at: nil)
|
|
|
|
.exists?
|
2013-02-07 07:45:58 +08:00
|
|
|
end
|
|
|
|
|
2013-05-11 04:58:23 +08:00
|
|
|
# Returns the flag counts for a post, taking into account that some users
|
|
|
|
# can weigh flags differently.
|
|
|
|
def self.flag_counts_for(post_id)
|
|
|
|
flag_counts = exec_sql("SELECT SUM(CASE
|
2013-06-20 15:42:15 +08:00
|
|
|
WHEN pa.deleted_at IS NULL AND (pa.staff_took_action) THEN :flags_required_to_hide_post
|
2013-06-01 05:38:28 +08:00
|
|
|
WHEN pa.deleted_at IS NULL AND (NOT pa.staff_took_action) THEN 1
|
2013-05-11 04:58:23 +08:00
|
|
|
ELSE 0
|
|
|
|
END) AS new_flags,
|
|
|
|
SUM(CASE
|
2013-06-20 15:42:15 +08:00
|
|
|
WHEN pa.deleted_at IS NOT NULL AND (pa.staff_took_action) THEN :flags_required_to_hide_post
|
2013-06-01 05:38:28 +08:00
|
|
|
WHEN pa.deleted_at IS NOT NULL AND (NOT pa.staff_took_action) THEN 1
|
2013-05-11 04:58:23 +08:00
|
|
|
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)",
|
|
|
|
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
|
|
|
|
|
|
|
|
[flag_counts['old_flags'].to_i, flag_counts['new_flags'].to_i]
|
|
|
|
end
|
|
|
|
|
2013-08-15 11:44:30 +08:00
|
|
|
def post_action_type_key
|
|
|
|
PostActionType.types[post_action_type_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def update_counters
|
2013-02-06 03:16:51 +08:00
|
|
|
# Update denormalized counts
|
2013-08-15 11:44:30 +08:00
|
|
|
column = "#{post_action_type_key.to_s}_count"
|
2013-02-06 03:16:51 +08:00
|
|
|
delta = deleted_at.nil? ? 1 : -1
|
|
|
|
|
2013-05-28 00:45:10 +08:00
|
|
|
# We probably want to refactor this method to something cleaner.
|
2013-08-15 11:44:30 +08:00
|
|
|
case post_action_type_key
|
2013-05-28 00:45:10 +08:00
|
|
|
when :vote
|
|
|
|
# Voting also changes the sort_order
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post_id).update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
|
2013-05-28 00:45:10 +08:00
|
|
|
delta: delta,
|
2013-07-02 02:45:52 +08:00
|
|
|
max: Topic.max_sort_order]
|
2013-05-28 00:45:10 +08:00
|
|
|
when :like
|
|
|
|
# `like_score` is weighted higher for staff accounts
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post_id).update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta",
|
2013-05-28 00:45:10 +08:00
|
|
|
delta: delta,
|
2013-07-02 02:45:52 +08:00
|
|
|
score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta]
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post_id).update_all ["#{column} = #{column} + ?", delta]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-28 00:45:10 +08:00
|
|
|
|
2013-07-02 02:45:52 +08:00
|
|
|
Topic.where(id: post.topic_id).update_all ["#{column} = #{column} + ?", delta]
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
|
2013-05-30 04:49:34 +08:00
|
|
|
if PostActionType.notify_flag_type_ids.include?(post_action_type_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
PostAction.update_flagged_posts_count
|
|
|
|
end
|
|
|
|
|
2013-08-15 11:44:30 +08:00
|
|
|
end
|
2013-06-20 15:42:15 +08:00
|
|
|
|
2013-08-15 11:44:30 +08:00
|
|
|
def enforce_rules
|
|
|
|
PostAction.auto_hide_if_needed(post, post_action_type_key)
|
|
|
|
SpamRulesEnforcer.enforce!(post.user) if post_action_type_key == :spam
|
2013-06-20 15:42:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.auto_hide_if_needed(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
|
|
|
|
|
2013-05-11 04:58:23 +08:00
|
|
|
old_flags, new_flags = PostAction.flag_counts_for(post.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
if new_flags >= SiteSetting.flags_required_to_hide_post
|
2013-06-20 15:42:15 +08:00
|
|
|
hide_post!(post, guess_hide_reason(old_flags))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-06-20 15:42:15 +08:00
|
|
|
end
|
2013-05-31 23:41:40 +08:00
|
|
|
|
2013-06-20 15:42:15 +08:00
|
|
|
|
|
|
|
def self.hide_post!(post, reason=nil)
|
|
|
|
return if post.hidden
|
|
|
|
|
|
|
|
unless reason
|
|
|
|
old_flags,_ = PostAction.flag_counts_for(post.id)
|
|
|
|
reason = guess_hide_reason(old_flags)
|
|
|
|
end
|
|
|
|
|
2013-07-02 02:45:52 +08:00
|
|
|
Post.where(id: post.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason])
|
|
|
|
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 })
|
2013-06-20 15:42:15 +08:00
|
|
|
|
|
|
|
# inform user
|
|
|
|
if post.user
|
|
|
|
SystemMessage.create(post.user,
|
|
|
|
:post_hidden,
|
|
|
|
url: post.url,
|
|
|
|
edit_delay: SiteSetting.cooldown_minutes_after_hiding_posts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.guess_hide_reason(old_flags)
|
|
|
|
old_flags > 0 ?
|
|
|
|
Post.hidden_reasons[:flag_threshold_reached_again] :
|
|
|
|
Post.hidden_reasons[:flag_threshold_reached]
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-04-12 15:55:45 +08:00
|
|
|
|
2013-04-15 11:09:52 +08:00
|
|
|
protected
|
2013-04-12 15:55:45 +08:00
|
|
|
|
2013-05-09 15:37:34 +08:00
|
|
|
def self.target_moderators
|
|
|
|
Group[:moderators].name
|
2013-04-12 15:55:45 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_actions
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# post_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# post_action_type_id :integer not null
|
|
|
|
# deleted_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-07-10 03:20:18 +08:00
|
|
|
# deleted_by_id :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
# message :text
|
|
|
|
# related_post_id :integer
|
2013-06-17 08:48:58 +08:00
|
|
|
# staff_took_action :boolean default(FALSE), not null
|
2013-06-20 15:42:15 +08:00
|
|
|
# defer :boolean
|
|
|
|
# defer_by :integer
|
2014-02-06 06:54:16 +08:00
|
|
|
# targets_topic :boolean default(FALSE)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# idx_unique_actions (user_id,post_action_type_id,post_id,deleted_at) UNIQUE
|
|
|
|
# index_post_actions_on_post_id (post_id)
|
|
|
|
#
|
|
|
|
|