A platform for community discussion. Free, open, simple.
FIX: Don’t apply callbacks from disabled plugins (#35630)
Loïc Guitaut2a7cb3dc004b
diff --git a/app/models/site.rb b/app/models/site.rbindex c8b5318a496..939de47c2f7 100644--- a/app/models/site.rb+++ b/app/models/site.rb@@ -25,8 +25,8 @@ class Sitecattr_accessor :markdown_additional_optionsself.markdown_additional_options = {}- def self.add_categories_callbacks(&block)- categories_callbacks << block+ def self.add_categories_callbacks(enabled: -> { true }, &block)+ categories_callbacks << { block:, enabled: }enddef self.categories_callbacks@@ -165,7 +165,10 @@ class Sitecategories.reject! { |c| c[:parent_category_id] && !by_id[c[:parent_category_id]] }- self.class.categories_callbacks.each { |callback| callback.call(categories, @guardian) }+ self.class.categories_callbacks.each do |callback|+ next unless callback[:enabled].call+ callback[:block].call(categories, @guardian)+ endcategoriesenddiff --git a/app/models/topic_list.rb b/app/models/topic_list.rbindex 2fa69a348e7..b1c1aac0c98 100644--- a/app/models/topic_list.rb+++ b/app/models/topic_list.rb@@ -21,14 +21,15 @@ class TopicList@preload.each { |preload| preload.call(topics, object) } if @preloadend- def self.on_preload_user_ids(&blk)- (@preload_user_ids ||= Set.new) << blk+ def self.on_preload_user_ids(enabled: -> { true }, &block)+ (@preload_user_ids ||= Set.new) << { block:, enabled: }enddef self.preload_user_ids(topics, user_ids, object)if @preload_user_ids@preload_user_ids.each do |preload_user_ids|- user_ids = preload_user_ids.call(topics, user_ids, object)+ next unless preload_user_ids[:enabled].call+ user_ids = preload_user_ids[:block].call(topics, user_ids, object)endenduser_idsdiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 0acfe07ca23..eab5e2ebf38 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -293,7 +293,7 @@ class Plugin::Instance# scope.where("word_count = 42")# enddef register_custom_filter_by_status(status, &block)- TopicsFilter.add_filter_by_status(status, &block)+ TopicsFilter.add_filter_by_status(status, enabled: method(:enabled?), &block)end# Allows to define custom search order. Example usage:@@ -301,7 +301,7 @@ class Plugin::Instance# posts.reorder("(SELECT LENGTH(raw) FROM posts WHERE posts.topic_id = subquery.topic_id) DESC")# enddef register_search_advanced_order(trigger, &block)- Search.advanced_order(trigger, &block)+ Search.advanced_order(trigger, enabled: method(:enabled?), &block)end# Allows to define custom search filters. Example usage:@@ -309,7 +309,7 @@ class Plugin::Instance# posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)# enddef register_search_advanced_filter(trigger, &block)- Search.advanced_filter(trigger, &block)+ Search.advanced_filter(trigger, enabled: method(:enabled?), &block)end# Allows to define TopicView posts filters. Example usage:@@ -317,7 +317,7 @@ class Plugin::Instance# posts.where(wiki: true)# enddef register_topic_view_posts_filter(trigger, &block)- TopicView.add_custom_filter(trigger, &block)+ TopicView.add_custom_filter(trigger, enabled: method(:enabled?), &block)end# Allows to add more user IDs to the list of preloaded users. This can be@@ -327,7 +327,7 @@ class Plugin::Instance# user_ids << Discourse::SYSTEM_USER_ID# enddef register_topic_list_preload_user_ids(&block)- TopicList.on_preload_user_ids(&block)+ TopicList.on_preload_user_ids(enabled: method(:enabled?), &block)end# Allow to eager load additional tables in Search. Useful to avoid N+1 performance problems.@@ -338,7 +338,7 @@ class Plugin::Instance# OR# register_search_topic_eager_load(%i(example_table))def register_search_topic_eager_load(tables = nil, &block)- Search.custom_topic_eager_load(tables, &block)+ Search.custom_topic_eager_load(tables, enabled: method(:enabled?), &block)end# Request a new size for topic thumbnails@@ -359,7 +359,7 @@ class Plugin::Instance# end# enddef register_site_categories_callback(&block)- Site.add_categories_callbacks(&block)+ Site.add_categories_callbacks(enabled: method(:enabled?), &block)end# Add a category parameter that includes both controller param permission@@ -757,8 +757,7 @@ class Plugin::Instanceenddef register_email_poller(poller)- plugin = self- DiscoursePluginRegistry.register_mail_poller(poller) if plugin.enabled?+ DiscoursePluginRegistry.register_mail_poller(poller) if enabled?enddef register_asset(file, opts = nil)diff --git a/lib/search.rb b/lib/search.rbindex 7210280545d..c5474164713 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -365,24 +365,24 @@ class Search@resultsend- def self.advanced_order(trigger, &block)- advanced_orders[trigger] = block+ def self.advanced_order(trigger, enabled: -> { true }, &block)+ advanced_orders[trigger] = { block:, enabled: }enddef self.advanced_orders@advanced_orders ||= {}end- def self.advanced_filter(trigger, name: nil, &block)- advanced_filters[trigger] = { block:, name: }+ def self.advanced_filter(trigger, name: nil, enabled: -> { true }, &block)+ advanced_filters[trigger] = { block:, name:, enabled: }enddef self.advanced_filters@advanced_filters ||= {}end- def self.custom_topic_eager_load(tables = nil, &block)- (@custom_topic_eager_loads ||= []) << (tables || block)+ def self.custom_topic_eager_load(tables = nil, enabled: -> { true }, &block)+ (@custom_topic_eager_loads ||= []) << { tables:, block:, enabled: }enddef self.custom_topic_eager_loads@@ -882,8 +882,8 @@ class Searchendif @order- advanced_order = Search.advanced_orders&.fetch(@order, nil)- posts = advanced_order.call(posts) if advanced_order+ advanced_order = Search.advanced_orders[@order]+ posts = advanced_order[:block].call(posts) if advanced_order.try(:[], :enabled).try(:call)endposts@@ -936,6 +936,7 @@ class SearchSearch.advanced_filters.each do |matcher, options|block = options[:block]name = options[:name]+ next unless options[:enabled].callcase_insensitive_matcher =Regexp.new(matcher.source, matcher.options | Regexp::IGNORECASE)@@ -1541,8 +1542,9 @@ class Searchtopic_eager_loads << :tags if SiteSetting.tagging_enabledSearch.custom_topic_eager_loads.each do |custom_loads|+ next unless custom_loads[:enabled].calltopic_eager_loads.concat(- custom_loads.is_a?(Array) ? custom_loads : custom_loads.call(search_pms: @search_pms).to_a,+ custom_loads[:tables] || custom_loads[:block].call(search_pms: @search_pms).to_a,)enddiff --git a/lib/topic_view.rb b/lib/topic_view.rbindex d19001c47d5..944a7abd5de 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -76,8 +76,8 @@ class TopicViewwpcf.flatten.uniqend- def self.add_custom_filter(key, &blk)- custom_filters[key] = blk+ def self.add_custom_filter(key, enabled: -> { true }, &block)+ custom_filters[key] = { block:, enabled: }enddef self.custom_filters@@ -1017,7 +1017,11 @@ class TopicViewendif @filter.present? && @filter.to_s != "summary" && TopicView.custom_filters[@filter].present?- @filtered_posts = TopicView.custom_filters[@filter].call(@filtered_posts, self)+ @filtered_posts =+ TopicView.custom_filters[@filter][:block].call(+ @filtered_posts,+ self,+ ) if TopicView.custom_filters[@filter][:enabled].callendif @best.present?diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 019c193bf17..3ed36dd8e55 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -118,8 +118,8 @@ class TopicsFilter@scopeend- def self.add_filter_by_status(status, &blk)- custom_status_filters[status] = blk+ def self.add_filter_by_status(status, enabled: -> { true }, &block)+ custom_status_filters[status] = { block:, enabled: }enddef self.custom_status_filters@@ -148,7 +148,7 @@ class TopicsFilter@scope = @scope.joins(:category).where("NOT categories.read_restricted")elseif custom_filter = TopicsFilter.custom_status_filters[status]- @scope = custom_filter.call(@scope)+ @scope = custom_filter[:block].call(@scope) if custom_filter[:enabled].callendenddiff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex e2d1eaeb5d9..579a332a10d 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -47,6 +47,8 @@ after_initialize doif TopicQuery.respond_to?(:results_filter_callbacks)TopicQuery.results_filter_callbacks << ->(_type, result, user, options) do+ return result unless SiteSetting.topic_voting_enabled+result = result.includes(:topic_vote_count)if userdiff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rbindex 2d5b69fb861..94872f87af0 100644--- a/spec/lib/plugin/instance_spec.rb+++ b/spec/lib/plugin/instance_spec.rb@@ -860,24 +860,60 @@ TEXTdescribe "#register_site_categories_callback" dofab!(:category)- it "adds a callback to the Site#categories" do- instance = Plugin::Instance.new-- site_guardian = Guardian.new+ let(:instance) { Plugin::Instance.new }+ let(:site_guardian) { Guardian.new }+ let(:site) { Site.new(site_guardian) }+ before do+ allow(instance).to receive(:enabled?).and_call_originalinstance.register_site_categories_callback do |categories, guardian|categories.each { |category| category[:test_field] = "test" }expect(guardian).to eq(site_guardian)end+ end- site = Site.new(site_guardian)-- expect(site.categories.first[:test_field]).to eq("test")- ensure+ after doSite.clear_cacheSite.categories_callbacks.clearend++ it "adds a callback to the Site#categories" do+ expect(site.categories.first[:test_field]).to eq("test")+ end++ context "when the plugin is disabled" do+ before { allow(instance).to receive(:enabled?).and_return(false) }++ it "does not run the callback" do+ expect(site.categories.first[:test_field]).to be_blank+ end+ end+ end++ describe "#register_topic_list_preload_user_ids" do+ subject(:preload_user_ids) { TopicList.preload_user_ids(stub, [], stub) }++ let(:instance) { Plugin::Instance.new }++ before do+ allow(instance).to receive(:enabled?).and_call_original+ instance.register_topic_list_preload_user_ids { |topics, user_ids, object| [1] }+ end++ after { TopicList.instance_variable_set(:@preload_user_ids, nil) }++ it "adds a callback to TopicList.preload_user_ids" do+ expect(preload_user_ids).to include(1)+ end++ context "when the plugin is disabled" do+ before { allow(instance).to receive(:enabled?).and_return(false) }++ it "does not run the callback" do+ expect(preload_user_ids).to be_empty+ end+ endenddescribe "#register_notification_consolidation_plan" dodiff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex a94043832fd..dc09b7c2bd0 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -179,29 +179,31 @@ RSpec.describe Search dodescribe "custom_eager_load" dofab!(:topic)- fab!(:post) { Fabricate(:post, topic: topic) }+ fab!(:post) { Fabricate(:post, topic:) }++ let(:enabled?) { true }+ let(:record) { described_class.execute("test").posts[0].topic }before doSearchIndexer.enableSearchIndexer.index(topic, force: true)+ described_class.custom_topic_eager_load([:topic_users], enabled: method(:enabled?))+ described_class.custom_topic_eager_load(enabled: method(:enabled?)) { [:bookmarks] }end+ after { described_class.instance_variable_set(:@custom_topic_eager_loads, []) }+it "includes custom tables" do- begin- SiteSetting.tagging_enabled = false- expect(Search.execute("test").posts[0].topic.association(:category).loaded?).to be true- expect(Search.execute("test").posts[0].topic.association(:tags).loaded?).to be false+ expect(record.association(:topic_users)).to be_loaded+ expect(record.association(:bookmarks)).to be_loaded+ end- SiteSetting.tagging_enabled = true- Search.custom_topic_eager_load([:topic_users])- Search.custom_topic_eager_load() { [:bookmarks] }+ context "when tables/blocks are disabled" do+ let(:enabled?) { false }- expect(Search.execute("test").posts[0].topic.association(:tags).loaded?).to be true- expect(Search.execute("test").posts[0].topic.association(:topic_users).loaded?).to be true- expect(Search.execute("test").posts[0].topic.association(:bookmarks).loaded?).to be true- ensure- SiteSetting.tagging_enabled = false- Search.instance_variable_set(:@custom_topic_eager_loads, [])+ it "does not include custom tables" do+ expect(record.association(:topic_users)).not_to be_loaded+ expect(record.association(:bookmarks)).not_to be_loadedendendend@@ -2850,18 +2852,37 @@ RSpec.describe Search doraw: "this is the first post about advanced filter with length more than 50 chars",)end- let!(:post1) { Fabricate(:post, raw: "this is the second post about advanced filter") }+ let!(:post1) { Fabricate(:post, raw: "another post about advanced filter min_chars:50") }- it "allows to define custom filter" do- expect(Search.new("advanced").execute.posts).to eq([post1, post0])+ context "with custom filters" do+ let(:enabled?) { true }+ let(:posts) { described_class.new("advanced min_chars:50").execute.posts }- Search.advanced_filter(/^min_chars:(\d+)$/) do |posts, match|- posts.where("(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?", match.to_i)+ before do+ described_class.advanced_filter(+ /^min_chars:(\d+)$/,+ enabled: method(:enabled?),+ ) do |posts, match|+ posts.where(+ "(SELECT LENGTH(p2.raw) FROM posts p2 WHERE p2.id = posts.id) >= ?",+ match.to_i,+ )+ endend- expect(Search.new("advanced min_chars:50").execute.posts).to eq([post0])- ensure- Search.advanced_filters.delete(/^min_chars:(\d+)$/)+ after { described_class.advanced_filters.delete(/^min_chars:(\d+)$/) }++ it "applies the custom filter" do+ expect(posts).to contain_exactly(post0)+ end++ context "when the filter is disabled" do+ let(:enabled?) { false }++ it "does not apply the custom filter" do+ expect(posts).to contain_exactly(post1) # matches `min_chars:50` as a normal word+ end+ endendit "forces custom filters matchers to be case insensitive" do@@ -2876,14 +2897,29 @@ RSpec.describe Search doSearch.advanced_filters.delete(/^MIN_CHARS:(\d+)$/)end- it "allows to define custom order" do- expect(Search.new("advanced").execute.posts).to eq([post1, post0])+ context "with custom order" do+ let(:enabled?) { true }+ let(:posts) { described_class.new("advanced order:chars").execute.posts }- Search.advanced_order(:chars) { |posts| posts.reorder("MAX(LENGTH(posts.raw)) DESC") }+ before do+ described_class.advanced_order(:chars, enabled: method(:enabled?)) do+ _1.reorder("MAX(LENGTH(posts.raw)) DESC")+ end+ end- expect(Search.new("advanced order:chars").execute.posts).to eq([post0, post1])- ensure- Search.advanced_orders.delete(:chars)+ after { described_class.advanced_orders.delete(:chars) }++ it "applies custom order" do+ expect(posts).to eq([post0, post1])+ end++ context "when the callback is disabled" do+ let(:enabled?) { false }++ it "does not apply custom order" do+ expect(posts).to eq([post1, post0])+ end+ endendenddiff --git a/spec/lib/topic_view_spec.rb b/spec/lib/topic_view_spec.rbindex 00088dc2aa7..e88f2c4e93c 100644--- a/spec/lib/topic_view_spec.rb+++ b/spec/lib/topic_view_spec.rb@@ -72,19 +72,28 @@ RSpec.describe TopicView dofab!(:p0) { Fabricate(:post, topic: topic) }fab!(:p1) { Fabricate(:post, topic: topic, wiki: true) }- after { TopicView.custom_filters.clear }+ let(:tv) { described_class.new(topic.id, evil_trout, { filter: }) }+ let(:enabled?) { true }+ let(:filter) { "wiki" }- it "allows to register custom filters" do- tv = TopicView.new(topic.id, evil_trout, { filter: "wiki" })- expect(tv.filter_posts({ filter: "wiki" })).to eq([p0, p1])+ before do+ described_class.add_custom_filter("wiki", enabled: method(:enabled?)) do |posts, topic_view|+ posts.where(wiki: true)+ end+ end- TopicView.add_custom_filter("wiki") { |posts, topic_view| posts.where(wiki: true) }+ after { described_class.custom_filters.clear }- tv = TopicView.new(topic.id, evil_trout, { filter: "wiki" })- expect(tv.filter_posts).to eq([p1])+ it "applies custom filters" do+ expect(tv.filter_posts).to contain_exactly(p1)+ end++ context "when the custom filter is disabled" do+ let(:enabled?) { false }- tv = TopicView.new(topic.id, evil_trout, { filter: "whatever" })- expect(tv.filter_posts).to eq([p0, p1])+ it "does not apply the custom filter" do+ expect(tv.filter_posts).to contain_exactly(p0, p1)+ endendenddiff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex c8b3c97ef42..edeb289d252 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -1058,15 +1058,36 @@ RSpec.describe TopicsFilter doafter { TopicsFilter.custom_status_filters.clear }- it "supports custom status filters" do- TopicsFilter.add_filter_by_status("foobar") { |scope| scope.where("word_count = 42") }+ context "with custom status filters" do+ let(:enabled?) { true }- expect(- TopicsFilter- .new(guardian: Guardian.new)- .filter_from_query_string("status:foobar")- .pluck(:id),- ).to contain_exactly(foobar_topic.id)+ before do+ TopicsFilter.add_filter_by_status("foobar", enabled: method(:enabled?)) do |scope|+ scope.where("word_count = 42")+ end+ end++ it "applies the custom filter" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("status:foobar")+ .pluck(:id),+ ).to contain_exactly(foobar_topic.id)+ end++ context "when the filter is disabled" do+ let(:enabled?) { false }++ it "does not apply the custom filter" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("status:foobar")+ .pluck(:id),+ ).to contain_exactly(*Topic.all.pluck(:id))+ end+ endendit "should only return topics that have not been closed or archived when query string is `status:open`" do