2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
require_dependency 'enum'
|
|
|
|
require_dependency 'reviewable/actions'
|
|
|
|
require_dependency 'reviewable/conversation'
|
|
|
|
require_dependency 'reviewable/editable_fields'
|
|
|
|
require_dependency 'reviewable/perform_result'
|
|
|
|
require_dependency 'reviewable_serializer'
|
|
|
|
|
|
|
|
class Reviewable < ActiveRecord::Base
|
|
|
|
class UpdateConflict < StandardError; end
|
|
|
|
|
|
|
|
class InvalidAction < StandardError
|
|
|
|
def initialize(action_id, klass)
|
|
|
|
@action_id, @klass = action_id, klass
|
|
|
|
super("Can't peform `#{action_id}` on #{klass.name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-18 05:12:32 +08:00
|
|
|
before_save :apply_review_group
|
2019-04-24 03:53:37 +08:00
|
|
|
attr_accessor :created_new
|
2019-01-04 01:03:01 +08:00
|
|
|
validates_presence_of :type, :status, :created_by_id
|
|
|
|
belongs_to :target, polymorphic: true
|
|
|
|
belongs_to :created_by, class_name: 'User'
|
|
|
|
belongs_to :target_created_by, class_name: 'User'
|
|
|
|
belongs_to :reviewable_by_group, class_name: 'Group'
|
|
|
|
|
|
|
|
# Optional, for filtering
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :category
|
|
|
|
|
|
|
|
has_many :reviewable_histories
|
2019-06-01 02:59:26 +08:00
|
|
|
has_many :reviewable_scores, -> { order(created_at: :desc) }
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
after_create do
|
2019-04-05 04:07:34 +08:00
|
|
|
log_history(:created, created_by)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:59:56 +08:00
|
|
|
after_commit(on: :create) do
|
2019-01-04 01:03:01 +08:00
|
|
|
DiscourseEvent.trigger(:reviewable_created, self)
|
|
|
|
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if pending?
|
|
|
|
end
|
|
|
|
|
2019-08-01 23:23:23 +08:00
|
|
|
# Can be used if several actions are equivalent
|
|
|
|
def self.action_aliases
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
# The gaps are in case we want more precision in the future
|
2019-05-23 05:23:45 +08:00
|
|
|
def self.priorities
|
|
|
|
@priorities ||= Enum.new(
|
|
|
|
low: 0,
|
|
|
|
medium: 5,
|
|
|
|
high: 10
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
# The gaps are in case we want more precision in the future
|
|
|
|
def self.sensitivity
|
|
|
|
@sensitivity ||= Enum.new(
|
|
|
|
disabled: 0,
|
|
|
|
low: 9,
|
|
|
|
medium: 6,
|
|
|
|
high: 3
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def self.statuses
|
|
|
|
@statuses ||= Enum.new(
|
|
|
|
pending: 0,
|
|
|
|
approved: 1,
|
|
|
|
rejected: 2,
|
|
|
|
ignored: 3,
|
|
|
|
deleted: 4
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate `pending?`, `rejected?`, etc helper methods
|
|
|
|
statuses.each do |name, id|
|
|
|
|
define_method("#{name}?") { status == id }
|
|
|
|
self.class.define_method(name) { where(status: id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_visible
|
2019-05-08 01:25:11 +08:00
|
|
|
where("score >= ?", min_score_for_priority)
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_type?(type)
|
|
|
|
return false unless type =~ /^Reviewable[A-Za-z]+$/
|
|
|
|
type.constantize <= Reviewable
|
|
|
|
rescue NameError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.types
|
|
|
|
%w[ReviewableFlaggedPost ReviewableQueuedPost ReviewableUser]
|
|
|
|
end
|
|
|
|
|
2019-05-02 02:40:38 +08:00
|
|
|
def created_new!
|
|
|
|
self.created_new = true
|
|
|
|
self.topic = target.topic if topic.blank? && target.is_a?(Post)
|
|
|
|
self.target_created_by_id = target.is_a?(Post) ? target.user_id : nil
|
|
|
|
self.category_id = topic.category_id if category_id.blank? && topic.present?
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
# Create a new reviewable, or if the target has already been reviewed return it to the
|
|
|
|
# pending state and re-use it.
|
|
|
|
#
|
|
|
|
# You probably want to call this to create your reviewable rather than `.create`.
|
|
|
|
def self.needs_review!(
|
|
|
|
target: nil,
|
|
|
|
topic: nil,
|
|
|
|
created_by:,
|
|
|
|
payload: nil,
|
|
|
|
reviewable_by_moderator: false,
|
|
|
|
potential_spam: true
|
|
|
|
)
|
2019-04-18 05:12:32 +08:00
|
|
|
reviewable = new(
|
2019-01-04 01:03:01 +08:00
|
|
|
target: target,
|
|
|
|
topic: topic,
|
|
|
|
created_by: created_by,
|
|
|
|
reviewable_by_moderator: reviewable_by_moderator,
|
|
|
|
payload: payload,
|
|
|
|
potential_spam: potential_spam
|
|
|
|
)
|
2019-05-02 02:40:38 +08:00
|
|
|
reviewable.created_new!
|
2019-04-18 05:12:32 +08:00
|
|
|
reviewable.save!
|
2019-04-24 03:53:37 +08:00
|
|
|
reviewable
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2019-04-02 05:07:47 +08:00
|
|
|
|
|
|
|
row_count = DB.exec(<<~SQL, status: statuses[:pending], id: target.id, type: target.class.name)
|
|
|
|
UPDATE reviewables
|
|
|
|
SET status = :status
|
|
|
|
WHERE status <> :status
|
|
|
|
AND target_id = :id
|
|
|
|
AND target_type = :type
|
|
|
|
SQL
|
|
|
|
|
|
|
|
where(target: target).update_all(potential_spam: true) if potential_spam
|
|
|
|
|
|
|
|
reviewable = find_by(target: target)
|
|
|
|
reviewable.log_history(:transitioned, created_by) if row_count > 0
|
|
|
|
reviewable
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_score(
|
|
|
|
user,
|
|
|
|
reviewable_score_type,
|
2019-04-11 23:11:35 +08:00
|
|
|
reason: nil,
|
2019-01-04 01:03:01 +08:00
|
|
|
created_at: nil,
|
|
|
|
take_action: false,
|
|
|
|
meta_topic_id: nil,
|
|
|
|
force_review: false
|
|
|
|
)
|
|
|
|
|
|
|
|
type_bonus = PostActionType.where(id: reviewable_score_type).pluck(:score_bonus)[0] || 0
|
|
|
|
take_action_bonus = take_action ? 5.0 : 0.0
|
|
|
|
sub_total = (ReviewableScore.user_flag_score(user) + type_bonus + take_action_bonus)
|
|
|
|
|
|
|
|
# We can force a reviewable to hit the threshold, for example with queued posts
|
2019-05-08 01:25:11 +08:00
|
|
|
if force_review && sub_total < Reviewable.min_score_for_priority
|
|
|
|
sub_total = Reviewable.min_score_for_priority
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
2019-04-11 23:11:35 +08:00
|
|
|
rs = reviewable_scores.new(
|
2019-01-04 01:03:01 +08:00
|
|
|
user: user,
|
|
|
|
status: ReviewableScore.statuses[:pending],
|
|
|
|
reviewable_score_type: reviewable_score_type,
|
|
|
|
score: sub_total,
|
|
|
|
meta_topic_id: meta_topic_id,
|
|
|
|
take_action_bonus: take_action_bonus,
|
|
|
|
created_at: created_at || Time.zone.now
|
|
|
|
)
|
2019-04-11 23:11:35 +08:00
|
|
|
rs.reason = reason.to_s if reason
|
|
|
|
rs.save!
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
update(score: self.score + rs.score, latest_score: rs.created_at)
|
|
|
|
topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic
|
|
|
|
|
|
|
|
rs
|
|
|
|
end
|
|
|
|
|
2019-05-23 05:23:45 +08:00
|
|
|
def self.set_priorities(values)
|
|
|
|
values.each do |k, v|
|
|
|
|
id = Reviewable.priorities[k]
|
|
|
|
PluginStore.set('reviewables', "priority_#{id}", v) unless id.nil?
|
|
|
|
end
|
2019-05-08 01:25:11 +08:00
|
|
|
end
|
|
|
|
|
2019-05-25 02:13:03 +08:00
|
|
|
def self.sensitivity_score(sensitivity, scale: 1.0)
|
|
|
|
return Float::MAX if sensitivity == 0
|
|
|
|
|
|
|
|
ratio = sensitivity / Reviewable.sensitivity[:low].to_f
|
|
|
|
high = PluginStore.get('reviewables', "priority_#{Reviewable.priorities[:high]}")
|
|
|
|
return (10.0 * scale) if high.nil?
|
|
|
|
|
|
|
|
# We want this to be hard to reach
|
|
|
|
(high.to_f * ratio) * scale
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.score_to_auto_close_topic
|
|
|
|
sensitivity_score(SiteSetting.auto_close_topic_sensitivity, scale: 2.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.spam_score_to_silence_new_user
|
|
|
|
sensitivity_score(SiteSetting.silence_new_user_sensitivity, scale: 0.6)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.score_required_to_hide_post
|
|
|
|
sensitivity_score(SiteSetting.hide_post_sensitivity)
|
|
|
|
end
|
|
|
|
|
2019-05-08 01:25:11 +08:00
|
|
|
def self.min_score_for_priority(priority = nil)
|
|
|
|
priority ||= SiteSetting.reviewable_default_visibility
|
2019-05-23 05:23:45 +08:00
|
|
|
id = Reviewable.priorities[priority.to_sym]
|
|
|
|
return 0.0 if id.nil?
|
|
|
|
return PluginStore.get('reviewables', "priority_#{id}").to_f
|
2019-05-08 01:25:11 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def history
|
|
|
|
reviewable_histories.order(:created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_history(reviewable_history_type, performed_by, edited: nil)
|
|
|
|
reviewable_histories.create!(
|
|
|
|
reviewable_history_type: ReviewableHistory.types[reviewable_history_type],
|
|
|
|
status: status,
|
|
|
|
created_by: performed_by,
|
|
|
|
edited: edited
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-04-18 05:12:32 +08:00
|
|
|
def apply_review_group
|
2019-05-08 22:20:51 +08:00
|
|
|
return unless SiteSetting.enable_category_group_review? &&
|
|
|
|
category.present? &&
|
|
|
|
category.reviewable_by_group_id
|
|
|
|
|
2019-04-18 05:12:32 +08:00
|
|
|
self.reviewable_by_group_id = category.reviewable_by_group_id
|
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
def actions_for(guardian, args = nil)
|
|
|
|
args ||= {}
|
2019-05-08 22:20:51 +08:00
|
|
|
|
|
|
|
Actions.new(self, guardian).tap do |actions|
|
|
|
|
build_actions(actions, guardian, args)
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def editable_for(guardian, args = nil)
|
|
|
|
args ||= {}
|
2019-05-08 22:20:51 +08:00
|
|
|
EditableFields.new(self, guardian, args).tap do |fields|
|
|
|
|
build_editable_fields(fields, guardian, args)
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# subclasses must implement "build_actions" to list the actions they're capable of
|
|
|
|
def build_actions(actions, guardian, args)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# subclasses can implement "build_editable_fields" to list stuff that can be edited
|
|
|
|
def build_editable_fields(actions, guardian, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_fields(params, performed_by, version: nil)
|
|
|
|
return true if params.blank?
|
|
|
|
|
|
|
|
(params[:payload] || {}).each { |k, v| self.payload[k] = v }
|
|
|
|
self.category_id = params[:category_id] if params.has_key?(:category_id)
|
|
|
|
|
|
|
|
result = false
|
|
|
|
|
|
|
|
Reviewable.transaction do
|
|
|
|
increment_version!(version)
|
|
|
|
changes_json = changes.as_json
|
|
|
|
changes_json.delete('version')
|
|
|
|
|
|
|
|
result = save
|
|
|
|
log_history(:edited, performed_by, edited: changes_json) if result
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Delegates to a `perform_#{action_id}` method, which returns a `PerformResult` with
|
|
|
|
# the result of the operation and whether the status of the reviewable changed.
|
|
|
|
def perform(performed_by, action_id, args = nil)
|
|
|
|
args ||= {}
|
|
|
|
|
2019-08-01 23:23:23 +08:00
|
|
|
# Support this action or any aliases
|
|
|
|
aliases = self.class.action_aliases
|
|
|
|
valid = [ action_id, aliases.to_a.select { |k, v| v == action_id }.map(&:first) ].flatten
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
# Ensure the user has access to the action
|
|
|
|
actions = actions_for(Guardian.new(performed_by), args)
|
2019-08-01 23:23:23 +08:00
|
|
|
raise InvalidAction.new(action_id, self.class) unless valid.any? { |a| actions.has?(a) }
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2019-08-01 23:23:23 +08:00
|
|
|
perform_method = "perform_#{aliases[action_id] || action_id}".to_sym
|
2019-01-04 01:03:01 +08:00
|
|
|
raise InvalidAction.new(action_id, self.class) unless respond_to?(perform_method)
|
|
|
|
|
|
|
|
result = nil
|
2019-04-05 04:07:34 +08:00
|
|
|
update_count = false
|
2019-01-04 01:03:01 +08:00
|
|
|
Reviewable.transaction do
|
|
|
|
increment_version!(args[:version])
|
2019-05-07 09:57:55 +08:00
|
|
|
result = public_send(perform_method, performed_by, args)
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2019-05-10 21:53:25 +08:00
|
|
|
raise ActiveRecord::Rollback unless result.success?
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2019-05-10 21:53:25 +08:00
|
|
|
update_count = transition_to(result.transition_to, performed_by) if result.transition_to
|
|
|
|
update_flag_stats(**result.update_flag_stats) if result.update_flag_stats
|
|
|
|
|
|
|
|
recalculate_score if result.recalculate_score
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
2019-04-24 05:29:46 +08:00
|
|
|
if result && result.after_commit
|
|
|
|
result.after_commit.call
|
|
|
|
end
|
2019-04-05 04:07:34 +08:00
|
|
|
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def transition_to(status_symbol, performed_by)
|
|
|
|
was_pending = pending?
|
|
|
|
|
|
|
|
self.status = Reviewable.statuses[status_symbol]
|
|
|
|
save!
|
2019-04-02 05:07:47 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
log_history(:transitioned, performed_by)
|
|
|
|
DiscourseEvent.trigger(:reviewable_transitioned_to, status_symbol, self)
|
|
|
|
|
|
|
|
if score_status = ReviewableScore.score_transitions[status_symbol]
|
|
|
|
reviewable_scores.pending.update_all(
|
|
|
|
status: score_status,
|
|
|
|
reviewed_by_id: performed_by.id,
|
|
|
|
reviewed_at: Time.zone.now
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-04-05 04:07:34 +08:00
|
|
|
was_pending
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_options
|
|
|
|
Discourse.deprecate(
|
|
|
|
"Reviewable#post_options is deprecated. Please use #payload instead.",
|
|
|
|
output_in_test: true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bulk_perform_targets(performed_by, action, type, target_ids, args = nil)
|
|
|
|
args ||= {}
|
|
|
|
viewable_by(performed_by).where(type: type, target_id: target_ids).each do |r|
|
|
|
|
r.perform(performed_by, action, args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.viewable_by(user, order: nil, preload: true)
|
|
|
|
return none unless user.present?
|
|
|
|
|
|
|
|
result = self.order(order || 'score desc, created_at desc')
|
|
|
|
|
|
|
|
if preload
|
|
|
|
result = result.includes(
|
|
|
|
{ created_by: :user_stat },
|
|
|
|
:topic,
|
|
|
|
:target,
|
|
|
|
:target_created_by,
|
|
|
|
:reviewable_histories
|
|
|
|
).includes(reviewable_scores: { user: :user_stat, meta_topic: :posts })
|
|
|
|
end
|
|
|
|
return result if user.admin?
|
|
|
|
|
2019-04-18 05:12:32 +08:00
|
|
|
group_ids = SiteSetting.enable_category_group_review? ? user.group_users.pluck(:group_id) : []
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result.where(
|
|
|
|
'(reviewable_by_moderator AND :staff) OR (reviewable_by_group_id IN (:group_ids))',
|
|
|
|
staff: user.staff?,
|
2019-04-18 05:12:32 +08:00
|
|
|
group_ids: group_ids
|
2019-01-04 01:03:01 +08:00
|
|
|
).where("category_id IS NULL OR category_id IN (?)", Guardian.new(user).allowed_category_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.pending_count(user)
|
|
|
|
list_for(user).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.list_for(
|
|
|
|
user,
|
|
|
|
status: :pending,
|
|
|
|
category_id: nil,
|
|
|
|
topic_id: nil,
|
|
|
|
type: nil,
|
|
|
|
limit: nil,
|
|
|
|
offset: nil,
|
2019-05-08 01:25:11 +08:00
|
|
|
priority: nil,
|
2019-06-06 01:19:57 +08:00
|
|
|
username: nil,
|
|
|
|
sort_order: nil
|
2019-01-04 01:03:01 +08:00
|
|
|
)
|
2019-05-08 01:25:11 +08:00
|
|
|
min_score = Reviewable.min_score_for_priority(priority)
|
2019-06-06 01:19:57 +08:00
|
|
|
|
|
|
|
order = case sort_order
|
|
|
|
when 'priority_asc'
|
|
|
|
'score ASC, created_at DESC'
|
|
|
|
when 'created_at'
|
|
|
|
'created_at DESC, score DESC'
|
|
|
|
when 'created_at_asc'
|
|
|
|
'created_at ASC, score DESC'
|
|
|
|
else
|
|
|
|
'score DESC, created_at DESC'
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
if username.present?
|
|
|
|
user_id = User.find_by_username(username)&.id
|
|
|
|
return [] if user_id.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
return [] if user.blank?
|
|
|
|
result = viewable_by(user, order: order)
|
|
|
|
|
|
|
|
result = by_status(result, status)
|
|
|
|
result = result.where(type: type) if type
|
|
|
|
result = result.where(category_id: category_id) if category_id
|
|
|
|
result = result.where(topic_id: topic_id) if topic_id
|
2019-05-08 01:25:11 +08:00
|
|
|
result = result.where("score >= ?", min_score) if min_score > 0
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
# If a reviewable doesn't have a target, allow us to filter on who created that reviewable.
|
|
|
|
if user_id
|
|
|
|
result = result.where(
|
|
|
|
"(target_created_by_id IS NULL AND created_by_id = :user_id) OR (target_created_by_id = :user_id)",
|
|
|
|
user_id: user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
result = result.limit(limit) if limit
|
|
|
|
result = result.offset(offset) if offset
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def serializer
|
|
|
|
self.class.serializer_for(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_serializer_for(type)
|
|
|
|
"#{type}Serializer".constantize
|
|
|
|
rescue NameError
|
|
|
|
ReviewableSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.serializer_for(reviewable)
|
|
|
|
type = reviewable.type
|
|
|
|
@@serializers ||= {}
|
|
|
|
@@serializers[type] ||= lookup_serializer_for(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_result(status, transition_to = nil)
|
|
|
|
result = PerformResult.new(self, status)
|
|
|
|
result.transition_to = transition_to
|
|
|
|
yield result if block_given?
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.scores_with_topics
|
|
|
|
ReviewableScore.joins(reviewable: :topic).where("reviewables.type = ?", name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.count_by_date(start_date, end_date, category_id = nil)
|
|
|
|
scores_with_topics
|
|
|
|
.where('reviewable_scores.created_at BETWEEN ? AND ?', start_date, end_date)
|
|
|
|
.where("topics.category_id = COALESCE(?, topics.category_id)", category_id)
|
|
|
|
.group("date(reviewable_scores.created_at)")
|
|
|
|
.order('date(reviewable_scores.created_at)')
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def recalculate_score
|
2019-06-26 23:20:59 +08:00
|
|
|
# pending/agreed scores count
|
|
|
|
sql = <<~SQL
|
2019-01-04 01:03:01 +08:00
|
|
|
UPDATE reviewables
|
|
|
|
SET score = COALESCE((
|
|
|
|
SELECT sum(score)
|
|
|
|
FROM reviewable_scores AS rs
|
|
|
|
WHERE rs.reviewable_id = :id
|
2019-06-26 23:20:59 +08:00
|
|
|
AND rs.status IN (:pending, :agreed)
|
2019-01-04 01:03:01 +08:00
|
|
|
), 0.0)
|
|
|
|
WHERE id = :id
|
|
|
|
RETURNING score
|
|
|
|
SQL
|
|
|
|
|
2019-06-26 23:20:59 +08:00
|
|
|
result = DB.query(
|
|
|
|
sql,
|
|
|
|
id: self.id,
|
|
|
|
pending: ReviewableScore.statuses[:pending],
|
|
|
|
agreed: ReviewableScore.statuses[:agreed]
|
|
|
|
)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
# Update topic score
|
2019-06-26 23:20:59 +08:00
|
|
|
sql = <<~SQL
|
2019-01-04 01:03:01 +08:00
|
|
|
UPDATE topics
|
|
|
|
SET reviewable_score = COALESCE((
|
|
|
|
SELECT SUM(score)
|
|
|
|
FROM reviewables AS r
|
|
|
|
WHERE r.topic_id = :topic_id
|
2019-06-26 23:20:59 +08:00
|
|
|
AND r.status IN (:pending, :approved)
|
2019-01-04 01:03:01 +08:00
|
|
|
), 0.0)
|
|
|
|
WHERE id = :topic_id
|
|
|
|
SQL
|
|
|
|
|
2019-06-26 23:20:59 +08:00
|
|
|
DB.query(
|
|
|
|
sql,
|
|
|
|
topic_id: topic_id,
|
|
|
|
pending: Reviewable.statuses[:pending],
|
|
|
|
approved: Reviewable.statuses[:approved]
|
|
|
|
)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
self.score = result[0].score
|
|
|
|
end
|
|
|
|
|
|
|
|
def increment_version!(version = nil)
|
|
|
|
version_result = nil
|
|
|
|
|
|
|
|
if version
|
|
|
|
version_result = DB.query_single(
|
|
|
|
"UPDATE reviewables SET version = version + 1 WHERE id = :id AND version = :version RETURNING version",
|
|
|
|
version: version,
|
|
|
|
id: self.id
|
|
|
|
)
|
|
|
|
else
|
|
|
|
# We didn't supply a version to update safely, so just increase it
|
|
|
|
version_result = DB.query_single(
|
|
|
|
"UPDATE reviewables SET version = version + 1 WHERE id = :id RETURNING version",
|
|
|
|
id: self.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if version_result && version_result[0]
|
|
|
|
self.version = version_result[0]
|
|
|
|
else
|
|
|
|
raise UpdateConflict.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.by_status(partial_result, status)
|
|
|
|
return partial_result if status == :all
|
|
|
|
|
|
|
|
if status == :reviewed
|
|
|
|
partial_result.where(status: [statuses[:approved], statuses[:rejected], statuses[:ignored]])
|
|
|
|
else
|
|
|
|
partial_result.where(status: statuses[status])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def update_flag_stats(status:, user_ids:)
|
|
|
|
return unless [:agreed, :disagreed, :ignored].include?(status)
|
|
|
|
|
|
|
|
# Don't count self-flags
|
|
|
|
user_ids -= [post&.user_id]
|
|
|
|
return if user_ids.blank?
|
|
|
|
|
|
|
|
result = DB.query(<<~SQL, user_ids: user_ids)
|
|
|
|
UPDATE user_stats
|
|
|
|
SET flags_#{status} = flags_#{status} + 1
|
|
|
|
WHERE user_id IN (:user_ids)
|
|
|
|
RETURNING user_id, flags_agreed + flags_disagreed + flags_ignored AS total
|
|
|
|
SQL
|
|
|
|
|
2019-04-01 21:53:55 +08:00
|
|
|
user_ids = result.select { |r| r.total > Jobs::TruncateUserFlagStats.truncate_to }.map(&:user_id)
|
|
|
|
return if user_ids.blank?
|
|
|
|
|
|
|
|
Jobs.enqueue(:truncate_user_flag_stats, user_ids: user_ids)
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewables
|
|
|
|
#
|
2019-05-03 06:34:12 +08:00
|
|
|
# id :bigint not null, primary key
|
2019-01-04 01:03:01 +08:00
|
|
|
# type :string not null
|
|
|
|
# status :integer default(0), not null
|
|
|
|
# created_by_id :integer not null
|
|
|
|
# reviewable_by_moderator :boolean default(FALSE), not null
|
|
|
|
# reviewable_by_group_id :integer
|
|
|
|
# category_id :integer
|
|
|
|
# topic_id :integer
|
|
|
|
# score :float default(0.0), not null
|
|
|
|
# potential_spam :boolean default(FALSE), not null
|
|
|
|
# target_id :integer
|
|
|
|
# target_type :string
|
|
|
|
# target_created_by_id :integer
|
|
|
|
# payload :json
|
|
|
|
# version :integer default(0), not null
|
|
|
|
# latest_score :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-05-03 06:34:12 +08:00
|
|
|
# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)
|
2019-04-12 21:55:27 +08:00
|
|
|
# index_reviewables_on_status_and_created_at (status,created_at)
|
|
|
|
# index_reviewables_on_status_and_score (status,score)
|
|
|
|
# index_reviewables_on_status_and_type (status,type)
|
|
|
|
# index_reviewables_on_topic_id_and_status_and_created_by_id (topic_id,status,created_by_id)
|
|
|
|
# index_reviewables_on_type_and_target_id (type,target_id) UNIQUE
|
2019-01-04 01:03:01 +08:00
|
|
|
#
|