2013-07-03 16:27:40 +08:00
|
|
|
require_dependency 'promotion'
|
|
|
|
|
|
|
|
class BoostTrustLevel
|
|
|
|
|
2013-07-08 17:53:22 +08:00
|
|
|
def initialize(args)
|
|
|
|
@user = args[:user]
|
|
|
|
@level = args[:level].to_i
|
2013-07-03 16:27:40 +08:00
|
|
|
@promotion = Promotion.new(@user)
|
|
|
|
@trust_levels = TrustLevel.levels
|
2013-07-08 17:53:22 +08:00
|
|
|
@logger = args[:logger]
|
2013-07-03 16:27:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def save!
|
2013-08-14 00:04:28 +08:00
|
|
|
previous_level = @user.trust_level
|
2013-07-08 18:51:35 +08:00
|
|
|
success = if @level < @user.trust_level
|
|
|
|
demote!
|
|
|
|
else
|
|
|
|
@user.update_attributes!(trust_level: @level)
|
|
|
|
end
|
2013-08-14 00:04:28 +08:00
|
|
|
@logger.log_trust_level_change(@user, previous_level, @level)
|
2014-05-14 23:31:41 +08:00
|
|
|
BadgeGranter.update_badges(@user, trust_level: @level)
|
2013-07-08 18:51:35 +08:00
|
|
|
success
|
2013-07-03 16:27:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def demote!
|
|
|
|
current_trust_level = @user.trust_level
|
|
|
|
@user.update_attributes!(trust_level: @level)
|
|
|
|
if @promotion.review
|
|
|
|
@user.update_attributes!(trust_level: current_trust_level)
|
|
|
|
raise Discourse::InvalidAccess.new, I18n.t('trust_levels.change_failed_explanation',
|
|
|
|
user_name: @user.name,
|
|
|
|
new_trust_level: trust_level_lookup(@level),
|
|
|
|
current_trust_level: trust_level_lookup(current_trust_level))
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def trust_level_lookup(level)
|
|
|
|
@trust_levels.key(level).id2name
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|