2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class ReviewableScore < ActiveRecord::Base
|
|
|
|
belongs_to :reviewable
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :reviewed_by, class_name: 'User'
|
|
|
|
belongs_to :meta_topic, class_name: 'Topic'
|
|
|
|
|
|
|
|
# To keep things simple the types correspond to `PostActionType` for backwards
|
|
|
|
# compatibility, but we can add extra reasons for scores.
|
|
|
|
def self.types
|
|
|
|
@types ||= PostActionType.flag_types.merge(
|
|
|
|
needs_approval: 9
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-01-17 09:59:38 -05:00
|
|
|
# When extending post action flags, we need to call this method in order to
|
|
|
|
# get the latests flags.
|
|
|
|
def self.reload_types
|
|
|
|
@types = nil
|
|
|
|
types
|
|
|
|
end
|
|
|
|
|
2020-07-02 10:47:43 -04:00
|
|
|
def self.add_new_types(type_names)
|
|
|
|
next_id = types.values.max + 1
|
|
|
|
|
|
|
|
type_names.each_with_index do |name, idx|
|
|
|
|
@types[name] = next_id + idx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def self.statuses
|
|
|
|
@statuses ||= Enum.new(
|
|
|
|
pending: 0,
|
|
|
|
agreed: 1,
|
|
|
|
disagreed: 2,
|
|
|
|
ignored: 3
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.score_transitions
|
|
|
|
{
|
|
|
|
approved: statuses[:agreed],
|
|
|
|
rejected: statuses[:disagreed],
|
|
|
|
ignored: statuses[:ignored]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Generate `pending?`, `rejected?`, etc helper methods
|
|
|
|
statuses.each do |name, id|
|
|
|
|
define_method("#{name}?") { status == id }
|
2021-12-08 05:03:42 -05:00
|
|
|
singleton_class.define_method(name) { where(status: id) }
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def score_type
|
|
|
|
Reviewable::Collection::Item.new(reviewable_score_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def took_action?
|
|
|
|
take_action_bonus > 0
|
|
|
|
end
|
|
|
|
|
2019-10-11 12:07:19 -04:00
|
|
|
def self.calculate_score(user, type_bonus, take_action_bonus)
|
|
|
|
score = user_flag_score(user) + type_bonus + take_action_bonus
|
|
|
|
score > 0 ? score : 0
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
# A user's flag score is:
|
|
|
|
# 1.0 + trust_level + user_accuracy_bonus
|
|
|
|
# (trust_level is 5 for staff)
|
|
|
|
def self.user_flag_score(user)
|
2019-10-11 12:07:19 -04:00
|
|
|
1.0 + (user.staff? ? 5.0 : user.trust_level.to_f) + user_accuracy_bonus(user)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# A user's accuracy bonus is:
|
|
|
|
# if 5 or less flags => 0.0
|
|
|
|
# if > 5 flags => (agreed flags / total flags) * 5.0
|
|
|
|
def self.user_accuracy_bonus(user)
|
|
|
|
user_stat = user&.user_stat
|
2020-01-02 11:24:24 -05:00
|
|
|
return 0.0 if user_stat.blank? || user.bot?
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-09-17 14:47:56 -04:00
|
|
|
calc_user_accuracy_bonus(user_stat.flags_agreed, user_stat.flags_disagreed)
|
2019-09-04 11:56:25 -04:00
|
|
|
end
|
|
|
|
|
2019-09-17 14:47:56 -04:00
|
|
|
def self.calc_user_accuracy_bonus(agreed, disagreed)
|
2019-09-04 11:56:25 -04:00
|
|
|
agreed ||= 0
|
|
|
|
disagreed ||= 0
|
|
|
|
|
2019-09-17 14:47:56 -04:00
|
|
|
total = (agreed + disagreed).to_f
|
2019-01-03 12:03:01 -05:00
|
|
|
return 0.0 if total <= 5
|
2019-10-04 15:10:56 -04:00
|
|
|
accuracy_axis = 0.7
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-10-04 15:10:56 -04:00
|
|
|
percent_correct = agreed / total
|
|
|
|
positive_accuracy = percent_correct >= accuracy_axis
|
|
|
|
|
|
|
|
bottom = positive_accuracy ? accuracy_axis : 0.0
|
|
|
|
top = positive_accuracy ? 1.0 : accuracy_axis
|
|
|
|
|
|
|
|
absolute_distance = positive_accuracy ?
|
|
|
|
percent_correct - bottom :
|
|
|
|
top - percent_correct
|
|
|
|
|
|
|
|
axis_distance_multiplier = 1.0 / (top - bottom)
|
|
|
|
positivity_multiplier = positive_accuracy ? 1.0 : -1.0
|
|
|
|
|
2019-10-08 09:49:07 -04:00
|
|
|
(absolute_distance * axis_distance_multiplier * positivity_multiplier * (Math.log(total, 4) * 5.0))
|
|
|
|
.round(2)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reviewable_conversation
|
|
|
|
return if meta_topic.blank?
|
|
|
|
Reviewable::Conversation.new(meta_topic)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: reviewable_scores
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# id :bigint not null, primary key
|
2019-01-03 12:03:01 -05:00
|
|
|
# reviewable_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# reviewable_score_type :integer not null
|
|
|
|
# status :integer not null
|
|
|
|
# score :float default(0.0), not null
|
|
|
|
# take_action_bonus :float default(0.0), not null
|
|
|
|
# reviewed_by_id :integer
|
|
|
|
# reviewed_at :datetime
|
|
|
|
# meta_topic_id :integer
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-04-12 09:55:27 -04:00
|
|
|
# reason :string
|
2019-10-17 01:57:53 -04:00
|
|
|
# user_accuracy_bonus :float default(0.0), not null
|
2019-01-03 12:03:01 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_reviewable_scores_on_reviewable_id (reviewable_id)
|
|
|
|
# index_reviewable_scores_on_user_id (user_id)
|
|
|
|
#
|