Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify on alert words (#1301 rebase + tidy) #1499

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 107 additions & 24 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2089,50 +2089,120 @@ def test__update_topic_index(
"user_id",
"vary_each_msg",
"visual_notification_status",
"types_when_notify_called",
"is_muted_stream",
"is_muted_topic",
"expected_message_types_when_notify_called",
],
[
(
5140,
case(
5140, # message_fixture sender_id is 5140
{"flags": ["mentioned", "wildcard_mentioned"]},
True,
False,
False,
[],
), # 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"]),
],
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",
id="not_notified_since_self_message",
), # Even if mentioned, stream notifying, all unmuted
case(
5179,
{"flags": ["mentioned"]},
False,
True,
True,
["stream", "private"],
id="notified_stream_and_private_since_directly_mentioned",
), # Direct mention is overriding all (stream/topic) muting
case(
5179,
{"flags": ["wildcard_mentioned"]},
False,
True,
True,
["stream", "private"],
id="notified_stream_and_private_since_wildcard_mentioned",
), # Wildcard mention is overriding all (stream/topic) muting
case(
5179,
{"flags": []},
True,
True,
True,
["stream", "private"],
id="notified_stream_since_stream_has_desktop_notifications",
), # Stream setting is overriding all (stream/topic) muting
case(
5179,
{"flags": []},
False,
True,
True,
["private"],
id="notified_private_since_private_message",
), # No Stream setting so (stream/topic) muting limits streams
case(
5179,
{"flags": ["has_alert_word"]},
False,
True,
True,
["private"],
id="not_notified_for_stream_since_topic/stream_both_muted",
),
case(
5179,
{"flags": ["has_alert_word"]},
False,
True,
False,
["private"],
id="not_notified_for_stream_since_stream_muted",
),
case(
5179,
{"flags": ["has_alert_word"]},
False,
False,
True,
["private"],
id="not_notified_for_stream_since_topic_muted",
),
case(
5179,
{"flags": ["has_alert_word"]},
False,
False,
False,
["stream", "private"],
id="notified_for_stream_since_topic/stream_both_not_muted",
),
],
)
def test_notify_users_calling_msg_type(
def test_notify_user__calling_message_type(
self,
mocker,
model,
message_fixture,
user_id,
vary_each_msg,
visual_notification_status,
types_when_notify_called,
is_muted_stream: bool,
is_muted_topic: bool,
expected_message_types_when_notify_called,
):
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)

target = None
if message_fixture["type"] in types_when_notify_called:
if message_fixture["type"] in expected_message_types_when_notify_called:
who = message_fixture["type"]
if who == "stream":
target = "PTEST -> Test"
Expand Down Expand Up @@ -2178,7 +2248,7 @@ def test_notify_users_calling_msg_type(
),
],
)
def test_notify_user_transformed_content(
def test_notify_user__transformed_content(
self, mocker, model, message_fixture, content, expected_notification_text
):
mocker.patch(MODEL + ".is_visual_notifications_enabled", lambda s, id: True)
Expand All @@ -2192,25 +2262,38 @@ def test_notify_user_transformed_content(
@pytest.mark.parametrize(
"notify_enabled, is_notify_called",
[
(True, True),
(False, False),
case(True, True, id="notify_enabled:notify_called"),
case(False, False, id="notify_disabled:notify_not_called"),
],
)
def test_notify_users_enabled(
def test_notify_user__enabled(
self, mocker, model, message_fixture, notify_enabled, is_notify_called
):
message_fixture.update({"sender_id": 2, "flags": ["mentioned"]})
model.controller.notify_enabled = notify_enabled
model.user_id = 1
notify = mocker.patch(MODULE + ".notify")

model.notify_user(message_fixture)

assert notify.called == is_notify_called

@pytest.mark.parametrize(
"hide_content, expected_content",
[(True, "New direct message from Foo Foo"), (False, "private content here.")],
[
case(
True,
"New direct message from Foo Foo",
id="PM_message_content_visible_in_notification",
),
case(
False,
"private content here.",
id="PM_message_content_hidden_in_notification",
),
],
)
def test_notify_users_hides_PM_content_based_on_user_setting(
def test_notify_user__hides_PM_content_based_on_user_setting(
self, mocker, model, private_message_fixture, hide_content, expected_content
):
notify = mocker.patch(MODULE + ".notify")
Expand Down
19 changes: 16 additions & 3 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,22 @@ def notify_user(self, message: Message) -> str:
hidden_content = True
elif message["type"] == "stream":
stream_id = message["stream_id"]
if {"mentioned", "wildcard_mentioned"}.intersection(
set(message["flags"])
) or self.is_visual_notifications_enabled(stream_id):
topic = message.get("subject", "")

contains_mention = set(message["flags"]).intersection(
{"mentioned", "wildcard_mentioned"}
)
stream_settings_notify = self.is_visual_notifications_enabled(stream_id)
contains_alert_word_and_stream_topic_not_muted = (
"has_alert_word" in message["flags"]
and not self.is_muted_stream(stream_id)
and not self.is_muted_topic(stream_id, topic)
)
if (
contains_mention
or stream_settings_notify
or contains_alert_word_and_stream_topic_not_muted
):
recipient = "{display_recipient} -> {subject}".format(**message)

if recipient:
Expand Down
Loading