FEATURE: Hook for suggested topic customization (#20618)
This commit is contained in:
parent
ab442058c0
commit
0a5b078ac7
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue