From 832cb9fdc338e993fdaf216cf1ad3175fd9afd53 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 17 Apr 2015 14:03:08 +1000 Subject: [PATCH] FIX: admin check missing from topic tracking state FIX: handle muted categories correctly --- app/models/topic_tracking_state.rb | 5 +++-- spec/models/topic_tracking_state_spec.rb | 25 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb index 05334ccadae..0bf3fa7096c 100644 --- a/app/models/topic_tracking_state.rb +++ b/app/models/topic_tracking_state.rb @@ -148,14 +148,15 @@ class TopicTrackingState ((#{unread}) OR (#{new})) AND (topics.visible OR u.admin OR u.moderator) AND topics.deleted_at IS NULL AND - ( category_id IS NULL OR NOT c.read_restricted OR category_id IN ( + ( category_id IS NULL OR NOT c.read_restricted OR u.admin OR category_id IN ( SELECT c2.id FROM categories c2 JOIN category_groups cg ON cg.category_id = c2.id JOIN group_users gu ON gu.user_id = u.id AND cg.group_id = gu.group_id WHERE c2.read_restricted ) ) AND NOT EXISTS( SELECT 1 FROM category_users cu - WHERE cu.user_id = u.id AND + WHERE last_read_post_number IS NULL AND + cu.user_id = u.id AND cu.category_id = topics.category_id AND cu.notification_level = #{CategoryUser.notification_levels[:muted]}) diff --git a/spec/models/topic_tracking_state_spec.rb b/spec/models/topic_tracking_state_spec.rb index 3c94c26b15e..6897a2550ac 100644 --- a/spec/models/topic_tracking_state_spec.rb +++ b/spec/models/topic_tracking_state_spec.rb @@ -15,11 +15,34 @@ describe TopicTrackingState do TopicTrackingState.publish_unread(post) end + it "correctly handles muted categories" do + + user = Fabricate(:user) + post + + report = TopicTrackingState.report([user.id]) + expect(report.length).to eq(1) + + CategoryUser.create!(user_id: user.id, + notification_level: CategoryUser.notification_levels[:muted], + category_id: post.topic.category_id + ) + + create_post(topic_id: post.topic_id) + + report = TopicTrackingState.report([user.id]) + expect(report.length).to eq(0) + + TopicUser.create!(user_id: user.id, topic_id: post.topic_id, last_read_post_number: 1, notification_level: 3) + + report = TopicTrackingState.report([user.id]) + expect(report.length).to eq(1) + end + it "correctly gets the tracking state" do report = TopicTrackingState.report([user.id]) expect(report.length).to eq(0) - new_post = post post.topic.notifier.watch_topic!(post.topic.user_id) report = TopicTrackingState.report([user.id])