2013-07-03 04:27:40 -04:00
|
|
|
require_dependency 'promotion'
|
|
|
|
|
|
|
|
class BoostTrustLevel
|
|
|
|
|
2013-07-08 05:53:22 -04:00
|
|
|
def initialize(args)
|
|
|
|
@user = args[:user]
|
|
|
|
@level = args[:level].to_i
|
2013-07-03 04:27:40 -04:00
|
|
|
@promotion = Promotion.new(@user)
|
|
|
|
@trust_levels = TrustLevel.levels
|
2013-07-08 05:53:22 -04:00
|
|
|
@logger = args[:logger]
|
2013-07-03 04:27:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save!
|
2013-08-13 12:04:28 -04:00
|
|
|
previous_level = @user.trust_level
|
2013-07-08 06:51:35 -04:00
|
|
|
success = if @level < @user.trust_level
|
|
|
|
demote!
|
|
|
|
else
|
|
|
|
@user.update_attributes!(trust_level: @level)
|
|
|
|
end
|
2013-08-13 12:04:28 -04:00
|
|
|
@logger.log_trust_level_change(@user, previous_level, @level)
|
2014-05-14 11:31:41 -04:00
|
|
|
BadgeGranter.update_badges(@user, trust_level: @level)
|
2013-07-08 06:51:35 -04:00
|
|
|
success
|
2013-07-03 04:27:40 -04: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
|