diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 2395947e253..2ca9ee0bce1 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -460,6 +460,8 @@ class TopicQuery options[:filter] ||= filter topics ||= default_results(options) topics = yield(topics) if block_given? + topics = + DiscoursePluginRegistry.apply_modifier(:topic_query_create_list_topics, topics, options, self) options = options.merge(@options) if %w[activity default].include?(options[:order] || "activity") && !options[:unordered] && diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index 547b16de737..98966dacff9 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -1940,4 +1940,29 @@ RSpec.describe TopicQuery do ).to contain_exactly(category.topic_id, subcategory.topic_id, subcategory_regular_topic.id) end end + + describe "with topic_query_create_list_topics modifier" do + fab!(:topic1) { Fabricate(:topic, created_at: 3.days.ago, bumped_at: 1.hour.ago) } + fab!(:topic2) { Fabricate(:topic, created_at: 2.days.ago, bumped_at: 3.hour.ago) } + + after { DiscoursePluginRegistry.clear_modifiers! } + + it "allows changing" do + original_topic_query = TopicQuery.new(user) + + Plugin::Instance + .new + .register_modifier(:topic_query_create_list_topics) do |topics, options, topic_query| + expect(topic_query).to eq(topic_query) + topic_query.options[:order] = "created" + topics + end + + expect(original_topic_query.list_latest.topics.map(&:id)).to eq([topic1, topic2].map(&:id)) + + DiscoursePluginRegistry.clear_modifiers! + + expect(original_topic_query.list_latest.topics.map(&:id)).to eq([topic2, topic1].map(&:id)) + end + end end