diff --git a/app/services/category_hashtag_data_source.rb b/app/services/category_hashtag_data_source.rb index 0987b6eb0a6..fe8f63caa8e 100644 --- a/app/services/category_hashtag_data_source.rb +++ b/app/services/category_hashtag_data_source.rb @@ -74,13 +74,13 @@ class CategoryHashtagDataSource Category .includes(:parent_category) .secured(guardian) - .joins( - "LEFT JOIN category_users ON category_users.user_id = #{guardian.user.id} - AND category_users.category_id = categories.id", - ) .where( - "category_users.notification_level IS NULL OR category_users.notification_level != ?", - CategoryUser.notification_levels[:muted], + "categories.id NOT IN (#{ + CategoryUser + .muted_category_ids_query(guardian.user, include_direct: true) + .select("categories.id") + .to_sql + })", ) .order(topic_count: :desc) .take(limit) diff --git a/spec/services/category_hashtag_data_source_spec.rb b/spec/services/category_hashtag_data_source_spec.rb index f6f62b45682..4235923afff 100644 --- a/spec/services/category_hashtag_data_source_spec.rb +++ b/spec/services/category_hashtag_data_source_spec.rb @@ -91,6 +91,15 @@ RSpec.describe CategoryHashtagDataSource do ) expect(described_class.search_without_term(guardian, 5).map(&:slug)).not_to include("random") end + + it "does not return child categories where the user has muted the parent" do + CategoryUser.create!( + user: user, + category: parent_category, + notification_level: CategoryUser.notification_levels[:muted], + ) + expect(described_class.search_without_term(guardian, 5).map(&:slug)).not_to include("random") + end end describe "#search_sort" do