From 2acec4370bc8b7bbecf297237c590b1f9d8a8f81 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Fri, 29 May 2020 12:59:34 +1000 Subject: [PATCH] FIX: muted tags removed topics with no tags from counts We previously did not account for completely untagged topics when looking at muted tags, this caused new/unread counts to be off if 1. You had muted tags 2. You had an unread/new topic 3. This topic had no tags --- app/models/topic_tracking_state.rb | 26 ++++++++++++++++-------- spec/models/topic_tracking_state_spec.rb | 10 +++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index eb926671b15..867961e19d0 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -315,18 +315,26 @@ class TopicTrackingState "(topics.visible #{append}) AND" end - tags_filter = - if opts[:muted_tag_ids].present? && SiteSetting.remove_muted_tags_from_latest == 'always' - <<~SQL - NOT ((select array_agg(tag_id) from topic_tags where topic_tags.topic_id = topics.id) && ARRAY[#{opts[:muted_tag_ids].join(',')}]) AND + tags_filter = "" + + if (muted_tag_ids = opts[:muted_tag_ids]).present? && ['always', 'only_muted'].include?(SiteSetting.remove_muted_tags_from_latest) + existing_tags_sql = "(select array_agg(tag_id) from topic_tags where topic_tags.topic_id = topics.id)" + muted_tags_array_sql = "ARRAY[#{opts[:muted_tag_ids].join(',')}]" + + if SiteSetting.remove_muted_tags_from_latest == 'always' + tags_filter = <<~SQL + NOT ( + COALESCE(#{existing_tags_sql}, ARRAY[]::int[]) && #{muted_tags_array_sql} + ) AND SQL - elsif opts[:muted_tag_ids].present? && SiteSetting.remove_muted_tags_from_latest == 'only_muted' - <<~SQL - NOT ((select array_agg(tag_id) from topic_tags where topic_tags.topic_id = topics.id) <@ ARRAY[#{opts[:muted_tag_ids].join(',')}]) AND + else # only muted + tags_filter = <<~SQL + NOT ( + COALESCE(#{existing_tags_sql}, ARRAY[-999]) <@ #{muted_tags_array_sql} + ) AND SQL - else - "" end + end sql = +<<~SQL SELECT #{select} diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index 3ac528268d9..ae0f852d635 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -446,6 +446,11 @@ describe TopicTrackingState do report = TopicTrackingState.report(user) expect(report.length).to eq(0) + + TopicTag.where(topic_id: topic.id).delete_all + + report = TopicTrackingState.report(user) + expect(report.length).to eq(1) end it "remove_muted_tags_from_latest is set to only_muted" do @@ -475,6 +480,11 @@ describe TopicTrackingState do report = TopicTrackingState.report(user) expect(report.length).to eq(0) + + TopicTag.where(topic_id: topic.id).delete_all + + report = TopicTrackingState.report(user) + expect(report.length).to eq(1) end it "remove_muted_tags_from_latest is set to never" do