diff --git a/lib/promotion.rb b/lib/promotion.rb index f18b4924c5d..7c22ede7c17 100644 --- a/lib/promotion.rb +++ b/lib/promotion.rb @@ -137,6 +137,9 @@ class Promotion TrustLevel.calculate(user, use_previous_trust_level: use_previous_trust_level) || TrustLevel[0] + granted_trust_level = user.trust_level if granted_trust_level < user.trust_level && + !can_downgrade_trust_level?(user) + # TrustLevel.calculate always returns a value, however we added extra protection just # in case this changes user.update_column(:trust_level, TrustLevel[granted_trust_level]) @@ -153,4 +156,12 @@ class Promotion user.change_trust_level!(TrustLevel[2], log_action_for: performed_by || Discourse.system_user) end end + + def self.can_downgrade_trust_level?(user) + return false if user.trust_level == TrustLevel[1] && tl1_met?(user) + return false if user.trust_level == TrustLevel[2] && tl2_met?(user) + return false if user.trust_level == TrustLevel[3] && tl3_met?(user) + + true + end end diff --git a/spec/models/group_user_spec.rb b/spec/models/group_user_spec.rb index 58f5252532d..05014c38474 100644 --- a/spec/models/group_user_spec.rb +++ b/spec/models/group_user_spec.rb @@ -303,5 +303,27 @@ RSpec.describe GroupUser do # keep in mind that we do not restore tl3, cause reqs can be lost expect(user.reload.trust_level).to eq(2) end + + it "protects user trust level if all requirements are met" do + Promotion.stubs(:tl2_met?).returns(true) + + user = Fabricate(:user) + expect(user.trust_level).to eq(1) + + group.update!(grant_trust_level: 1) + + Promotion.recalculate(user) + expect(user.reload.trust_level).to eq(2) + + group_user = Fabricate(:group_user, group: group, user: user) + expect_not_enqueued_with( + job: :send_system_message, + args: { + user_id: user.id, + message_type: "tl2_promotion_message", + }, + ) { group_user.destroy! } + expect(user.reload.trust_level).to eq(2) + end end end