Skip to content

A platform for community discussion. Free, open, simple.

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

Juan David Martínez Cubillos789aa2d9dec4
diff --git a/lib/promotion.rb b/lib/promotion.rb
index 7c22ede7c17..5e300295014 100644
--- a/lib/promotion.rb
+++ b/lib/promotion.rb
@@ -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? || [email protected]_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]
diff --git a/spec/lib/promotion_spec.rb b/spec/lib/promotion_spec.rb
index f31deebe961..990c516a4c6 100644
--- a/spec/lib/promotion_spec.rb
+++ b/spec/lib/promotion_spec.rb
@@ -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