DEV: Add PluginRegistry modifiers to #review and #recalculate (#29128)

* DEV: Add PluginRegistry modifiers to #review and #recalculate

* added tests

* changed added registry logic
This commit is contained in:
Juan David Martínez Cubillos 2024-10-16 10:26:10 -05:00 committed by GitHub
parent 6078fb73ea
commit 789aa2d9de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View File

@ -11,6 +11,9 @@ class Promotion
# 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
override = DiscoursePluginRegistry.apply_modifier(:review_trust_level, false, @user)
return override if override
# nil users are never promoted
return false if @user.blank? || !@user.manual_locked_trust_level.nil?
@ -148,6 +151,10 @@ class Promotion
promotion = Promotion.new(user)
override =
DiscoursePluginRegistry.apply_modifier(:recalculate_trust_level, false, user, promotion)
return override if override
promotion.review_tl0 if granted_trust_level < TrustLevel[1]
promotion.review_tl1 if granted_trust_level < TrustLevel[2]
promotion.review_tl2 if granted_trust_level < TrustLevel[3]

View File

@ -16,6 +16,11 @@ RSpec.describe Promotion do
describe "newuser" do
fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0], created_at: 2.days.ago) }
let(:promotion) { Promotion.new(user) }
let!(:plugin) { Plugin::Instance.new }
let!(:review_modifier) { :review_trust_level }
let!(:recalculate_modifier) { :recalculate_trust_level }
let!(:deny_block) { Proc.new { false } }
let!(:allow_block) { Proc.new { true } }
it "doesn't raise an error with a nil user" do
expect { Promotion.new(nil).review }.not_to raise_error
@ -49,6 +54,33 @@ RSpec.describe Promotion do
it "has upgraded the user to basic" do
expect(user.trust_level).to eq(TrustLevel[1])
end
it "allows plugins to control promotion #review" do
DiscoursePluginRegistry.register_modifier(plugin, :review_trust_level, &deny_block)
action = Promotion.new(user).review
expect(action).to eq(false)
DiscoursePluginRegistry.register_modifier(plugin, review_modifier, &allow_block)
action = Promotion.new(user).review
expect(action).to eq(true)
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, review_modifier, &deny_block)
DiscoursePluginRegistry.unregister_modifier(plugin, review_modifier, &allow_block)
end
it "allows plugins to control promotion #recalculate" do
DiscoursePluginRegistry.register_modifier(plugin, recalculate_modifier, &deny_block)
action = Promotion.recalculate(user)
expect(action).to eq(nil)
DiscoursePluginRegistry.register_modifier(plugin, recalculate_modifier, &allow_block)
action = Promotion.recalculate(user)
expect(action).to eq(true)
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, recalculate_modifier, &deny_block)
DiscoursePluginRegistry.unregister_modifier(plugin, recalculate_modifier, &allow_block)
end
end
context "when user has not done the requisite things" do