From 9d53f9ecaed095dd77ea9c843210c33db761cd05 Mon Sep 17 00:00:00 2001 From: SUBHASISH BEHERA Date: Sun, 19 Feb 2023 21:10:32 +0000 Subject: [PATCH] model/test_model: Notify user of alert-word. In the notify_user function a condition of "has_alert_word" is added which also checks if stream/topic is muted(unlike mentions,wild-card_mentions). Changed the existing test function test_notify_users_calling_msg_type. Added the case for notificaiton for all private messages. Apart from that added two parameters is_topic_muted and is_stream_muted. --- tests/model/test_model.py | 71 ++++++++++++++++++++++++++++++++++++--- zulipterminal/model.py | 7 ++++ 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/tests/model/test_model.py b/tests/model/test_model.py index aa5c9bf587..86701c3251 100644 --- a/tests/model/test_model.py +++ b/tests/model/test_model.py @@ -1978,6 +1978,8 @@ def test__update_topic_index( "vary_each_msg", "visual_notification_status", "types_when_notify_called", + "is_muted_stream", + "is_muted_topic", ], [ ( @@ -1985,11 +1987,52 @@ def test__update_topic_index( {"flags": ["mentioned", "wildcard_mentioned"]}, True, [], + None, + None, ), # message_fixture sender_id is 5140 - (5179, {"flags": ["mentioned"]}, False, ["stream", "private"]), - (5179, {"flags": ["wildcard_mentioned"]}, False, ["stream", "private"]), - (5179, {"flags": []}, True, ["stream", "private"]), - (5179, {"flags": []}, False, ["private"]), + (5179, {"flags": ["mentioned"]}, False, ["stream", "private"], None, None), + ( + 5179, + {"flags": ["wildcard_mentioned"]}, + False, + ["stream", "private"], + None, + None, + ), + (5179, {"flags": []}, True, ["stream", "private"], None, None), + (5179, {"flags": []}, False, ["private"], None, None), + ( + 5179, + {"flags": ["has_alert_word"]}, + False, + ["stream", "private"], + True, + True, + ), + ( + 5179, + {"flags": ["has_alert_word"]}, + False, + ["stream", "private"], + True, + False, + ), + ( + 5179, + {"flags": ["has_alert_word"]}, + False, + ["stream", "private"], + False, + True, + ), + ( + 5179, + {"flags": ["has_alert_word"]}, + False, + ["stream", "private"], + False, + False, + ), ], ids=[ "not_notified_since_self_message", @@ -1997,6 +2040,10 @@ def test__update_topic_index( "notified_stream_and_private_since_wildcard_mentioned", "notified_stream_since_stream_has_desktop_notifications", "notified_private_since_private_message", + "not_notified_for_stream_since_topic/stream_both_muted", + "not_notified_for_stream_since_stream_muted", + "not_notified_for_stream_since_topic_muted", + "notified_for_stream_since_topic/stream_both_not_muted", ], ) def test_notify_users_calling_msg_type( @@ -2008,6 +2055,8 @@ def test_notify_users_calling_msg_type( vary_each_msg, visual_notification_status, types_when_notify_called, + is_muted_stream: Optional[bool], + is_muted_topic: Optional[bool], ): message_fixture.update(vary_each_msg) model.user_id = user_id @@ -2015,6 +2064,8 @@ def test_notify_users_calling_msg_type( MODEL + ".is_visual_notifications_enabled", return_value=visual_notification_status, ) + mocker.patch.object(model, "is_muted_stream", return_value=is_muted_stream) + mocker.patch.object(model, "is_muted_topic", return_value=is_muted_topic) notify = mocker.patch(MODULE + ".notify") model.notify_user(message_fixture) @@ -2031,7 +2082,17 @@ def test_notify_users_calling_msg_type( if target is not None: title = f"Test Organization Name:\nFoo Foo (to {target})" # TODO: Test message content too? - notify.assert_called_once_with(title, mocker.ANY) + if message_fixture["type"] == "private": + notify.assert_called_once_with(title, mocker.ANY) + elif len( + vary_each_msg["flags"] + ) == 1 and "has_alert_word" in vary_each_msg.get("flags"): + if not is_muted_topic and not is_muted_stream: + notify.assert_called_once_with(title, mocker.ANY) + else: + notify.assert_not_called() + else: + notify.assert_called_once_with(title, mocker.ANY) else: notify.assert_not_called() diff --git a/zulipterminal/model.py b/zulipterminal/model.py index ac4cd1dea0..8a42e8c85c 100644 --- a/zulipterminal/model.py +++ b/zulipterminal/model.py @@ -1517,6 +1517,13 @@ def notify_user(self, message: Message) -> str: ) or self.is_visual_notifications_enabled(stream_id): recipient = "{display_recipient} -> {subject}".format(**message) + if "has_alert_word" in message["flags"]: + # Check if stream or topic is muted + topic = message.get("subject", "") + if not self.is_muted_stream(stream_id) and not self.is_muted_topic( + stream_id, topic + ): + recipient = "{display_recipient} -> {subject}".format(**message) if recipient: if hidden_content: text = content