FIX: duplicate topics and posts in summary email because user has muted tags and topics contain multiple tags

This commit is contained in:
Neil Lalonde 2017-07-04 16:12:10 -04:00
parent df213639f1
commit fcb2f68565
2 changed files with 10 additions and 5 deletions

View File

@ -372,8 +372,11 @@ class Topic < ActiveRecord::Base
# Remove muted tags
muted_tag_ids = TagUser.lookup(user, :muted).pluck(:tag_id)
unless muted_tag_ids.empty?
topics = topics.joins("LEFT OUTER JOIN topic_tags ON topic_tags.topic_id = topics.id")
.where("topic_tags.tag_id NOT IN (?) OR topic_tags.tag_id is null", muted_tag_ids)
# If multiple tags per topic, include topics with tags that aren't muted,
# and don't forget untagged topics.
topics = topics.where(
"EXISTS ( SELECT 1 FROM topic_tags WHERE topic_tags.topic_id = topics.id AND tag_id NOT IN (?) )
OR NOT EXISTS (SELECT 1 FROM topic_tags WHERE topic_tags.topic_id = topics.id)", muted_tag_ids)
end
topics

View File

@ -1415,13 +1415,15 @@ describe Topic do
it "returns topics with no tags too" do
user = Fabricate(:user)
muted_tag, other_tag = Fabricate(:tag), Fabricate(:tag)
muted_tag = Fabricate(:tag)
TagUser.change(user.id, muted_tag.id, TagUser.notification_levels[:muted])
topic1 = Fabricate(:topic, tags: [muted_tag])
topic2 = Fabricate(:topic, tags: [other_tag])
topic2 = Fabricate(:topic, tags: [Fabricate(:tag), Fabricate(:tag)])
topic3 = Fabricate(:topic)
expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to contain_exactly(topic2, topic3)
topics = Topic.for_digest(user, 1.year.ago, top_order: true)
expect(topics.size).to eq(2)
expect(topics).to contain_exactly(topic2, topic3)
end
it "sorts by category notification levels" do