2013-02-06 03:16:51 +08:00
|
|
|
#
|
|
|
|
# Check whether a user is ready for a new trust level.
|
|
|
|
#
|
|
|
|
class Promotion
|
|
|
|
|
|
|
|
def initialize(user)
|
2013-02-26 00:42:20 +08:00
|
|
|
@user = user
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Review a user for a promotion. Delegates work to a review_#{trust_level} method.
|
|
|
|
# Returns true if the user was promoted, false otherwise.
|
|
|
|
def review
|
|
|
|
# nil users are never promoted
|
|
|
|
return false if @user.blank?
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
# Promotion beyond basic requires some expensive queries, so don't do that here.
|
|
|
|
return false if @user.trust_level >= TrustLevel.levels[:regular]
|
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
trust_key = TrustLevel.levels[@user.trust_level]
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
review_method = :"review_#{trust_key.to_s}"
|
|
|
|
return send(review_method) if respond_to?(review_method)
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-04-18 07:11:13 +08:00
|
|
|
def review_newuser
|
2014-06-17 08:46:30 +08:00
|
|
|
Promotion.basic_met?(@user) && change_trust_level!(:basic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def review_basic
|
|
|
|
Promotion.regular_met?(@user) && change_trust_level!(:regular)
|
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
def review_regular
|
|
|
|
Promotion.leader_met?(@user) && change_trust_level!(:leader)
|
|
|
|
end
|
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
def change_trust_level!(level, opts = {})
|
|
|
|
raise "Invalid trust level #{level}" unless TrustLevel.valid_level?(level)
|
|
|
|
|
|
|
|
old_level = @user.trust_level
|
|
|
|
new_level = TrustLevel.levels[level]
|
|
|
|
|
|
|
|
if new_level < old_level
|
|
|
|
next_up = TrustLevel.levels[new_level+1]
|
|
|
|
key = "#{next_up}_met?"
|
|
|
|
if self.class.respond_to?(key) && self.class.send(key, @user)
|
|
|
|
raise Discourse::InvalidAccess.new, I18n.t('trust_levels.change_failed_explanation',
|
|
|
|
user_name: @user.name,
|
|
|
|
new_trust_level: new_level,
|
|
|
|
current_trust_level: old_level)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
admin = opts && opts[:log_action_for]
|
|
|
|
|
|
|
|
@user.trust_level = new_level
|
2014-06-17 08:58:21 +08:00
|
|
|
@user.user_profile.bio_raw_will_change! # So it can get re-cooked based on the new trust level
|
2014-06-17 08:46:30 +08:00
|
|
|
|
|
|
|
@user.transaction do
|
|
|
|
if admin
|
|
|
|
StaffActionLogger.new(admin).log_trust_level_change(@user, old_level, new_level)
|
2014-07-09 05:39:36 +08:00
|
|
|
else
|
|
|
|
UserHistory.create!( action: UserHistory.actions[:auto_trust_level_change],
|
|
|
|
target_user_id: @user.id,
|
|
|
|
previous_value: old_level,
|
|
|
|
new_value: new_level)
|
2014-06-17 08:46:30 +08:00
|
|
|
end
|
|
|
|
@user.save!
|
|
|
|
@user.user_profile.recook_bio
|
|
|
|
@user.user_profile.save!
|
|
|
|
Group.user_trust_level_change!(@user.id, @user.trust_level)
|
2014-07-23 09:42:24 +08:00
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::TrustLevelChange, user: @user)
|
2014-06-17 08:46:30 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
|
|
|
|
def self.regular_met?(user)
|
|
|
|
stat = user.user_stat
|
2013-10-04 11:28:49 +08:00
|
|
|
return false if stat.topics_entered < SiteSetting.regular_requires_topics_entered
|
|
|
|
return false if stat.posts_read_count < SiteSetting.regular_requires_read_posts
|
|
|
|
return false if (stat.time_read / 60) < SiteSetting.regular_requires_time_spent_mins
|
|
|
|
return false if stat.days_visited < SiteSetting.regular_requires_days_visited
|
|
|
|
return false if stat.likes_received < SiteSetting.regular_requires_likes_received
|
|
|
|
return false if stat.likes_given < SiteSetting.regular_requires_likes_given
|
|
|
|
return false if stat.topic_reply_count < SiteSetting.regular_requires_topic_reply_count
|
2013-04-05 12:29:46 +08:00
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.basic_met?(user)
|
|
|
|
stat = user.user_stat
|
|
|
|
return false if stat.topics_entered < SiteSetting.basic_requires_topics_entered
|
|
|
|
return false if stat.posts_read_count < SiteSetting.basic_requires_read_posts
|
|
|
|
return false if (stat.time_read / 60) < SiteSetting.basic_requires_time_spent_mins
|
|
|
|
return true
|
2013-04-05 12:29:46 +08:00
|
|
|
end
|
|
|
|
|
2014-06-28 00:26:03 +08:00
|
|
|
def self.leader_met?(user)
|
|
|
|
LeaderRequirements.new(user).requirements_met?
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|