From 848c0803bdcea41dcc372fe5bab9611fa7363288 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 31 Aug 2017 14:19:05 -0700 Subject: [PATCH] Exclude muted topics from unread count. --- zerver/lib/message.py | 22 ++++++++++++++++++---- zerver/tests/test_events.py | 28 +++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 6e46453966..2f39dd093e 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -14,6 +14,7 @@ from zerver.lib.cache import cache_with_key, to_dict_cache_key from zerver.lib.request import JsonableError from zerver.lib.str_utils import force_bytes, dict_with_str_keys from zerver.lib.timestamp import datetime_to_timestamp +from zerver.lib.topic_mutes import build_topic_mute_checker from zerver.models import ( get_display_recipient_by_id, @@ -448,10 +449,23 @@ def get_unread_message_ids_per_recipient(user_profile): rows = list(reversed(user_msgs)) muted_recipient_ids = get_muted_recipient_ids(user_profile) - active_stream_rows = [ - row for row in rows - if row['message__recipient_id'] not in muted_recipient_ids - ] + + topic_mute_checker = build_topic_mute_checker(user_profile) + + def is_row_muted(row): + # type: (Dict[str, Any]) -> bool + recipient_id = row['message__recipient_id'] + + if recipient_id in muted_recipient_ids: + return True + + topic_name = row['message__subject'] + if topic_mute_checker(recipient_id, topic_name): + return True + + return False + + active_stream_rows = [row for row in rows if not is_row_muted(row)] count = len(active_stream_rows) diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index a59d4e5787..e35adc7352 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -79,6 +79,9 @@ from zerver.lib.test_helpers import POSTRequestMock, get_subscription, \ from zerver.lib.test_classes import ( ZulipTestCase, ) +from zerver.lib.topic_mutes import ( + add_topic_mute, +) from zerver.lib.validator import ( check_bool, check_dict, check_dict_only, check_float, check_int, check_list, check_string, equals, check_none_or, Validator @@ -1655,12 +1658,26 @@ class FetchInitialStateDataTest(ZulipTestCase): subscription.in_home_view = False subscription.save() + def mute_topic(user_profile, stream_name, topic_name): + # type: (UserProfile, Text, Text) -> None + stream = get_stream(stream_name, realm) + recipient = get_recipient(Recipient.STREAM, stream.id) + + add_topic_mute( + user_profile=user_profile, + stream_id=stream.id, + recipient_id=recipient.id, + topic_name='muted-topic', + ) + cordelia = self.example_user('cordelia') sender_id = cordelia.id sender_email = cordelia.email user_profile = self.example_user('hamlet') othello = self.example_user('othello') + realm = user_profile.realm + # our tests rely on order assert(sender_email < user_profile.email) assert(user_profile.email < othello.email) @@ -1670,8 +1687,12 @@ class FetchInitialStateDataTest(ZulipTestCase): muted_stream = self.subscribe(user_profile, 'Muted Stream') mute_stream(user_profile, muted_stream) + mute_topic(user_profile, 'Denmark', 'muted-topic') + stream_message_id = self.send_message(sender_email, "Denmark", Recipient.STREAM, "hello") muted_stream_message_id = self.send_message(sender_email, "Muted Stream", Recipient.STREAM, "hello") + muted_topic_message_id = self.send_message(sender_email, "Denmark", Recipient.STREAM, + subject="muted-topic", content="hello") huddle_message_id = self.send_message(sender_email, [user_profile.email, othello.email], @@ -1696,10 +1717,15 @@ class FetchInitialStateDataTest(ZulipTestCase): unread_stream = result['streams'][0] self.assertEqual(unread_stream['stream_id'], get_stream('Denmark', user_profile.realm).id) + self.assertEqual(unread_stream['topic'], 'muted-topic') + self.assertEqual(unread_stream['unread_message_ids'], [muted_topic_message_id]) + + unread_stream = result['streams'][1] + self.assertEqual(unread_stream['stream_id'], get_stream('Denmark', user_profile.realm).id) self.assertEqual(unread_stream['topic'], 'test') self.assertEqual(unread_stream['unread_message_ids'], [stream_message_id]) - unread_stream = result['streams'][1] + unread_stream = result['streams'][2] self.assertEqual(unread_stream['stream_id'], get_stream('Muted Stream', user_profile.realm).id) self.assertEqual(unread_stream['topic'], 'test') self.assertEqual(unread_stream['unread_message_ids'], [muted_stream_message_id])