A platform for community discussion. Free, open, simple.
FIX: Use topic filters for ordered topic IDs (#42919)
Natalie Tay01afcedd2590
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex 73ce35ef48b..b4216f9140d 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -132,7 +132,7 @@ class ListController < ApplicationControllerdef filtertopic_query_opts = { no_definitions: !SiteSetting.show_category_definitions_in_topic_lists }- %i[page q topic_ids].each do |key|+ %i[page q].each do |key|if params.key?(key.to_s)value = params[key]raise Discourse::InvalidParameters.new(key) if !TopicQuery.validate?(key, value)@@ -140,8 +140,6 @@ class ListController < ApplicationControllerendend- topic_query_opts[:topic_ids] = param_to_integer_list(:topic_ids) if params.key?(:topic_ids)-user = current_userlist = TopicQuery.new(user, topic_query_opts).list_filterlist.more_topics_url = construct_url_with(:next, topic_query_opts)diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 8e2589d6c4d..023b504a45a 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3184,6 +3184,8 @@ en:order: "Sort topics by a specific field"category: "Show topics in a specific category"category_any: "Show topics in any of the specified categories (comma-separated)"+ topic: "Show specific topics by ID"+ topic_any: "Show topics in the specified order (comma-separated IDs)"exclude_category: "Exclude topics from a specific category"category_without_subcategories: "Show topics only in the parent category, excluding subcategories"exclude_category_without_subcategories: "Exclude topics only from the parent category, not subcategories"diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 241512e0a0c..1096ee60fea 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -318,18 +318,14 @@ class TopicQueryresults = remove_muted_topics(results, @user)end- if (topic_ids = @options[:topic_ids].presence)- results =- results.reorder(- DB.sql_fragment("array_position(ARRAY[?]::integer[], topics.id)", topic_ids.uniq),- )- elsif results.order_values.empty?- results = apply_ordering(results)- end+ results = apply_ordering(results) if results.order_values.empty?create_list(:filter,- { include_filter_option_info: @options[:include_filter_option_info].to_s != "false" },+ {+ include_filter_option_info: @options[:include_filter_option_info].to_s != "false",+ unordered: topics_filter.topic_ids.present?,+ },results,)enddiff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 1e3e5fdf5f9..c7a01c04554 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueclass TopicsFilter- attr_reader :topic_notification_levels+ attr_reader :topic_ids, :topic_notification_levelsdef initialize(guardian:, scope: Topic.all, loaded_topic_users_reference: false)@loaded_topic_users_reference = loaded_topic_users_reference@@ -104,6 +104,8 @@ class TopicsFilterfilter_tag_groups(values: key_prefixes.zip(filter_values))when "tag"filter_tags(values: key_prefixes.zip(filter_values))+ when "topic"+ filter_topics(values: filter_values)when "locale"filter_locale(values: key_prefixes.zip(filter_values))when "views-min"@@ -199,6 +201,12 @@ class TopicsFilter},],},+ {+ name: "topic:",+ description: I18n.t("filter.description.topic"),+ type: "text",+ delimiters: [{ name: ",", description: I18n.t("filter.description.topic_any") }],+ },{name: "activity-before:",description: I18n.t("filter.description.activity_before"),@@ -401,6 +409,17 @@ class TopicsFilterprivate+ def filter_topics(values:)+ @topic_ids =+ values+ .flat_map { |value| value.split(",") }+ .filter_map { |value| Integer(value, 10, exception: false) }+ .select(&:positive?)+ .uniq++ @scope = @scope.in_order_of(:id, @topic_ids)+ end+YYYY_MM_DD_REGEXP =/\A(?<year>[12][0-9]{3})-(?<month>0?[1-9]|1[0-2])-(?<day>0?[1-9]|[12]\d|3[01])\z/private_constant :YYYY_MM_DD_REGEXPdiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex caddb9f67dc..81b2f942a41 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -1842,12 +1842,13 @@ RSpec.describe ListController doendend- it "returns topic_ids in the order when specified" do- topic_1 = Fabricate(:topic, bumped_at: 3.days.ago)+ it "returns topics in the order specified by the topic filter" do+ topic_1 =+ Fabricate(:topic, bumped_at: 3.days.ago, pinned_at: 1.day.ago, pinned_globally: true)topic_2 = Fabricate(:topic, bumped_at: 2.days.ago)topic_3 = Fabricate(:topic, bumped_at: 1.day.ago)- get "/filter.json", params: { topic_ids: [topic_3.id, topic_1.id, topic_2.id].join(",") }+ get "/filter.json", params: { q: "topic:#{topic_3.id},#{topic_1.id},#{topic_2.id}" }expect(response.status).to eq(200)expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(