diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb index 044935f5b..77b05dcfc 100644 --- a/lib/discourse_tagging.rb +++ b/lib/discourse_tagging.rb @@ -185,14 +185,8 @@ module DiscourseTagging end end - # TODO: this is unused? - def self.notification_key(tag_id) - "tags_notification:#{tag_id}" - end - - # TODO: this is unused? def self.muted_tags(user) return [] unless user - UserCustomField.where(user_id: user.id, value: TopicUser.notification_levels[:muted]).pluck(:name).map { |x| x[0,17] == "tags_notification" ? x[18..-1] : nil}.compact + TagUser.lookup(user, :muted).joins(:tag).pluck('tags.name') end end diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 49cef6da0..a8e1d77e6 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -597,8 +597,7 @@ class TopicQuery if user.nil? || !SiteSetting.tagging_enabled || !SiteSetting.remove_muted_tags_from_latest list else - muted_tags = DiscourseTagging.muted_tags(user) - if muted_tags.empty? + if !TagUser.lookup(user, :muted).exists? list else showing_tag = if opts[:filter] @@ -608,17 +607,17 @@ class TopicQuery nil end - if muted_tags.include?(showing_tag) + if TagUser.lookup(user, :muted).joins(:tag).where('tags.name = ?', showing_tag).exists? list # if viewing the topic list for a muted tag, show all the topics else - arr = muted_tags.map{ |z| "'#{z}'" }.join(',') - list.where("EXISTS ( - SELECT 1 - FROM topic_custom_fields tcf - WHERE tcf.name = 'tags' - AND tcf.value NOT IN (#{arr}) - AND tcf.topic_id = topics.id - ) OR NOT EXISTS (select 1 from topic_custom_fields tcf where tcf.name = 'tags' and tcf.topic_id = topics.id)") + muted_tag_ids = TagUser.lookup(user, :muted).pluck(:tag_id) + list = list.where(" + EXISTS ( + SELECT 1 + FROM topic_tags tt + WHERE tt.tag_id NOT IN (:tag_ids) + AND tt.topic_id = topics.id + ) OR NOT EXISTS (SELECT 1 FROM topic_tags tt WHERE tt.topic_id = topics.id)", tag_ids: muted_tag_ids) end end end diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 1c6e63fce..3461a5d27 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -178,6 +178,33 @@ describe TopicQuery do end end + context 'muted tags' do + it 'is removed from new and latest lists' do + SiteSetting.tagging_enabled = true + SiteSetting.remove_muted_tags_from_latest = true + + muted_tag, other_tag = Fabricate(:tag), Fabricate(:tag) + + muted_topic = Fabricate(:topic, tags: [muted_tag]) + tagged_topic = Fabricate(:topic, tags: [other_tag]) + untagged_topic = Fabricate(:topic) + + TagUser.create!(user_id: user.id, + tag_id: muted_tag.id, + notification_level: CategoryUser.notification_levels[:muted]) + + topic_ids = topic_query.list_latest.topics.map(&:id) + expect(topic_ids).not_to include(muted_topic.id) + expect(topic_ids).to include(tagged_topic.id) + expect(topic_ids).to include(untagged_topic.id) + + topic_ids = topic_query.list_new.topics.map(&:id) + expect(topic_ids).not_to include(muted_topic.id) + expect(topic_ids).to include(tagged_topic.id) + expect(topic_ids).to include(untagged_topic.id) + end + end + context 'a bunch of topics' do let!(:regular_topic) do Fabricate(:topic, title: 'this is a regular topic',