mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 00:41:16 +08:00
FIX: tracked filter did not account for max_category_nesting of 3 (#16963)
This commit is contained in:

committed by
GitHub

parent
7b4e338c0e
commit
7ae647d092
@ -346,16 +346,22 @@ class TopicQuery
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.tracked_filter(list, user_id)
|
def self.tracked_filter(list, user_id)
|
||||||
|
tracked_category_ids_sql = <<~SQL
|
||||||
|
SELECT cd.category_id FROM category_users cd
|
||||||
|
WHERE cd.user_id = :user_id AND cd.notification_level >= :tracking
|
||||||
|
SQL
|
||||||
|
|
||||||
|
has_sub_sub_categories = SiteSetting.max_category_nesting == 3
|
||||||
|
|
||||||
sql = +<<~SQL
|
sql = +<<~SQL
|
||||||
topics.category_id IN (
|
topics.category_id IN (
|
||||||
SELECT cu.category_id FROM category_users cu
|
SELECT
|
||||||
WHERE cu.user_id = :user_id AND cu.notification_level >= :tracking
|
c.id
|
||||||
)
|
FROM categories c
|
||||||
OR topics.category_id IN (
|
#{has_sub_sub_categories ? "LEFT JOIN categories parent_categories ON parent_categories.id = c.parent_category_id" : ""}
|
||||||
SELECT c.id FROM categories c WHERE c.parent_category_id IN (
|
WHERE (c.parent_category_id IS NULL AND c.id IN (#{tracked_category_ids_sql}))
|
||||||
SELECT cd.category_id FROM category_users cd
|
OR c.parent_category_id IN (#{tracked_category_ids_sql})
|
||||||
WHERE cd.user_id = :user_id AND cd.notification_level >= :tracking
|
#{has_sub_sub_categories ? "OR (parent_categories.id IS NOT NULL AND parent_categories.parent_category_id IN (#{tracked_category_ids_sql}))" : ""}
|
||||||
)
|
|
||||||
)
|
)
|
||||||
SQL
|
SQL
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ describe TopicQuery do
|
|||||||
SiteSetting.tagging_enabled = true
|
SiteSetting.tagging_enabled = true
|
||||||
|
|
||||||
tag = Fabricate(:tag)
|
tag = Fabricate(:tag)
|
||||||
Fabricate(:topic, tags: [tag])
|
topic = Fabricate(:topic, tags: [tag])
|
||||||
topic2 = Fabricate(:topic)
|
topic2 = Fabricate(:topic)
|
||||||
|
|
||||||
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
||||||
@ -183,26 +183,38 @@ describe TopicQuery do
|
|||||||
)
|
)
|
||||||
|
|
||||||
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
||||||
expect(query.topics.length).to eq(1)
|
|
||||||
|
expect(query.topics.map(&:id)).to contain_exactly(topic.id)
|
||||||
|
|
||||||
cu.update!(notification_level: NotificationLevels.all[:tracking])
|
cu.update!(notification_level: NotificationLevels.all[:tracking])
|
||||||
|
|
||||||
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
||||||
expect(query.topics.length).to eq(2)
|
|
||||||
|
expect(query.topics.map(&:id)).to contain_exactly(topic.id, topic2.id)
|
||||||
|
|
||||||
# includes subcategories of tracked categories
|
# includes subcategories of tracked categories
|
||||||
parentcat = Fabricate(:category)
|
parent_category = Fabricate(:category)
|
||||||
subcat = Fabricate(:category, parent_category_id: parentcat.id)
|
sub_category = Fabricate(:category, parent_category_id: parent_category.id)
|
||||||
topic3 = Fabricate(:topic, category_id: subcat.id)
|
topic3 = Fabricate(:topic, category_id: sub_category.id)
|
||||||
|
|
||||||
CategoryUser.create!(
|
CategoryUser.create!(
|
||||||
category_id: parentcat.id,
|
category_id: parent_category.id,
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
notification_level: NotificationLevels.all[:tracking]
|
notification_level: NotificationLevels.all[:tracking]
|
||||||
)
|
)
|
||||||
|
|
||||||
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
||||||
expect(query.topics.length).to eq(3)
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
query = TopicQuery.new(user, filter: 'tracked').list_latest
|
||||||
|
|
||||||
|
expect(query.topics.map(&:id)).to contain_exactly(topic.id, topic2.id, topic3.id, topic4.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user