2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-05 06:42:18 +08:00
|
|
|
class InvalidTrustLevel < StandardError
|
|
|
|
end
|
2014-11-17 22:02:10 +08:00
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
class TrustLevel
|
2022-08-29 18:00:48 +08:00
|
|
|
def self.[](level)
|
|
|
|
raise InvalidTrustLevel if !valid?(level)
|
2014-11-17 22:02:10 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
level
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.levels
|
|
|
|
@levels ||= Enum.new(:newuser, :basic, :member, :regular, :leader, start: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid?(level)
|
|
|
|
valid_range === level
|
|
|
|
end
|
2014-09-05 06:42:18 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
def self.valid_range
|
|
|
|
(0..4)
|
|
|
|
end
|
2014-09-05 06:42:18 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
def self.compare(current_level, level)
|
|
|
|
(current_level || 0) >= level
|
|
|
|
end
|
2013-02-28 21:08:56 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
def self.name(level)
|
|
|
|
I18n.t("js.trust_levels.names.#{levels[level]}")
|
|
|
|
end
|
2014-09-05 13:20:39 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
def self.calculate(user, use_previous_trust_level: false)
|
|
|
|
# First, use the manual locked level
|
|
|
|
return user.manual_locked_trust_level if user.manual_locked_trust_level.present?
|
2013-02-28 21:08:56 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
# Then consider the group locked level (or the previous trust level)
|
|
|
|
granted_trust_level = user.group_granted_trust_level || 0
|
|
|
|
previous_trust_level = use_previous_trust_level ? find_previous_trust_level(user) : 0
|
2014-11-17 22:02:10 +08:00
|
|
|
|
2023-03-01 12:35:21 +08:00
|
|
|
[granted_trust_level, previous_trust_level, SiteSetting.default_trust_level].max
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-11-17 22:02:10 +08:00
|
|
|
|
2022-08-29 18:00:48 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.find_previous_trust_level(user)
|
|
|
|
UserHistory
|
|
|
|
.where(action: UserHistory.actions[:change_trust_level])
|
|
|
|
.where(target_user_id: user.id)
|
|
|
|
.order(created_at: :desc)
|
2023-02-13 12:39:45 +08:00
|
|
|
.pick(:new_value)
|
2022-08-29 18:00:48 +08:00
|
|
|
.to_i
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|