FIX: use category's default sort order in latest & unseen filters only. (#14571)

FIX: use category’s default sort order in latest & unseen filters only. (#14571)

Previously, even the top topics filter rendered all the topics in default sort order.

diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 992a3c7..48aa18f 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -408,6 +408,7 @@ class TopicQuery
   end
 
   def create_list(filter, options = {}, topics = nil)
+    options[:filter] ||= filter
     topics ||= default_results(options)
     topics = yield(topics) if block_given?
 
@@ -636,9 +637,10 @@ class TopicQuery
       result = result.references(:categories)
 
       if !@options[:order]
+        filter = (options[:filter] || options[:f])
         # category default sort order
         sort_order, sort_ascending = Category.where(id: category_id).pluck_first(:sort_order, :sort_ascending)
-        if sort_order
+        if sort_order && (filter.blank? || [:latest, :unseen].include?(filter))
           options[:order] = sort_order
           options[:ascending] = !!sort_ascending ? 'true' : 'false'
         else
diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb
index ec21641..1b79156 100644
--- a/spec/components/topic_query_spec.rb
+++ b/spec/components/topic_query_spec.rb
@@ -781,6 +781,17 @@ describe TopicQuery do
         expect(topic_ids - [topic_category.id]).to eq([topic_in_cat1.id, topic_in_cat2.id])
       end
 
+      it "should apply default sort order to latest and unseen filters only" do
+        category.update!(sort_order: 'created', sort_ascending: true)
+
+        topic1 = Fabricate(:topic, category: category, like_count: 1000, posts_count: 100, created_at: 1.day.ago)
+        topic2 = Fabricate(:topic, category: category, like_count: 5200, posts_count: 500, created_at: 1.hour.ago)
+        TopTopic.refresh!
+
+        topic_ids = TopicQuery.new(user, category: category.id).list_top_for(:monthly).topics.map(&:id)
+        expect(topic_ids).to eq([topic2.id, topic1.id])
+      end
+
       it "ignores invalid order value" do
         category.update!(sort_order: 'funny')
         topic_ids = TopicQuery.new(user, category: category.id).list_latest.topics.map(&:id)

GitHub sha: fd9a5bc02328b1301814e1b70fe4b7fc470d1a48

This commit appears in #14571 which was approved by gschlager. It was merged by vinothkannans.