Class: TrustLevel3Requirements
- Inherits:
-
Object
- Object
- TrustLevel3Requirements
- Includes:
- ActiveModel::Serialization
- Defined in:
- app/models/trust_level3_requirements.rb
Overview
This class performs calculations to determine if a user qualifies for the Leader (3) trust level.
Defined Under Namespace
Classes: PenaltyCounts
Constant Summary collapse
- LOW_WATER_MARK =
0.9
- FORGIVENESS_PERIOD =
6.months
- CACHE_DURATION =
1.day.seconds - 60
- NUM_TOPICS_KEY =
"tl3_num_topics"
- NUM_POSTS_KEY =
"tl3_num_posts"
Instance Attribute Summary collapse
-
#days_visited ⇒ Object
Returns the value of attribute days_visited.
-
#max_flagged_posts ⇒ Object
Returns the value of attribute max_flagged_posts.
-
#min_days_visited ⇒ Object
Returns the value of attribute min_days_visited.
-
#min_likes_given ⇒ Object
Returns the value of attribute min_likes_given.
-
#min_likes_received ⇒ Object
Returns the value of attribute min_likes_received.
-
#min_likes_received_days ⇒ Object
Returns the value of attribute min_likes_received_days.
-
#min_likes_received_users ⇒ Object
Returns the value of attribute min_likes_received_users.
-
#min_posts_read ⇒ Object
Returns the value of attribute min_posts_read.
-
#min_posts_read_all_time ⇒ Object
Returns the value of attribute min_posts_read_all_time.
-
#min_topics_replied_to ⇒ Object
Returns the value of attribute min_topics_replied_to.
-
#min_topics_viewed ⇒ Object
Returns the value of attribute min_topics_viewed.
-
#min_topics_viewed_all_time ⇒ Object
Returns the value of attribute min_topics_viewed_all_time.
-
#num_flagged_posts ⇒ Object
Returns the value of attribute num_flagged_posts.
-
#num_likes_given ⇒ Object
Returns the value of attribute num_likes_given.
-
#num_likes_received ⇒ Object
Returns the value of attribute num_likes_received.
-
#num_likes_received_days ⇒ Object
Returns the value of attribute num_likes_received_days.
-
#num_likes_received_users ⇒ Object
Returns the value of attribute num_likes_received_users.
-
#num_topics_replied_to ⇒ Object
Returns the value of attribute num_topics_replied_to.
-
#on_grace_period ⇒ Object
Returns the value of attribute on_grace_period.
-
#posts_read ⇒ Object
Returns the value of attribute posts_read.
-
#posts_read_all_time ⇒ Object
Returns the value of attribute posts_read_all_time.
-
#topics_viewed ⇒ Object
Returns the value of attribute topics_viewed.
-
#topics_viewed_all_time ⇒ Object
Returns the value of attribute topics_viewed_all_time.
-
#trust_level_locked ⇒ Object
Returns the value of attribute trust_level_locked.
Class Method Summary collapse
Instance Method Summary collapse
- #flagged_post_ids ⇒ Object
-
#initialize(user) ⇒ TrustLevel3Requirements
constructor
A new instance of TrustLevel3Requirements.
- #max_flagged_by_users ⇒ Object
- #num_flagged_by_users ⇒ Object
- #num_likes_received_query ⇒ Object
- #penalty_counts ⇒ Object
- #requirements_lost? ⇒ Boolean
- #requirements_met? ⇒ Boolean
- #time_period ⇒ Object
- #topics_viewed_query ⇒ Object
Constructor Details
#initialize(user) ⇒ TrustLevel3Requirements
Returns a new instance of TrustLevel3Requirements.
56 57 58 |
# File 'app/models/trust_level3_requirements.rb', line 56 def initialize(user) @user = user end |
Instance Attribute Details
#days_visited ⇒ Object
Returns the value of attribute days_visited.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def days_visited @days_visited end |
#max_flagged_posts ⇒ Object
Returns the value of attribute max_flagged_posts.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def max_flagged_posts @max_flagged_posts end |
#min_days_visited ⇒ Object
Returns the value of attribute min_days_visited.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_days_visited @min_days_visited end |
#min_likes_given ⇒ Object
Returns the value of attribute min_likes_given.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_likes_given @min_likes_given end |
#min_likes_received ⇒ Object
Returns the value of attribute min_likes_received.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_likes_received @min_likes_received end |
#min_likes_received_days ⇒ Object
Returns the value of attribute min_likes_received_days.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_likes_received_days @min_likes_received_days end |
#min_likes_received_users ⇒ Object
Returns the value of attribute min_likes_received_users.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_likes_received_users @min_likes_received_users end |
#min_posts_read ⇒ Object
Returns the value of attribute min_posts_read.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_posts_read @min_posts_read end |
#min_posts_read_all_time ⇒ Object
Returns the value of attribute min_posts_read_all_time.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_posts_read_all_time @min_posts_read_all_time end |
#min_topics_replied_to ⇒ Object
Returns the value of attribute min_topics_replied_to.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_topics_replied_to @min_topics_replied_to end |
#min_topics_viewed ⇒ Object
Returns the value of attribute min_topics_viewed.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_topics_viewed @min_topics_viewed end |
#min_topics_viewed_all_time ⇒ Object
Returns the value of attribute min_topics_viewed_all_time.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def min_topics_viewed_all_time @min_topics_viewed_all_time end |
#num_flagged_posts ⇒ Object
Returns the value of attribute num_flagged_posts.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_flagged_posts @num_flagged_posts end |
#num_likes_given ⇒ Object
Returns the value of attribute num_likes_given.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_likes_given @num_likes_given end |
#num_likes_received ⇒ Object
Returns the value of attribute num_likes_received.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_likes_received @num_likes_received end |
#num_likes_received_days ⇒ Object
Returns the value of attribute num_likes_received_days.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_likes_received_days @num_likes_received_days end |
#num_likes_received_users ⇒ Object
Returns the value of attribute num_likes_received_users.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_likes_received_users @num_likes_received_users end |
#num_topics_replied_to ⇒ Object
Returns the value of attribute num_topics_replied_to.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def num_topics_replied_to @num_topics_replied_to end |
#on_grace_period ⇒ Object
Returns the value of attribute on_grace_period.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def on_grace_period @on_grace_period end |
#posts_read ⇒ Object
Returns the value of attribute posts_read.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def posts_read @posts_read end |
#posts_read_all_time ⇒ Object
Returns the value of attribute posts_read_all_time.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def posts_read_all_time @posts_read_all_time end |
#topics_viewed ⇒ Object
Returns the value of attribute topics_viewed.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def topics_viewed @topics_viewed end |
#topics_viewed_all_time ⇒ Object
Returns the value of attribute topics_viewed_all_time.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def topics_viewed_all_time @topics_viewed_all_time end |
#trust_level_locked ⇒ Object
Returns the value of attribute trust_level_locked.
29 30 31 |
# File 'app/models/trust_level3_requirements.rb', line 29 def trust_level_locked @trust_level_locked end |
Class Method Details
.clear_cache ⇒ Object
288 289 290 291 |
# File 'app/models/trust_level3_requirements.rb', line 288 def self.clear_cache Discourse.redis.del NUM_TOPICS_KEY Discourse.redis.del NUM_POSTS_KEY end |
.num_posts_in_time_period ⇒ Object
307 308 309 310 311 312 313 314 |
# File 'app/models/trust_level3_requirements.rb', line 307 def self.num_posts_in_time_period Discourse.redis.get(NUM_POSTS_KEY) || begin count = Post.public_posts.visible.created_since(SiteSetting.tl3_time_period.days.ago).count Discourse.redis.setex NUM_POSTS_KEY, CACHE_DURATION, count count end end |
.num_topics_in_time_period ⇒ Object
297 298 299 300 301 302 303 304 305 |
# File 'app/models/trust_level3_requirements.rb', line 297 def self.num_topics_in_time_period Discourse.redis.get(NUM_TOPICS_KEY) || begin count = Topic.listable_topics.visible.created_since(SiteSetting.tl3_time_period.days.ago).count Discourse.redis.setex NUM_TOPICS_KEY, CACHE_DURATION, count count end end |
Instance Method Details
#flagged_post_ids ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 |
# File 'app/models/trust_level3_requirements.rb', line 316 def flagged_post_ids @_flagged_post_ids ||= @user .posts .with_deleted .where( "created_at > ? AND (spam_count > 0 OR inappropriate_count > 0)", time_period.days.ago, ) .pluck(:id) end |
#max_flagged_by_users ⇒ Object
235 236 237 |
# File 'app/models/trust_level3_requirements.rb', line 235 def max_flagged_by_users SiteSetting.tl3_requires_max_flagged end |
#num_flagged_by_users ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/trust_level3_requirements.rb', line 223 def num_flagged_by_users @_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 end |
#num_likes_received_query ⇒ Object
252 253 254 255 256 257 258 |
# File 'app/models/trust_level3_requirements.rb', line 252 def num_likes_received_query 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.) end |
#penalty_counts ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/trust_level3_requirements.rb', line 108 def penalty_counts args = { user_id: @user.id, system_user_id: Discourse.system_user.id, silence_user: UserHistory.actions[:silence_user], unsilence_user: UserHistory.actions[:unsilence_user], suspend_user: UserHistory.actions[:suspend_user], unsuspend_user: UserHistory.actions[:unsuspend_user], since: FORGIVENESS_PERIOD.ago, } sql = <<~SQL SELECT 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 FROM user_histories AS uh WHERE uh.target_user_id = :user_id AND uh.action IN (:silence_user, :suspend_user, :unsilence_user, :unsuspend_user) AND uh.created_at > :since SQL PenaltyCounts.new(@user, DB.query_hash(sql, args).first) end |
#requirements_lost? ⇒ Boolean
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/trust_level3_requirements.rb', line 74 def requirements_lost? return false if trust_level_locked return false if SiteSetting.default_trust_level > 2 @user.suspended? || @user.silenced? || penalty_counts.total > 0 || 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 end |
#requirements_met? ⇒ Boolean
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/trust_level3_requirements.rb', line 60 def requirements_met? return false if trust_level_locked (!@user.suspended?) && (!@user.silenced?) && penalty_counts.total == 0 && 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 end |
#time_period ⇒ Object
92 93 94 |
# File 'app/models/trust_level3_requirements.rb', line 92 def time_period SiteSetting.tl3_time_period end |
#topics_viewed_query ⇒ Object
156 157 158 159 160 161 162 |
# File 'app/models/trust_level3_requirements.rb', line 156 def topics_viewed_query TopicViewItem .where(user_id: @user.id) .joins(:topic) .where("topics.archetype <> ?", Archetype.) .select("topic_id") end |