FIX: Make topic query include topics from sub-sub-categories (#8709)

This commit is contained in:
Dan Ungureanu 2020-01-20 17:06:58 +02:00 committed by GitHub
parent 1014e56e80
commit 788ddcc407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -677,15 +677,18 @@ class TopicQuery
else else
sql = <<~SQL sql = <<~SQL
categories.id IN ( categories.id IN (
SELECT c2.id FROM categories c2 WHERE c2.parent_category_id = :category_id WITH RECURSIVE subcategories AS (
UNION ALL SELECT :category_id id, 1 depth
SELECT :category_id UNION
) AND SELECT categories.id, (subcategories.depth + 1) depth
topics.id NOT IN ( FROM categories
SELECT c3.topic_id FROM categories c3 WHERE c3.parent_category_id = :category_id AND c3.topic_id IS NOT NULL JOIN subcategories ON subcategories.id = categories.parent_category_id
WHERE subcategories.depth < :max_category_nesting
) )
SELECT subcategories.id FROM subcategories
) AND (categories.id = :category_id OR topics.id != categories.topic_id)
SQL SQL
result = result.where(sql, category_id: category_id) result = result.where(sql, category_id: category_id, max_category_nesting: SiteSetting.max_category_nesting)
end end
result = result.references(:categories) result = result.references(:categories)

View File

@ -153,6 +153,7 @@ describe TopicQuery do
context 'subcategories' do context 'subcategories' do
let!(:subcategory) { Fabricate(:category_with_definition, parent_category_id: category.id) } let!(:subcategory) { Fabricate(:category_with_definition, parent_category_id: category.id) }
let(:subsubcategory) { Fabricate(:category_with_definition, parent_category_id: subcategory.id) }
it "works with subcategories" do it "works with subcategories" do
expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(1) expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(1)
@ -160,6 +161,23 @@ describe TopicQuery do
expect(TopicQuery.new(moderator, category: category.id, no_subcategories: true).list_latest.topics.size).to eq(1) expect(TopicQuery.new(moderator, category: category.id, no_subcategories: true).list_latest.topics.size).to eq(1)
end end
it "works with subsubcategories" do
SiteSetting.max_category_nesting = 3
Fabricate(:topic, category: category)
Fabricate(:topic, category: subcategory)
Fabricate(:topic, category: subsubcategory)
SiteSetting.max_category_nesting = 2
expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(3)
expect(TopicQuery.new(moderator, category: subcategory.id).list_latest.topics.size).to eq(3)
expect(TopicQuery.new(moderator, category: subsubcategory.id).list_latest.topics.size).to eq(2)
SiteSetting.max_category_nesting = 3
expect(TopicQuery.new(moderator, category: category.id).list_latest.topics.size).to eq(4)
expect(TopicQuery.new(moderator, category: subcategory.id).list_latest.topics.size).to eq(3)
expect(TopicQuery.new(moderator, category: subsubcategory.id).list_latest.topics.size).to eq(2)
end
end end
end end