Skip to content

Commit

Permalink
model/test_model: Notify user of alert-word.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Subhasish-Behera committed Sep 8, 2023
1 parent 3081df9 commit 9d53f9e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
71 changes: 66 additions & 5 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,25 +1978,72 @@ def test__update_topic_index(
"vary_each_msg",
"visual_notification_status",
"types_when_notify_called",
"is_muted_stream",
"is_muted_topic",
],
[
(
5140,
{"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",
"notified_stream_and_private_since_directly_mentioned",
"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(
Expand All @@ -2008,13 +2055,17 @@ 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
mocker.patch(
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)
Expand All @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9d53f9e

Please sign in to comment.