FIX: indirectly muted categories for topic-tracking-state (#16067)

Topics belonging to indirectly muted categories should be excluded from topic-tracking-state report.
This commit is contained in:
Krzysztof Kotlarek
2022-03-02 15:02:09 +11:00
committed by GitHub
parent 9d2c5c489e
commit ea3a58d051
6 changed files with 114 additions and 18 deletions

View File

@ -235,26 +235,37 @@ class CategoryUser < ActiveRecord::Base
end
end
def self.indirectly_muted_category_ids(user)
query = Category.where.not(parent_category_id: nil)
def self.muted_category_ids_query(user, include_direct: false)
query = Category
query = query.where.not(parent_category_id: nil) if !include_direct
query = query
.joins("LEFT JOIN categories categories2 ON categories2.id = categories.parent_category_id")
.joins("LEFT JOIN category_users ON category_users.category_id = categories.id AND category_users.user_id = #{user.id}")
.joins("LEFT JOIN category_users category_users2 ON category_users2.category_id = categories2.id AND category_users2.user_id = #{user.id}")
.where("category_users.id IS NULL")
direct_category_muted_sql = "COALESCE(category_users.notification_level, #{CategoryUser.default_notification_level}) = #{CategoryUser.notification_levels[:muted]}"
parent_category_muted_sql =
"(category_users.id IS NULL AND COALESCE(category_users2.notification_level, #{CategoryUser.default_notification_level}) = #{notification_levels[:muted]})"
conditions = [parent_category_muted_sql]
conditions.push(direct_category_muted_sql) if include_direct
if SiteSetting.max_category_nesting === 3
query = query
.joins("LEFT JOIN categories categories3 ON categories3.id = categories2.parent_category_id")
.joins("LEFT JOIN category_users category_users3 ON category_users3.category_id = categories3.id AND category_users3.user_id = #{user.id}")
.where("
(category_users2.notification_level = #{notification_levels[:muted]})
OR
(category_users2.id IS NULL AND category_users3.notification_level = #{notification_levels[:muted]})
")
else
query = query.where("category_users2.notification_level = #{notification_levels[:muted]}")
grandparent_category_muted_sql = "(category_users.id IS NULL AND category_users2.id IS NULL AND COALESCE(category_users3.notification_level, #{CategoryUser.default_notification_level}) = #{notification_levels[:muted]})"
conditions.push(grandparent_category_muted_sql)
end
query.pluck("categories.id")
query.where(conditions.join(" OR "))
end
def self.muted_category_ids(user)
muted_category_ids_query(user, include_direct: true).pluck("categories.id")
end
def self.indirectly_muted_category_ids(user)
muted_category_ids_query(user).pluck("categories.id")
end
end