FIX: Respect show_category_definitions_in_topic_lists in category lists (#10853)

FIX: Respect show_category_definitions_in_topic_lists in category lists (#10853)

When that site setting is enabled, the category counts (new/unread) include the subcategory definition topics, but the topics aren’t included in the list. This fixes that discrepancy.

diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index fb6ff57..a5f4bd7 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -700,11 +700,10 @@ class TopicQuery
       if options[:no_subcategories]
         result = result.where('categories.id = ?', category_id)
       else
-        result = result.where(<<~SQL, subcategory_ids: Category.subcategory_ids(category_id), category_id: category_id)
-          categories.id in (:subcategory_ids) AND (
-            categories.topic_id <> topics.id OR categories.id = :category_id
-          )
-          SQL
+        result = result.where("categories.id IN (?)", Category.subcategory_ids(category_id))
+        if !SiteSetting.show_category_definitions_in_topic_lists
+          result = result.where("categories.topic_id <> topics.id OR categories.id = ?", category_id)
+        end
       end
       result = result.references(:categories)
 
diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb
index fdb0598..802840c 100644
--- a/spec/components/topic_query_spec.rb
+++ b/spec/components/topic_query_spec.rb
@@ -217,6 +217,11 @@ describe TopicQuery do
         expect(TopicQuery.new(moderator, category: category.id, no_subcategories: true).list_latest.topics.size).to eq(1)
       end
 
+      it "shows a subcategory definition topic in its parent list with the right site setting" do
+        SiteSetting.show_category_definitions_in_topic_lists = true
+        expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(2)
+      end
+
       it "works with subsubcategories" do
         SiteSetting.max_category_nesting = 3
 

GitHub sha: ddd6c990

This commit appears in #10853 which was approved by eviltrout. It was merged by pmusaraj.