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