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
|
2014-01-22 17:09:56 -05:00
|
|
|
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2014-07-28 02:42:38 -04:00
|
|
|
LOW_WATER_MARK = 0.9
|
|
|
|
|
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
|
2014-08-29 12:33:34 -04:00
|
|
|
!@user.suspended? &&
|
2014-06-26 13:48:07 -04:00
|
|
|
days_visited >= min_days_visited &&
|
2014-08-29 12:33:34 -04:00
|
|
|
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 &&
|
2014-10-07 15:52:54 -04:00
|
|
|
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
|
2014-08-29 12:33:34 -04:00
|
|
|
@user.suspended? ||
|
2014-07-28 02:42:38 -04: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 ||
|
2014-08-22 17:33:05 -04:00
|
|
|
posts_read_all_time < min_posts_read_all_time ||
|
|
|
|
num_likes_given < min_likes_given * LOW_WATER_MARK ||
|
2014-10-07 15:52:54 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2015-12-22 11:13:39 -05:00
|
|
|
@user.posts.select('distinct topic_id').where('created_at > ? AND post_number > 1', time_period.days.ago).count
|
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
|
2014-08-04 05:07:55 -04:00
|
|
|
TopicViewItem.where(user_id: @user.id).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
|
2017-07-27 21:20:09 -04: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-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
|
2017-07-27 21:20:09 -04: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-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
|
2015-12-22 11:13:39 -05:00
|
|
|
UserAction.where(user_id: @user.id, action_type: UserAction::LIKE).where('created_at > ?', time_period.days.ago).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
|
2015-12-22 11:13:39 -05:00
|
|
|
UserAction.where(user_id: @user.id, action_type: UserAction::WAS_LIKED).where('created_at > ?', time_period.days.ago)
|
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!
|
|
|
|
num_likes_received_query.pluck('date(created_at)').uniq.size
|
|
|
|
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
|
|
|
|
$redis.del NUM_TOPICS_KEY
|
|
|
|
$redis.del NUM_POSTS_KEY
|
|
|
|
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
|
|
|
|
$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
|
2014-06-27 12:26:03 -04:00
|
|
|
$redis.setex NUM_TOPICS_KEY, CACHE_DURATION, count
|
|
|
|
count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.num_posts_in_time_period
|
|
|
|
$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
|
2014-06-27 12:26:03 -04:00
|
|
|
$redis.setex NUM_POSTS_KEY, CACHE_DURATION, count
|
|
|
|
count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flagged_post_ids
|
2015-01-19 15:30:16 -05:00
|
|
|
@_flagged_post_ids ||= @user.posts
|
2017-07-27 21:20:09 -04:00
|
|
|
.with_deleted
|
|
|
|
.where('created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)', time_period.days.ago)
|
|
|
|
.pluck(:id)
|
2014-01-23 16:40:10 -05:00
|
|
|
end
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|