From d1779346e89f642d7b9ddcfb0c12912217a70609 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 6 Jan 2020 18:22:42 +0000 Subject: [PATCH] FIX: topic_tracking_state when mute_all_categories_by_default is enabled --- app/models/topic_tracking_state.rb | 7 ++++--- lib/topic_query.rb | 4 +--- spec/models/topic_tracking_state_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index aa65917c6ea..49ffeb97512 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -306,9 +306,10 @@ class TopicTrackingState #{tags_filter} topics.deleted_at IS NULL AND #{category_filter} - (category_users.notification_level IS NULL OR - last_read_post_number IS NOT NULL OR - category_users.notification_level <> #{CategoryUser.notification_levels[:muted]}) + NOT ( + last_read_post_number IS NULL AND + COALESCE(category_users.notification_level, #{CategoryUser.default_notification_level}) = #{CategoryUser.notification_levels[:muted]} + ) SQL if opts[:topic_id] diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 7e63bb024a4..49f02f6ffd1 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -857,8 +857,6 @@ class TopicQuery category_id = get_category_id(opts[:exclude]) if opts if user - default_notification_level = SiteSetting.mute_all_categories_by_default ? CategoryUser.notification_levels[:muted] : CategoryUser.notification_levels[:regular] - list = list .references("cu") .joins("LEFT JOIN category_users ON category_users.category_id = topics.category_id AND category_users.user_id = #{user.id}") @@ -866,7 +864,7 @@ class TopicQuery OR COALESCE(category_users.notification_level, :default) <> :muted OR tu.notification_level > :regular", category_id: category_id || -1, - default: default_notification_level, + default: CategoryUser.default_notification_level, muted: CategoryUser.notification_levels[:muted], regular: TopicUser.notification_levels[:regular]) elsif SiteSetting.mute_all_categories_by_default diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index 1691547ff46..e19c696eb79 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -366,6 +366,26 @@ describe TopicTrackingState do expect(report.length).to eq(1) end + it "works when categories are default muted" do + SiteSetting.mute_all_categories_by_default = true + + user = Fabricate(:user) + post + + report = TopicTrackingState.report(user) + expect(report.length).to eq(0) + + CategoryUser.create!(user_id: user.id, + notification_level: CategoryUser.notification_levels[:regular], + category_id: post.topic.category_id + ) + + create_post(topic_id: post.topic_id) + + report = TopicTrackingState.report(user) + expect(report.length).to eq(1) + end + context 'muted tags' do it "remove_muted_tags_from_latest is set to always" do SiteSetting.remove_muted_tags_from_latest = 'always'