Skip to content

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

DEV: add topic_query_suggested_options modifier (#20893)

Sam347681dd2006
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 854306235d3..dd6fe4689e8 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -605,7 +605,16 @@ class TopicView
def suggested_topics
if @include_suggested
- @suggested_topics ||= TopicQuery.new(@user).list_suggested_for(topic, pm_params: pm_params)
+ @suggested_topics ||=
+ begin
+ kwargs =
+ DiscoursePluginRegistry.apply_modifier(
+ :topic_view_suggested_topics_options,
+ { include_random: true, pm_params: pm_params },
+ self,
+ )
+ TopicQuery.new(@user).list_suggested_for(topic, **kwargs)
+ end
else
nil
end
diff --git a/spec/lib/topic_view_spec.rb b/spec/lib/topic_view_spec.rb
index 79b6bc9563f..e05cbcebc53 100644
--- a/spec/lib/topic_view_spec.rb
+++ b/spec/lib/topic_view_spec.rb
@@ -1091,4 +1091,30 @@ RSpec.describe TopicView do
end
end
end
+
+ describe "with topic_view_suggested_topics_options modifier" do
+ let!(:topic1) { Fabricate(:topic) }
+ let!(:topic2) { Fabricate(:topic) }
+
+ after { DiscoursePluginRegistry.clear_modifiers! }
+
+ it "allows disabling of random suggested" do
+ topic_view = TopicView.new(topic1)
+
+ Plugin::Instance
+ .new
+ .register_modifier(
+ :topic_view_suggested_topics_options,
+ ) do |suggested_options, inner_topic_view|
+ expect(inner_topic_view).to eq(topic_view)
+ suggested_options.merge(include_random: false)
+ end
+
+ expect(topic_view.suggested_topics.topics.count).to eq(0)
+
+ DiscoursePluginRegistry.clear_modifiers!
+
+ expect(TopicView.new(topic1).suggested_topics.topics.count).to be > 0
+ end
+ end
end