Skip to content

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

FIX: Keep order of ids specified (#42909)

Natalie Tayfd6f13f8681e
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 45d647dbf0d..241512e0a0c 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -318,7 +318,14 @@ class TopicQuery
results = remove_muted_topics(results, @user)
end
- results = apply_ordering(results) if results.order_values.empty?
+ 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
create_list(
:filter,
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index 704f38d459b..caddb9f67dc 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -1842,14 +1842,16 @@ RSpec.describe ListController do
end
end
- it "allows users to filter on a set of topic ids" do
- topic_2 = Fabricate(:topic)
+ it "returns topic_ids in the order when specified" do
+ topic_1 = Fabricate(:topic, bumped_at: 3.days.ago)
+ 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_2.id.to_s }
+ get "/filter.json", params: { topic_ids: [topic_3.id, topic_1.id, topic_2.id].join(",") }
expect(response.status).to eq(200)
expect(response.parsed_body["topic_list"]["topics"].map { |topic| topic["id"] }).to eq(
- [topic_2.id],
+ [topic_3.id, topic_1.id, topic_2.id],
)
end