2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class Reviewable < ActiveRecord::Base
|
|
|
|
class UpdateConflict < StandardError; end
|
|
|
|
|
|
|
|
class InvalidAction < StandardError
|
|
|
|
def initialize(action_id, klass)
|
|
|
|
@action_id, @klass = action_id, klass
|
2020-12-02 08:38:59 -05:00
|
|
|
super("Can't perform `#{action_id}` on #{klass.name}")
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-17 17:12:32 -04:00
|
|
|
before_save :apply_review_group
|
2019-04-23 15:53:37 -04:00
|
|
|
attr_accessor :created_new
|
2019-01-03 12:03:01 -05: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-05-31 14:59:26 -04:00
|
|
|
has_many :reviewable_scores, -> { order(created_at: :desc) }
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
after_create do
|
2019-04-04 16:07:34 -04:00
|
|
|
log_history(:created, created_by)
|
|
|
|
end
|
|
|
|
|
2019-04-04 16:59:56 -04:00
|
|
|
after_commit(on: :create) do
|
2019-01-03 12:03:01 -05:00
|
|
|
DiscourseEvent.trigger(:reviewable_created, self)
|
2019-08-08 10:04:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after_commit(on: [:create, :update]) do
|
2019-01-03 12:03:01 -05:00
|
|
|
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if pending?
|
|
|
|
end
|
|
|
|
|
2019-08-01 11:23:23 -04:00
|
|
|
# Can be used if several actions are equivalent
|
|
|
|
def self.action_aliases
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
# The gaps are in case we want more precision in the future
|
2019-05-22 17:23:45 -04:00
|
|
|
def self.priorities
|
|
|
|
@priorities ||= Enum.new(
|
|
|
|
low: 0,
|
|
|
|
medium: 5,
|
|
|
|
high: 10
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-05-24 14:13:03 -04: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-03 12:03:01 -05:00
|
|
|
def self.statuses
|
|
|
|
@statuses ||= Enum.new(
|
|
|
|
pending: 0,
|
|
|
|
approved: 1,
|
|
|
|
rejected: 2,
|
|
|
|
ignored: 3,
|
|
|
|
deleted: 4
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-09-19 13:17:00 -04:00
|
|
|
# This number comes from looking at forums in the wild and what numbers work.
|
|
|
|
# As the site accumulates real data it'll be based on the site activity instead.
|
|
|
|
def self.typical_sensitivity
|
|
|
|
12.5
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
# 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-07 13:25:11 -04:00
|
|
|
where("score >= ?", min_score_for_priority)
|
2019-01-03 12:03:01 -05: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-11-22 14:33:10 -05:00
|
|
|
def self.custom_filters
|
|
|
|
@reviewable_filters ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_custom_filter(new_filter)
|
|
|
|
custom_filters << new_filter
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_custom_filters!
|
|
|
|
@reviewable_filters = []
|
|
|
|
end
|
|
|
|
|
2019-05-01 14:40:38 -04: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-03 12:03:01 -05: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-17 17:12:32 -04:00
|
|
|
reviewable = new(
|
2019-01-03 12:03:01 -05:00
|
|
|
target: target,
|
|
|
|
topic: topic,
|
|
|
|
created_by: created_by,
|
2020-03-20 11:28:09 -04:00
|
|
|
reviewable_by_moderator: reviewable_by_moderator,
|
2019-01-03 12:03:01 -05:00
|
|
|
payload: payload,
|
|
|
|
potential_spam: potential_spam
|
|
|
|
)
|
2019-05-01 14:40:38 -04:00
|
|
|
reviewable.created_new!
|
2019-04-01 17:07:47 -04:00
|
|
|
|
2020-01-09 12:02:41 -05:00
|
|
|
if target.blank?
|
|
|
|
# If there is no target there's no chance of a conflict
|
|
|
|
reviewable.save!
|
|
|
|
else
|
|
|
|
# In this case, a reviewable might already exist for this (type, target_id) index.
|
|
|
|
# ActiveRecord can only validate indexes using a SELECT before the INSERT which
|
|
|
|
# is not safe under concurrency. Instead, we perform an UPDATE on the status, and return
|
|
|
|
# the previous value. We then know:
|
|
|
|
#
|
|
|
|
# a) if a previous row existed
|
|
|
|
# b) if it was changed
|
|
|
|
#
|
|
|
|
# And that allows us to complete our logic.
|
|
|
|
|
|
|
|
update_args = {
|
|
|
|
status: statuses[:pending],
|
|
|
|
id: target.id,
|
|
|
|
type: target.class.name,
|
|
|
|
potential_spam: potential_spam == true ? true : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
row = DB.query_single(<<~SQL, update_args)
|
|
|
|
UPDATE reviewables
|
|
|
|
SET status = :status,
|
|
|
|
potential_spam = COALESCE(:potential_spam, reviewables.potential_spam)
|
|
|
|
FROM reviewables AS old_reviewables
|
|
|
|
WHERE reviewables.target_id = :id
|
|
|
|
AND reviewables.target_type = :type
|
|
|
|
RETURNING old_reviewables.status
|
|
|
|
SQL
|
|
|
|
old_status = row[0]
|
|
|
|
|
|
|
|
if old_status.blank?
|
|
|
|
reviewable.save!
|
|
|
|
else
|
|
|
|
reviewable = find_by(target: target)
|
|
|
|
reviewable.log_history(:transitioned, created_by) if old_status != statuses[:pending]
|
|
|
|
end
|
|
|
|
end
|
2019-04-01 17:07:47 -04:00
|
|
|
|
|
|
|
reviewable
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_score(
|
|
|
|
user,
|
|
|
|
reviewable_score_type,
|
2019-04-11 11:11:35 -04:00
|
|
|
reason: nil,
|
2019-01-03 12:03:01 -05: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
|
2019-10-08 09:49:07 -04:00
|
|
|
user_accuracy_bonus = ReviewableScore.user_accuracy_bonus(user)
|
2019-10-11 12:07:19 -04:00
|
|
|
sub_total = ReviewableScore.calculate_score(user, type_bonus, take_action_bonus)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-11 11:11:35 -04:00
|
|
|
rs = reviewable_scores.new(
|
2019-01-03 12:03:01 -05:00
|
|
|
user: user,
|
|
|
|
status: ReviewableScore.statuses[:pending],
|
|
|
|
reviewable_score_type: reviewable_score_type,
|
|
|
|
score: sub_total,
|
2019-10-08 09:49:07 -04:00
|
|
|
user_accuracy_bonus: user_accuracy_bonus,
|
2019-01-03 12:03:01 -05:00
|
|
|
meta_topic_id: meta_topic_id,
|
|
|
|
take_action_bonus: take_action_bonus,
|
|
|
|
created_at: created_at || Time.zone.now
|
|
|
|
)
|
2019-04-11 11:11:35 -04:00
|
|
|
rs.reason = reason.to_s if reason
|
|
|
|
rs.save!
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2020-11-13 06:19:01 -05:00
|
|
|
update(score: self.score + rs.score, latest_score: rs.created_at, force_review: force_review)
|
2019-01-03 12:03:01 -05:00
|
|
|
topic.update(reviewable_score: topic.reviewable_score + rs.score) if topic
|
|
|
|
|
|
|
|
rs
|
|
|
|
end
|
|
|
|
|
2019-05-22 17:23:45 -04: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-07 13:25:11 -04:00
|
|
|
end
|
|
|
|
|
2019-08-06 15:34:39 -04:00
|
|
|
def self.sensitivity_score_value(sensitivity, scale)
|
2019-05-24 14:13:03 -04:00
|
|
|
return Float::MAX if sensitivity == 0
|
|
|
|
|
|
|
|
ratio = sensitivity / Reviewable.sensitivity[:low].to_f
|
2019-09-19 13:17:00 -04:00
|
|
|
high = (
|
|
|
|
PluginStore.get('reviewables', "priority_#{Reviewable.priorities[:high]}") ||
|
|
|
|
typical_sensitivity
|
|
|
|
).to_f
|
2019-05-24 14:13:03 -04:00
|
|
|
|
|
|
|
# We want this to be hard to reach
|
2019-09-19 13:17:00 -04:00
|
|
|
((high.to_f * ratio) * scale).truncate(2)
|
2019-05-24 14:13:03 -04:00
|
|
|
end
|
|
|
|
|
2019-08-06 15:34:39 -04:00
|
|
|
def self.sensitivity_score(sensitivity, scale: 1.0)
|
|
|
|
# If the score is less than the default visibility, bring it up to that level.
|
|
|
|
# Otherwise we have the confusing situation where a post might be hidden and
|
|
|
|
# moderators would never see it!
|
|
|
|
[sensitivity_score_value(sensitivity, scale), min_score_for_priority].max
|
|
|
|
end
|
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
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-07 13:25:11 -04:00
|
|
|
def self.min_score_for_priority(priority = nil)
|
|
|
|
priority ||= SiteSetting.reviewable_default_visibility
|
2019-05-22 17:23:45 -04:00
|
|
|
id = Reviewable.priorities[priority.to_sym]
|
|
|
|
return 0.0 if id.nil?
|
2019-11-14 15:10:51 -05:00
|
|
|
PluginStore.get('reviewables', "priority_#{id}").to_f
|
2019-05-07 13:25:11 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05: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-17 17:12:32 -04:00
|
|
|
def apply_review_group
|
2020-07-14 12:36:19 -04:00
|
|
|
return unless SiteSetting.enable_category_group_moderation? &&
|
2019-05-08 10:20:51 -04:00
|
|
|
category.present? &&
|
|
|
|
category.reviewable_by_group_id
|
|
|
|
|
2019-04-17 17:12:32 -04:00
|
|
|
self.reviewable_by_group_id = category.reviewable_by_group_id
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def actions_for(guardian, args = nil)
|
|
|
|
args ||= {}
|
2019-05-08 10:20:51 -04:00
|
|
|
|
|
|
|
Actions.new(self, guardian).tap do |actions|
|
|
|
|
build_actions(actions, guardian, args)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def editable_for(guardian, args = nil)
|
|
|
|
args ||= {}
|
2019-05-08 10:20:51 -04:00
|
|
|
EditableFields.new(self, guardian, args).tap do |fields|
|
|
|
|
build_editable_fields(fields, guardian, args)
|
|
|
|
end
|
2019-01-03 12:03:01 -05: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 11:23:23 -04: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-03 12:03:01 -05:00
|
|
|
# Ensure the user has access to the action
|
|
|
|
actions = actions_for(Guardian.new(performed_by), args)
|
2019-08-01 11:23:23 -04:00
|
|
|
raise InvalidAction.new(action_id, self.class) unless valid.any? { |a| actions.has?(a) }
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-08-01 11:23:23 -04:00
|
|
|
perform_method = "perform_#{aliases[action_id] || action_id}".to_sym
|
2019-01-03 12:03:01 -05:00
|
|
|
raise InvalidAction.new(action_id, self.class) unless respond_to?(perform_method)
|
|
|
|
|
|
|
|
result = nil
|
2019-04-04 16:07:34 -04:00
|
|
|
update_count = false
|
2019-01-03 12:03:01 -05:00
|
|
|
Reviewable.transaction do
|
|
|
|
increment_version!(args[:version])
|
2019-05-06 21:57:55 -04:00
|
|
|
result = public_send(perform_method, performed_by, args)
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-05-10 09:53:25 -04:00
|
|
|
raise ActiveRecord::Rollback unless result.success?
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-05-10 09:53:25 -04: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-03 12:03:01 -05:00
|
|
|
end
|
2019-04-23 17:29:46 -04:00
|
|
|
if result && result.after_commit
|
|
|
|
result.after_commit.call
|
|
|
|
end
|
2019-04-04 16:07:34 -04:00
|
|
|
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def transition_to(status_symbol, performed_by)
|
|
|
|
was_pending = pending?
|
|
|
|
|
|
|
|
self.status = Reviewable.statuses[status_symbol]
|
|
|
|
save!
|
2019-04-01 17:07:47 -04:00
|
|
|
|
2019-01-03 12:03:01 -05: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-04 16:07:34 -04:00
|
|
|
was_pending
|
2019-01-03 12:03:01 -05: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?
|
|
|
|
|
2020-05-12 19:05:56 -04:00
|
|
|
result = self.order(order || 'reviewables.score desc, reviewables.created_at desc')
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
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?
|
|
|
|
|
2020-07-14 12:36:19 -04:00
|
|
|
group_ids = SiteSetting.enable_category_group_moderation? ? user.group_users.pluck(:group_id) : []
|
2019-04-17 17:12:32 -04:00
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
result.where(
|
2020-05-12 19:05:56 -04:00
|
|
|
'(reviewables.reviewable_by_moderator AND :staff) OR (reviewables.reviewable_by_group_id IN (:group_ids))',
|
2019-01-03 12:03:01 -05:00
|
|
|
staff: user.staff?,
|
2019-04-17 17:12:32 -04:00
|
|
|
group_ids: group_ids
|
2020-05-12 19:05:56 -04:00
|
|
|
).where("reviewables.category_id IS NULL OR reviewables.category_id IN (?)", Guardian.new(user).allowed_category_ids)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.pending_count(user)
|
|
|
|
list_for(user).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.list_for(
|
|
|
|
user,
|
2021-02-26 05:56:14 -05:00
|
|
|
ids: nil,
|
2019-01-03 12:03:01 -05:00
|
|
|
status: :pending,
|
|
|
|
category_id: nil,
|
|
|
|
topic_id: nil,
|
|
|
|
type: nil,
|
|
|
|
limit: nil,
|
|
|
|
offset: nil,
|
2019-05-07 13:25:11 -04:00
|
|
|
priority: nil,
|
2019-06-05 13:19:57 -04:00
|
|
|
username: nil,
|
2020-12-04 12:09:05 -05:00
|
|
|
reviewed_by: nil,
|
2019-11-15 13:29:59 -05:00
|
|
|
sort_order: nil,
|
|
|
|
from_date: nil,
|
2019-11-22 14:33:10 -05:00
|
|
|
to_date: nil,
|
|
|
|
additional_filters: {}
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
2019-05-07 13:25:11 -04:00
|
|
|
min_score = Reviewable.min_score_for_priority(priority)
|
2019-06-05 13:19:57 -04:00
|
|
|
|
|
|
|
order = case sort_order
|
2020-05-27 11:50:28 -04:00
|
|
|
when 'score_asc'
|
2020-05-12 19:05:56 -04:00
|
|
|
'reviewables.score ASC, reviewables.created_at DESC'
|
2019-06-05 13:19:57 -04:00
|
|
|
when 'created_at'
|
2020-05-12 19:05:56 -04:00
|
|
|
'reviewables.created_at DESC, reviewables.score DESC'
|
2019-06-05 13:19:57 -04:00
|
|
|
when 'created_at_asc'
|
2020-05-12 19:05:56 -04:00
|
|
|
'reviewables.created_at ASC, reviewables.score DESC'
|
2019-06-05 13:19:57 -04:00
|
|
|
else
|
2020-05-12 19:05:56 -04:00
|
|
|
'reviewables.score DESC, reviewables.created_at DESC'
|
2019-06-05 13:19:57 -04:00
|
|
|
end
|
2019-01-03 12:03:01 -05: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)
|
2021-02-26 05:56:14 -05:00
|
|
|
result = result.where(id: ids) if ids
|
2020-05-12 19:05:56 -04:00
|
|
|
result = result.where('reviewables.type = ?', type) if type
|
|
|
|
result = result.where('reviewables.category_id = ?', category_id) if category_id
|
|
|
|
result = result.where('reviewables.topic_id = ?', topic_id) if topic_id
|
|
|
|
result = result.where("reviewables.created_at >= ?", from_date) if from_date
|
|
|
|
result = result.where("reviewables.created_at <= ?", to_date) if to_date
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2020-12-04 12:09:05 -05:00
|
|
|
if reviewed_by
|
|
|
|
reviewed_by_id = User.find_by_username(reviewed_by)&.id
|
|
|
|
return [] if reviewed_by_id.nil?
|
|
|
|
|
|
|
|
result = result.joins(<<~SQL
|
|
|
|
INNER JOIN(
|
|
|
|
SELECT reviewable_id
|
|
|
|
FROM reviewable_histories
|
|
|
|
WHERE reviewable_history_type = #{ReviewableHistory.types[:transitioned]} AND
|
|
|
|
status <> #{Reviewable.statuses[:pending]} AND created_by_id = #{reviewed_by_id}
|
|
|
|
) AS rh ON rh.reviewable_id = reviewables.id
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-13 06:19:01 -05:00
|
|
|
if min_score > 0 && status == :pending
|
|
|
|
result = result.where("reviewables.score >= ? OR reviewables.force_review", min_score)
|
2020-02-11 13:29:22 -05:00
|
|
|
elsif min_score > 0
|
2020-05-12 19:05:56 -04:00
|
|
|
result = result.where("reviewables.score >= ?", min_score)
|
2020-02-11 13:29:22 -05:00
|
|
|
end
|
|
|
|
|
2019-11-22 14:33:10 -05:00
|
|
|
if !custom_filters.empty?
|
|
|
|
result = custom_filters.reduce(result) do |memo, filter|
|
|
|
|
key = filter.first
|
|
|
|
filter_query = filter.last
|
|
|
|
|
|
|
|
next(memo) unless additional_filters[key]
|
|
|
|
filter_query.call(result, additional_filters[key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
# If a reviewable doesn't have a target, allow us to filter on who created that reviewable.
|
|
|
|
if user_id
|
|
|
|
result = result.where(
|
2020-05-12 19:05:56 -04:00
|
|
|
"(reviewables.target_created_by_id IS NULL AND reviewables.created_by_id = :user_id)
|
|
|
|
OR (reviewables.target_created_by_id = :user_id)",
|
2019-01-03 12:03:01 -05:00
|
|
|
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
|
|
|
|
|
2020-04-22 04:52:50 -04:00
|
|
|
def self.count_by_date(start_date, end_date, category_id = nil, include_subcategories = false)
|
|
|
|
query = scores_with_topics.where('reviewable_scores.created_at BETWEEN ? AND ?', start_date, end_date)
|
|
|
|
|
|
|
|
if category_id
|
|
|
|
if include_subcategories
|
|
|
|
query = query.where("topics.category_id IN (?)", Category.subcategory_ids(category_id))
|
|
|
|
else
|
|
|
|
query = query.where("topics.category_id = ?", category_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
query
|
2019-01-03 12:03:01 -05:00
|
|
|
.group("date(reviewable_scores.created_at)")
|
|
|
|
.order('date(reviewable_scores.created_at)')
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
2019-09-04 11:56:25 -04:00
|
|
|
def explain_score
|
|
|
|
DB.query(<<~SQL, reviewable_id: id)
|
|
|
|
SELECT rs.reviewable_id,
|
|
|
|
rs.user_id,
|
|
|
|
CASE WHEN (u.admin OR u.moderator) THEN 5.0 ELSE u.trust_level END AS trust_level_bonus,
|
|
|
|
us.flags_agreed,
|
|
|
|
us.flags_disagreed,
|
|
|
|
us.flags_ignored,
|
|
|
|
rs.score,
|
2019-10-08 09:49:07 -04:00
|
|
|
rs.user_accuracy_bonus,
|
2019-09-04 11:56:25 -04:00
|
|
|
rs.take_action_bonus,
|
|
|
|
COALESCE(pat.score_bonus, 0.0) AS type_bonus
|
|
|
|
FROM reviewable_scores AS rs
|
|
|
|
INNER JOIN users AS u ON u.id = rs.user_id
|
|
|
|
LEFT OUTER JOIN user_stats AS us ON us.user_id = rs.user_id
|
|
|
|
LEFT OUTER JOIN post_action_types AS pat ON pat.id = rs.reviewable_score_type
|
|
|
|
WHERE rs.reviewable_id = :reviewable_id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def recalculate_score
|
2019-06-26 11:20:59 -04:00
|
|
|
# pending/agreed scores count
|
|
|
|
sql = <<~SQL
|
2019-01-03 12:03:01 -05:00
|
|
|
UPDATE reviewables
|
|
|
|
SET score = COALESCE((
|
|
|
|
SELECT sum(score)
|
|
|
|
FROM reviewable_scores AS rs
|
|
|
|
WHERE rs.reviewable_id = :id
|
2019-06-26 11:20:59 -04:00
|
|
|
AND rs.status IN (:pending, :agreed)
|
2019-01-03 12:03:01 -05:00
|
|
|
), 0.0)
|
|
|
|
WHERE id = :id
|
|
|
|
RETURNING score
|
|
|
|
SQL
|
|
|
|
|
2019-06-26 11:20:59 -04:00
|
|
|
result = DB.query(
|
|
|
|
sql,
|
|
|
|
id: self.id,
|
|
|
|
pending: ReviewableScore.statuses[:pending],
|
|
|
|
agreed: ReviewableScore.statuses[:agreed]
|
|
|
|
)
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
# Update topic score
|
2019-06-26 11:20:59 -04:00
|
|
|
sql = <<~SQL
|
2019-01-03 12:03:01 -05:00
|
|
|
UPDATE topics
|
|
|
|
SET reviewable_score = COALESCE((
|
|
|
|
SELECT SUM(score)
|
|
|
|
FROM reviewables AS r
|
|
|
|
WHERE r.topic_id = :topic_id
|
2019-06-26 11:20:59 -04:00
|
|
|
AND r.status IN (:pending, :approved)
|
2019-01-03 12:03:01 -05:00
|
|
|
), 0.0)
|
|
|
|
WHERE id = :topic_id
|
|
|
|
SQL
|
|
|
|
|
2019-06-26 11:20:59 -04:00
|
|
|
DB.query(
|
|
|
|
sql,
|
|
|
|
topic_id: topic_id,
|
|
|
|
pending: Reviewable.statuses[:pending],
|
|
|
|
approved: Reviewable.statuses[:approved]
|
|
|
|
)
|
|
|
|
|
2019-01-03 12:03:01 -05: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
|
2019-12-30 12:56:17 -05:00
|
|
|
partial_result.where(status: statuses.except(:pending).values)
|
2019-01-03 12:03:01 -05:00
|
|
|
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 09:53:55 -04: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-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewables
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# id :bigint not null, primary key
|
2019-01-03 12:03:01 -05: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
|
2021-01-14 17:43:26 -05:00
|
|
|
# force_review :boolean default(FALSE), not null
|
|
|
|
# reject_reason :text
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# index_reviewables_on_reviewable_by_group_id (reviewable_by_group_id)
|
2019-04-12 09:55:27 -04: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)
|
2020-09-01 04:00:36 -04:00
|
|
|
# index_reviewables_on_target_id_where_post_type_eq_post (target_id) WHERE ((target_type)::text = 'Post'::text)
|
2019-04-12 09:55:27 -04:00
|
|
|
# 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-03 12:03:01 -05:00
|
|
|
#
|