From 82ac698d4fe4f0b1ed22d06ec96700880bb6d8d7 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Wed, 8 Jun 2022 10:45:59 +0800 Subject: [PATCH] FIX: Missing tracked sub category topics from tracked topic list (#17034) Follow-up to 7ae647d092ff13bea0a93bf7fdfa13cc898cd6f6 --- lib/topic_query.rb | 2 +- spec/lib/topic_query_spec.rb | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 134e50fb60..123e47f348 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -362,7 +362,7 @@ class TopicQuery c.id FROM categories c #{has_sub_sub_categories ? "LEFT JOIN categories parent_categories ON parent_categories.id = c.parent_category_id" : ""} - WHERE (c.parent_category_id IS NULL AND c.id IN (#{tracked_category_ids_sql})) + WHERE (c.id IN (#{tracked_category_ids_sql})) OR c.parent_category_id IN (#{tracked_category_ids_sql}) #{has_sub_sub_categories ? "OR (parent_categories.id IS NOT NULL AND parent_categories.parent_category_id IN (#{tracked_category_ids_sql}))" : ""} ) diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb index 66a12cb294..37275cb3e8 100644 --- a/spec/lib/topic_query_spec.rb +++ b/spec/lib/topic_query_spec.rb @@ -197,24 +197,34 @@ describe TopicQuery do sub_category = Fabricate(:category, parent_category_id: parent_category.id) topic3 = Fabricate(:topic, category_id: sub_category.id) + parent_category_2 = Fabricate(:category) + sub_category_2 = Fabricate(:category, parent_category: parent_category_2) + topic4 = Fabricate(:topic, category: sub_category_2) + CategoryUser.create!( category_id: parent_category.id, user_id: user.id, notification_level: NotificationLevels.all[:tracking] ) - query = TopicQuery.new(user, filter: 'tracked').list_latest - - expect(query.topics.map(&:id)).to contain_exactly(topic.id, topic2.id, topic3.id) - - # includes sub-subcategories of tracked categories - SiteSetting.max_category_nesting = 3 - sub_sub_category = Fabricate(:category, parent_category_id: sub_category.id) - topic4 = Fabricate(:topic, category_id: sub_sub_category.id) + CategoryUser.create!( + category_id: sub_category_2.id, + user_id: user.id, + notification_level: NotificationLevels.all[:tracking] + ) query = TopicQuery.new(user, filter: 'tracked').list_latest expect(query.topics.map(&:id)).to contain_exactly(topic.id, topic2.id, topic3.id, topic4.id) + + # includes sub-subcategories of tracked categories + SiteSetting.max_category_nesting = 3 + sub_sub_category = Fabricate(:category, parent_category_id: sub_category.id) + topic5 = Fabricate(:topic, category_id: sub_sub_category.id) + + query = TopicQuery.new(user, filter: 'tracked').list_latest + + expect(query.topics.map(&:id)).to contain_exactly(topic.id, topic2.id, topic3.id, topic4.id, topic5.id) end end