diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 53a46066ec5..d727ab2e625 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -111,6 +111,8 @@ class DiscoursePluginRegistry define_filtered_register :about_stat_groups define_filtered_register :bookmarkables + define_filtered_register :list_suggested_for_providers + def self.register_auth_provider(auth_provider) self.auth_providers << auth_provider end diff --git a/lib/topic_query.rb b/lib/topic_query.rb index fe5dc2ff500..88f6e428be4 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -216,6 +216,13 @@ class TopicQuery pm_params = pm_params || get_pm_params(topic) + if DiscoursePluginRegistry.list_suggested_for_providers.any? + DiscoursePluginRegistry.list_suggested_for_providers.each do |provider| + suggested = provider.call(topic, pm_params, self) + builder.add_results(suggested[:result]) if suggested && !suggested[:result].blank? + end + end + # When logged in we start with different results if @user if topic.private_message? diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index 520ee27e5ad..98442324c2d 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -1393,6 +1393,42 @@ RSpec.describe TopicQuery do end end + context "with a custom suggested provider registered" do + let!(:topic1) { Fabricate(:topic) } + let!(:topic2) { Fabricate(:topic) } + let!(:topic3) { Fabricate(:topic) } + let!(:topic4) { Fabricate(:topic) } + let!(:topic5) { Fabricate(:topic) } + let!(:topic6) { Fabricate(:topic) } + let!(:topic7) { Fabricate(:topic) } + + let(:plugin_class) do + Class.new(Plugin::Instance) do + attr_accessor :enabled + def enabled? + true + end + + def self.custom_suggested_topics(topic, pm_params, topic_query) + { result: Topic.order("id desc").limit(1), params: {} } + end + end + end + + let(:plugin) { plugin_class.new } + + it "should return suggested defined by the custom provider" do + DiscoursePluginRegistry.register_list_suggested_for_provider( + plugin_class.method(:custom_suggested_topics), + plugin, + ) + + expect(TopicQuery.new.list_suggested_for(topic1).topics).to include(Topic.last) + + DiscoursePluginRegistry.reset_register!(:list_suggested_for_providers) + end + end + context "when logged in" do def suggested_for(topic) topic_query.list_suggested_for(topic)&.topics&.map { |t| t.id }