From ddec7bf6a378975a2491df73a6dbc0c625aa138c Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 28 Mar 2023 15:52:17 +1100 Subject: [PATCH] DEV: allow API for list_suggested_for to exclude random (#20857) This is needed so plugins can potentially create lists without random topics --- lib/topic_query.rb | 5 +++-- spec/lib/topic_query_spec.rb | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 2d93e743925..5dace8bb8a1 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -204,7 +204,8 @@ class TopicQuery end # Return a list of suggested topics for a topic - def list_suggested_for(topic, pm_params: nil) + # The include_random param was added so plugins can generate a suggested topics list without the random topics + def list_suggested_for(topic, pm_params: nil, include_random: true) # Don't suggest messages unless we have a user, and private messages are # enabled. if topic.private_message? && @@ -250,7 +251,7 @@ class TopicQuery end if !topic.private_message? - unless builder.full? + if include_random && !builder.full? builder.add_results( random_suggested(topic, builder.results_left, builder.excluded_topic_ids), ) diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index 8ca8ce492ef..547b16de737 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -1379,6 +1379,10 @@ RSpec.describe TopicQuery do it "should return the new topic" do expect(TopicQuery.new.list_suggested_for(topic).topics).to eq([new_topic]) end + + it "should return the nothing when random topics excluded" do + expect(TopicQuery.new.list_suggested_for(topic, include_random: false).topics).to eq([]) + end end context "when anonymously browsing with invisible, closed and archived" do