2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-22 17:09:56 -05:00
|
|
|
# This class performs calculations to determine if a user qualifies for
|
|
|
|
# the Leader (3) trust level.
|
2014-09-05 01:20:39 -04:00
|
|
|
class TrustLevel3Requirements
|
2018-04-24 13:29:15 -04:00
|
|
|
class PenaltyCounts
|
|
|
|
attr_reader :silenced, :suspended
|
|
|
|
|
2019-11-08 05:23:57 -05:00
|
|
|
def initialize(user, row)
|
2018-04-24 13:29:15 -04:00
|
|
|
@silenced = row["silence_count"] || 0
|
|
|
|
@suspended = row["suspend_count"] || 0
|
2019-11-08 05:23:57 -05:00
|
|
|
|
|
|
|
# If penalty started more than 6 months ago and still continues, it will
|
|
|
|
# not be selected by the query from 'penalty_counts'.
|
|
|
|
@silenced += 1 if @silenced == 0 && user.silenced?
|
|
|
|
@suspended += 1 if @suspended == 0 && user.suspended?
|
2018-04-24 13:29:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def total
|
|
|
|
@silenced + @suspended
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-22 17:09:56 -05:00
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2014-07-28 02:42:38 -04:00
|
|
|
LOW_WATER_MARK = 0.9
|
2019-07-19 05:46:10 -04:00
|
|
|
FORGIVENESS_PERIOD = 6.months
|
2014-07-28 02:42:38 -04:00
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
attr_accessor :days_visited,
|
|
|
|
:min_days_visited,
|
2014-01-23 16:40:10 -05:00
|
|
|
:num_topics_replied_to,
|
|
|
|
:min_topics_replied_to,
|
2014-06-26 13:48:07 -04:00
|
|
|
:topics_viewed,
|
|
|
|
:min_topics_viewed,
|
|
|
|
:posts_read,
|
|
|
|
:min_posts_read,
|
2014-07-01 13:34:48 -04:00
|
|
|
:topics_viewed_all_time,
|
|
|
|
:min_topics_viewed_all_time,
|
|
|
|
:posts_read_all_time,
|
|
|
|
:min_posts_read_all_time,
|
2014-08-22 17:33:05 -04:00
|
|
|
:num_flagged_posts,
|
|
|
|
:max_flagged_posts,
|
|
|
|
:num_likes_given,
|
|
|
|
:min_likes_given,
|
2014-09-13 16:55:26 -04:00
|
|
|
:num_likes_received,
|
|
|
|
:min_likes_received,
|
2014-10-07 15:52:54 -04:00
|
|
|
:num_likes_received,
|
|
|
|
:min_likes_received,
|
|
|
|
:num_likes_received_days,
|
|
|
|
:min_likes_received_days,
|
|
|
|
:num_likes_received_users,
|
|
|
|
:min_likes_received_users,
|
2014-09-13 16:55:26 -04:00
|
|
|
:trust_level_locked,
|
|
|
|
:on_grace_period
|
2014-01-22 17:09:56 -05:00
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
2014-06-26 13:48:07 -04:00
|
|
|
def requirements_met?
|
2014-09-13 16:55:26 -04:00
|
|
|
return false if trust_level_locked
|
2018-02-12 17:19:47 -05:00
|
|
|
|
2018-04-24 13:29:15 -04:00
|
|
|
(!@user.suspended?) && (!@user.silenced?) && penalty_counts.total == 0 &&
|
2018-02-12 17:19:47 -05:00
|
|
|
days_visited >= min_days_visited && num_topics_replied_to >= min_topics_replied_to &&
|
|
|
|
topics_viewed >= min_topics_viewed && posts_read >= min_posts_read &&
|
|
|
|
num_flagged_posts <= max_flagged_posts && num_flagged_by_users <= max_flagged_by_users &&
|
|
|
|
topics_viewed_all_time >= min_topics_viewed_all_time &&
|
|
|
|
posts_read_all_time >= min_posts_read_all_time && num_likes_given >= min_likes_given &&
|
|
|
|
num_likes_received >= min_likes_received &&
|
|
|
|
num_likes_received_users >= min_likes_received_users &&
|
|
|
|
num_likes_received_days >= min_likes_received_days
|
2014-07-28 02:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def requirements_lost?
|
2014-09-13 16:55:26 -04:00
|
|
|
return false if trust_level_locked
|
2020-04-16 12:28:16 -04:00
|
|
|
return false if SiteSetting.default_trust_level > 2
|
2018-02-12 17:19:47 -05:00
|
|
|
|
2018-04-24 13:29:15 -04:00
|
|
|
@user.suspended? || @user.silenced? || penalty_counts.total > 0 ||
|
2018-02-12 17:19:47 -05:00
|
|
|
days_visited < min_days_visited * LOW_WATER_MARK ||
|
|
|
|
num_topics_replied_to < min_topics_replied_to * LOW_WATER_MARK ||
|
|
|
|
topics_viewed < min_topics_viewed * LOW_WATER_MARK ||
|
|
|
|
posts_read < min_posts_read * LOW_WATER_MARK || num_flagged_posts > max_flagged_posts ||
|
|
|
|
num_flagged_by_users > max_flagged_by_users ||
|
|
|
|
topics_viewed_all_time < min_topics_viewed_all_time ||
|
|
|
|
posts_read_all_time < min_posts_read_all_time ||
|
|
|
|
num_likes_given < min_likes_given * LOW_WATER_MARK ||
|
|
|
|
num_likes_received < min_likes_received * LOW_WATER_MARK ||
|
|
|
|
num_likes_received_users < min_likes_received_users * LOW_WATER_MARK ||
|
|
|
|
num_likes_received_days < min_likes_received_days * LOW_WATER_MARK
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
|
|
|
|
2015-12-22 11:13:39 -05:00
|
|
|
def time_period
|
|
|
|
SiteSetting.tl3_time_period
|
|
|
|
end
|
|
|
|
|
2014-09-13 16:55:26 -04:00
|
|
|
def trust_level_locked
|
2017-11-23 15:55:44 -05:00
|
|
|
!@user.manual_locked_trust_level.nil?
|
2014-09-13 16:55:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_grace_period
|
2014-09-24 20:19:26 -04:00
|
|
|
@user.on_tl3_grace_period?
|
2014-09-13 16:55:26 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 17:09:56 -05:00
|
|
|
def days_visited
|
2015-12-22 11:13:39 -05:00
|
|
|
@user.user_visits.where("visited_at > ? and posts_read > 0", time_period.days.ago).count
|
2014-01-23 16:40:10 -05:00
|
|
|
end
|
|
|
|
|
2018-04-24 13:29:15 -04:00
|
|
|
def penalty_counts
|
|
|
|
args = {
|
|
|
|
user_id: @user.id,
|
2019-12-20 18:22:53 -05:00
|
|
|
system_user_id: Discourse.system_user.id,
|
2018-04-24 13:29:15 -04:00
|
|
|
silence_user: UserHistory.actions[:silence_user],
|
2019-12-20 18:22:53 -05:00
|
|
|
unsilence_user: UserHistory.actions[:unsilence_user],
|
2018-04-24 13:29:15 -04:00
|
|
|
suspend_user: UserHistory.actions[:suspend_user],
|
2019-12-20 18:22:53 -05:00
|
|
|
unsuspend_user: UserHistory.actions[:unsuspend_user],
|
2019-07-19 05:46:10 -04:00
|
|
|
since: FORGIVENESS_PERIOD.ago,
|
2018-04-24 13:29:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sql = <<~SQL
|
2019-07-19 05:46:10 -04:00
|
|
|
SELECT
|
2019-12-20 18:22:53 -05:00
|
|
|
SUM(
|
|
|
|
CASE
|
|
|
|
WHEN action = :silence_user THEN 1
|
|
|
|
WHEN action = :unsilence_user AND acting_user_id != :system_user_id THEN -1
|
|
|
|
ELSE 0
|
|
|
|
END
|
|
|
|
) AS silence_count,
|
|
|
|
SUM(
|
|
|
|
CASE
|
|
|
|
WHEN action = :suspend_user THEN 1
|
|
|
|
WHEN action = :unsuspend_user AND acting_user_id != :system_user_id THEN -1
|
|
|
|
ELSE 0
|
|
|
|
END
|
|
|
|
) AS suspend_count
|
2018-04-24 13:29:15 -04:00
|
|
|
FROM user_histories AS uh
|
|
|
|
WHERE uh.target_user_id = :user_id
|
2019-12-20 18:22:53 -05:00
|
|
|
AND uh.action IN (:silence_user, :suspend_user, :unsilence_user, :unsuspend_user)
|
2019-07-19 05:46:10 -04:00
|
|
|
AND uh.created_at > :since
|
2018-04-24 13:29:15 -04:00
|
|
|
SQL
|
|
|
|
|
2019-11-08 05:23:57 -05:00
|
|
|
PenaltyCounts.new(@user, DB.query_hash(sql, args).first)
|
2018-04-24 13:29:15 -04:00
|
|
|
end
|
|
|
|
|
2014-01-23 16:40:10 -05:00
|
|
|
def min_days_visited
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_days_visited
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def num_topics_replied_to
|
2020-08-12 10:28:34 -04:00
|
|
|
@user.user_stat.calc_topic_reply_count!(time_period.days.ago)
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-01-23 16:40:10 -05:00
|
|
|
def min_topics_replied_to
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_topics_replied_to
|
2014-01-23 16:40:10 -05:00
|
|
|
end
|
|
|
|
|
2014-07-01 13:34:48 -04:00
|
|
|
def topics_viewed_query
|
2020-04-07 17:32:18 -04:00
|
|
|
TopicViewItem
|
|
|
|
.where(user_id: @user.id)
|
|
|
|
.joins(:topic)
|
|
|
|
.where("topics.archetype <> ?", Archetype.private_message)
|
|
|
|
.select("topic_id")
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
2014-06-26 13:48:07 -04:00
|
|
|
def topics_viewed
|
2015-12-22 11:13:39 -05:00
|
|
|
topics_viewed_query.where("viewed_at > ?", time_period.days.ago).count
|
2014-06-26 13:48:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def min_topics_viewed
|
2016-05-25 14:55:37 -04:00
|
|
|
[
|
|
|
|
(
|
|
|
|
TrustLevel3Requirements.num_topics_in_time_period.to_i *
|
|
|
|
(SiteSetting.tl3_requires_topics_viewed.to_f / 100.0)
|
|
|
|
).round,
|
|
|
|
SiteSetting.tl3_requires_topics_viewed_cap,
|
|
|
|
].min
|
2014-06-26 13:48:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def posts_read
|
2015-12-22 11:13:39 -05:00
|
|
|
@user.user_visits.where("visited_at > ?", time_period.days.ago).pluck(:posts_read).sum
|
2014-06-26 13:48:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def min_posts_read
|
2016-05-25 14:55:37 -04:00
|
|
|
[
|
|
|
|
(
|
|
|
|
TrustLevel3Requirements.num_posts_in_time_period.to_i *
|
|
|
|
(SiteSetting.tl3_requires_posts_read.to_f / 100.0)
|
|
|
|
).round,
|
|
|
|
SiteSetting.tl3_requires_posts_read_cap,
|
|
|
|
].min
|
2014-06-26 13:48:07 -04:00
|
|
|
end
|
|
|
|
|
2014-07-01 13:34:48 -04:00
|
|
|
def topics_viewed_all_time
|
|
|
|
topics_viewed_query.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def min_topics_viewed_all_time
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_topics_viewed_all_time
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def posts_read_all_time
|
|
|
|
@user.user_visits.pluck(:posts_read).sum
|
|
|
|
end
|
|
|
|
|
|
|
|
def min_posts_read_all_time
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_posts_read_all_time
|
2014-07-01 13:34:48 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 17:09:56 -05:00
|
|
|
def num_flagged_posts
|
2014-07-31 10:43:13 -04:00
|
|
|
PostAction
|
|
|
|
.with_deleted
|
|
|
|
.where(post_id: flagged_post_ids)
|
|
|
|
.where.not(user_id: @user.id)
|
|
|
|
.where.not(agreed_at: nil)
|
|
|
|
.pluck(:post_id)
|
|
|
|
.uniq
|
|
|
|
.count
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
2014-01-23 16:40:10 -05:00
|
|
|
|
|
|
|
def max_flagged_posts
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_max_flagged
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def num_flagged_by_users
|
2015-01-19 15:30:16 -05:00
|
|
|
@_num_flagged_by_users ||=
|
|
|
|
PostAction
|
|
|
|
.with_deleted
|
|
|
|
.where(post_id: flagged_post_ids)
|
|
|
|
.where.not(user_id: @user.id)
|
|
|
|
.where.not(agreed_at: nil)
|
|
|
|
.pluck(:user_id)
|
|
|
|
.uniq
|
|
|
|
.count
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_flagged_by_users
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_max_flagged
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
2014-08-22 17:33:05 -04:00
|
|
|
def num_likes_given
|
2020-04-07 17:32:18 -04:00
|
|
|
UserAction
|
|
|
|
.where(user_id: @user.id, action_type: UserAction::LIKE)
|
|
|
|
.where("user_actions.created_at > ?", time_period.days.ago)
|
|
|
|
.joins(:target_topic)
|
|
|
|
.where("topics.archetype <> ?", Archetype.private_message)
|
|
|
|
.count
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def min_likes_given
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_likes_given
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
2014-10-07 15:52:54 -04:00
|
|
|
def num_likes_received_query
|
2020-04-07 17:32:18 -04:00
|
|
|
UserAction
|
|
|
|
.where(user_id: @user.id, action_type: UserAction::WAS_LIKED)
|
|
|
|
.where("user_actions.created_at > ?", time_period.days.ago)
|
|
|
|
.joins(:target_topic)
|
|
|
|
.where("topics.archetype <> ?", Archetype.private_message)
|
2014-10-07 15:52:54 -04:00
|
|
|
end
|
|
|
|
|
2014-08-22 17:33:05 -04:00
|
|
|
def num_likes_received
|
2014-10-07 15:52:54 -04:00
|
|
|
num_likes_received_query.count
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def min_likes_received
|
2014-09-04 16:16:46 -04:00
|
|
|
SiteSetting.tl3_requires_likes_received
|
2014-08-22 17:33:05 -04:00
|
|
|
end
|
|
|
|
|
2014-10-07 15:52:54 -04:00
|
|
|
def num_likes_received_days
|
|
|
|
# don't do a COUNT(DISTINCT date(created_at)) here!
|
2020-04-07 17:32:18 -04:00
|
|
|
num_likes_received_query.pluck("date(user_actions.created_at)").uniq.size
|
2014-10-07 15:52:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def min_likes_received_days
|
2015-12-29 12:50:35 -05:00
|
|
|
# Since min_likes_received / 3 can be greater than the number of days in time_period,
|
|
|
|
# cap this result to be less than time_period.
|
|
|
|
[(min_likes_received.to_f / 3.0).ceil, (0.75 * time_period.to_f).ceil].min
|
2014-10-07 15:52:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def num_likes_received_users
|
|
|
|
# don't do a COUNT(DISTINCT acting_user_id) here!
|
|
|
|
num_likes_received_query.pluck(:acting_user_id).uniq.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def min_likes_received_users
|
|
|
|
(min_likes_received.to_f / 4.0).ceil
|
|
|
|
end
|
|
|
|
|
2014-06-27 12:26:03 -04:00
|
|
|
def self.clear_cache
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del NUM_TOPICS_KEY
|
|
|
|
Discourse.redis.del NUM_POSTS_KEY
|
2014-06-27 12:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
CACHE_DURATION = 1.day.seconds - 60
|
|
|
|
NUM_TOPICS_KEY = "tl3_num_topics"
|
|
|
|
NUM_POSTS_KEY = "tl3_num_posts"
|
|
|
|
|
|
|
|
def self.num_topics_in_time_period
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.get(NUM_TOPICS_KEY) ||
|
|
|
|
begin
|
2015-12-22 11:13:39 -05:00
|
|
|
count =
|
|
|
|
Topic.listable_topics.visible.created_since(SiteSetting.tl3_time_period.days.ago).count
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.setex NUM_TOPICS_KEY, CACHE_DURATION, count
|
2014-06-27 12:26:03 -04:00
|
|
|
count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.num_posts_in_time_period
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.get(NUM_POSTS_KEY) ||
|
|
|
|
begin
|
2015-12-22 11:13:39 -05:00
|
|
|
count = Post.public_posts.visible.created_since(SiteSetting.tl3_time_period.days.ago).count
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.setex NUM_POSTS_KEY, CACHE_DURATION, count
|
2014-06-27 12:26:03 -04:00
|
|
|
count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flagged_post_ids
|
2015-01-19 15:30:16 -05:00
|
|
|
@_flagged_post_ids ||=
|
|
|
|
@user
|
|
|
|
.posts
|
|
|
|
.with_deleted
|
2015-12-22 11:13:39 -05:00
|
|
|
.where(
|
|
|
|
"created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)",
|
|
|
|
time_period.days.ago,
|
|
|
|
)
|
2015-01-19 15:30:16 -05:00
|
|
|
.pluck(:id)
|
2014-01-23 16:40:10 -05:00
|
|
|
end
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|