Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

email_notifications: Show mentioned user group in email notifications.

Hashir Sarwar87d90f47ee57
diff --git a/templates/zerver/emails/missed_message.source.html b/templates/zerver/emails/missed_message.source.html
index 45679b186dd..12fb27fe5ad 100644
--- a/templates/zerver/emails/missed_message.source.html
+++ b/templates/zerver/emails/missed_message.source.html
@@ -26,7 +26,11 @@
<div class="email-preferences">
&mdash;<br />
{% if mention %}
+ {% if mentioned_user_group_name %}
+ {% trans %}You are receiving this because @{{ mentioned_user_group_name }} was mentioned in {{ realm_name }}.{% endtrans %}<br />
+ {% else %}
{% trans %}You are receiving this because you were mentioned in {{ realm_name }}.{% endtrans %}<br />
+ {% endif %}
{% elif stream_email_notify %}
{% trans %}You are receiving this because you have email notifications enabled for this stream.{% endtrans %}<br />
{% endif %}
diff --git a/templates/zerver/emails/missed_message.txt b/templates/zerver/emails/missed_message.txt
index 37b9eff0cc2..ccf6243f31e 100644
--- a/templates/zerver/emails/missed_message.txt
+++ b/templates/zerver/emails/missed_message.txt
@@ -22,7 +22,11 @@ See {{ alert_notif_url }} for more details.
--
{% if mention %}
+{% if mentioned_user_group_name %}
+{% trans %}You are receiving this because @{{ mentioned_user_group_name }} was mentioned in {{ realm_name }}.{% endtrans %}
+{% else %}
{% trans %}You are receiving this because you were mentioned in {{ realm_name }}.{% endtrans %}
+{% endif %}
{% elif stream_email_notify %}
{% trans %}You are receiving this because you have email notifications enabled for this stream.{% endtrans %}
{% endif %}
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.py
index 043077ec9d5..b74feecaa4e 100644
--- a/zerver/lib/email_notifications.py
+++ b/zerver/lib/email_notifications.py
@@ -1,5 +1,6 @@
# See https://zulip.readthedocs.io/en/latest/subsystems/notifications.html
+import math
import re
from collections import defaultdict
from datetime import timedelta
@@ -30,6 +31,7 @@ from zerver.lib.url_encoding import (
stream_narrow_url,
topic_narrow_url,
)
+from zerver.lib.user_groups import access_user_group_by_id, get_user_group_members
from zerver.models import (
Message,
Recipient,
@@ -354,6 +356,45 @@ def get_narrow_url(
return topic_narrow_url(user_profile.realm, stream, message.topic_name())
+def get_mentioned_user_group_name(
+ messages: List[Dict[str, Any]], user_profile: UserProfile
+) -> Optional[str]:
+ """Returns the user group name to display in the email notification
+ if user group(s) are mentioned.
+
+ This implements the same algorithm as get_user_group_mentions_data
+ in zerver/lib/notification_data.py, but we're passed a list of
+ messages instead.
+ """
+ for message in messages:
+ if message["mentioned_user_group_id"] is None and message["trigger"] == "mentioned":
+ # The user has also been personally mentioned, so that gets prioritized.
+ return None
+
+ # These IDs are those of the smallest user groups mentioned in each message.
+ mentioned_user_group_ids = [
+ message["mentioned_user_group_id"]
+ for message in messages
+ if message["mentioned_user_group_id"] is not None
+ ]
+
+ # We now want to calculate the name of the smallest user group mentioned among
+ # all these messages.
+ smallest_user_group_size = math.inf
+ smallest_user_group_name = None
+ for user_group_id in mentioned_user_group_ids:
+ current_user_group = access_user_group_by_id(user_group_id, user_profile)
+ current_user_group_size = len(get_user_group_members(current_user_group))
+
+ if current_user_group_size < smallest_user_group_size:
+ # If multiple user groups are mentioned, we prefer the
+ # user group with the least members.
+ smallest_user_group_size = current_user_group_size
+ smallest_user_group_name = current_user_group.name
+
+ return smallest_user_group_name
+
+
def message_content_allowed_in_missedmessage_emails(user_profile: UserProfile) -> bool:
return (
user_profile.realm.message_content_allowed_in_email_notifications
@@ -398,12 +439,15 @@ def do_send_missedmessage_events_reply_in_zulip(
realm_name_in_notifications=user_profile.realm_name_in_notifications,
)
+ mentioned_user_group_name = get_mentioned_user_group_name(missed_messages, user_profile)
triggers = [message["trigger"] for message in missed_messages]
unique_triggers = set(triggers)
+
context.update(
mention="mentioned" in unique_triggers or "wildcard_mentioned" in unique_triggers,
stream_email_notify="stream_email_notify" in unique_triggers,
mention_count=triggers.count("mentioned") + triggers.count("wildcard_mentioned"),
+ mentioned_user_group_name=mentioned_user_group_name,
)
# If this setting (email mirroring integration) is enabled, only then
@@ -532,7 +576,13 @@ def do_send_missedmessage_events_reply_in_zulip(
def handle_missedmessage_emails(
user_profile_id: int, missed_email_events: Iterable[Dict[str, Any]]
) -> None:
- message_ids = {event.get("message_id"): event.get("trigger") for event in missed_email_events}
+ message_ids = {
+ event.get("message_id"): {
+ "trigger": event.get("trigger"),
+ "mentioned_user_group_id": event.get("mentioned_user_group_id"),
+ }
+ for event in missed_email_events
+ }
user_profile = get_user_profile_by_id(user_profile_id)
if not receives_offline_email_notifications(user_profile):
@@ -587,9 +637,13 @@ def handle_missedmessage_emails(
for bucket_tup, ignored_max_id in bucket_tups:
unique_messages = {}
for m in messages_by_bucket[bucket_tup]:
+ message_info = message_ids.get(m.id)
unique_messages[m.id] = dict(
message=m,
- trigger=message_ids.get(m.id),
+ trigger=message_info["trigger"] if message_info else None,
+ mentioned_user_group_id=message_info.get("mentioned_user_group_id")
+ if message_info is not None
+ else None,
)
do_send_missedmessage_events_reply_in_zulip(
user_profile,
diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py
index 11c8774ea84..a2090938fe7 100644
--- a/zerver/tests/test_email_notifications.py
+++ b/zerver/tests/test_email_notifications.py
@@ -1,7 +1,7 @@
import random
import re
from email.headerregistry import Address
-from typing import List, Sequence
+from typing import List, Optional, Sequence
from unittest import mock
from unittest.mock import patch
@@ -22,6 +22,7 @@ from zerver.lib.email_notifications import (
)
from zerver.lib.send_email import FromAddress, send_custom_email
from zerver.lib.test_classes import ZulipTestCase
+from zerver.lib.user_groups import create_user_group
from zerver.models import (
ScheduledEmail,
UserMessage,
@@ -375,12 +376,22 @@ class TestMissedMessages(ZulipTestCase):
show_message_content: bool = True,
verify_body_does_not_include: Sequence[str] = [],
trigger: str = "",
+ mentioned_user_group_id: Optional[int] = None,
) -> None:
othello = self.example_user("othello")
hamlet = self.example_user("hamlet")
tokens = self._get_tokens()
with patch("zerver.lib.email_mirror.generate_missed_message_token", side_effect=tokens):
- handle_missedmessage_emails(hamlet.id, [{"message_id": msg_id, "trigger": trigger}])
+ handle_missedmessage_emails(
+ hamlet.id,
+ [
+ {
+ "message_id": msg_id,
+ "trigger": trigger,
+ "mentioned_user_group_id": mentioned_user_group_id,
+ }
+ ],
+ )
if settings.EMAIL_GATEWAY_PATTERN != "":
reply_to_addresses = [settings.EMAIL_GATEWAY_PATTERN % (t,) for t in tokens]
reply_to_emails = [
@@ -741,6 +752,85 @@ class TestMissedMessages(ZulipTestCase):
handle_missedmessage_emails(iago.id, [{"message_id": msg_id}])
self.assert_length(mail.outbox, 0)
+ def test_smaller_user_group_mention_priority(self) -> None:
+ hamlet = self.example_user("hamlet")
+ othello = self.example_user("othello")
+ cordelia = self.example_user("cordelia")
+
+ hamlet_only = create_user_group("hamlet_only", [hamlet], get_realm("zulip"))
+ hamlet_and_cordelia = create_user_group(
+ "hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip")
+ )
+
+ hamlet_only_message_id = self.send_stream_message(othello, "Denmark", "@*hamlet_only*")
+ hamlet_and_cordelia_message_id = self.send_stream_message(
+ othello, "Denmark", "@*hamlet_and_cordelia*"
+ )
+
+ handle_missedmessage_emails(
+ hamlet.id,
+ [
+ {
+ "message_id": hamlet_only_message_id,
+ "trigger": "mentioned",
+ "mentioned_user_group_id": hamlet_only.id,
+ },
+ {
+ "message_id": hamlet_and_cordelia_message_id,
+ "trigger": "mentioned",
+ "mentioned_user_group_id": hamlet_and_cordelia.id,
+ },
+ ],
+ )
+
+ expected_email_include = [
+ "Othello, the Moor of Venice: @*hamlet_only* @*hamlet_and_cordelia* -- ",
+ "You are receiving this because @hamlet_only was mentioned in Zulip Dev.",
+ ]
+
+ for text in expected_email_include:
+ self.assertIn(text, self.normalize_string(mail.outbox[0].body))
+
+ def test_personal_over_user_group_mention_priority(self) -> None:
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ othello = self.example_user("othello")
+
+ hamlet_and_cordelia = create_user_group(
+ "hamlet_and_cordelia", [hamlet, cordelia], get_realm("zulip")
+ )
+
+ user_group_mentioned_message_id = self.send_stream_message(
+ othello, "Denmark", "@*hamlet_and_cordelia*"
+ )
+ personal_mentioned_message_id = self.send_stream_message(
+ othello, "Denmark", "@**King Hamlet**"
+ )
+
+ handle_missedmessage_emails(
+ hamlet.id,
+ [
+ {
+ "message_id": user_group_mentioned_message_id,
+ "trigger": "mentioned",
+ "mentioned_user_group_id": hamlet_and_cordelia.id,
+ },
+ {
+ "message_id": personal_mentioned_message_id,
+ "trigger": "mentioned",
+ "mentioned_user_group_id": None,
+ },
+ ],
+ )
+
+ expected_email_include = [
+ "Othello, the Moor of Venice: @*hamlet_and_cordelia* @**King Hamlet** -- ",
+ "You are receiving this because you were mentioned in Zulip Dev.",
+ ]
+
+ for text in expected_email_include:
+ self.assertIn(text, self.normalize_string(mail.outbox[0].body))
+
def test_realm_name_in_notifications(self) -> None:
# Test with realm_name_in_notifications for hamlet disabled.
self._realm_name_in_missed_message_email_subject(False)