Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
notifications: Move user group mentions helpers together.
Zixuan James Lid8101de34d3a
diff --git a/zerver/lib/email_notifications.py b/zerver/lib/email_notifications.pyindex 1b6a2d7954c..2cdb92f5830 100644--- a/zerver/lib/email_notifications.py+++ b/zerver/lib/email_notifications.py@@ -1,7 +1,6 @@# See https://zulip.readthedocs.io/en/latest/subsystems/notifications.htmlimport logging-import mathimport refrom collections import defaultdictfrom datetime import timedelta@@ -23,6 +22,7 @@ from confirmation.models import one_click_unsubscribe_linkfrom zerver.decorator import statsd_incrementfrom zerver.lib.markdown.fenced_code import FENCE_REfrom zerver.lib.message import bulk_access_messages+from zerver.lib.notification_data import get_mentioned_user_group_namefrom zerver.lib.queue import queue_json_publishfrom zerver.lib.send_email import FromAddress, send_future_emailfrom zerver.lib.types import DisplayRecipientT@@ -32,12 +32,10 @@ from zerver.lib.url_encoding import (stream_narrow_url,topic_narrow_url,)-from zerver.lib.user_groups import get_user_group_direct_member_idsfrom zerver.models import (Message,Recipient,Stream,- UserGroup,UserMessage,UserProfile,get_context_for_message,@@ -344,45 +342,6 @@ 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 = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)- current_user_group_size = len(get_user_group_direct_member_ids(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_notificationsdiff --git a/zerver/lib/notification_data.py b/zerver/lib/notification_data.pyindex a789fc768a7..54b14e55f38 100644--- a/zerver/lib/notification_data.py+++ b/zerver/lib/notification_data.py@@ -1,8 +1,10 @@+import mathfrom dataclasses import dataclass-from typing import Collection, Dict, List, Optional, Set+from typing import Any, Collection, Dict, List, Optional, Setfrom zerver.lib.mention import MentionData-from zerver.models import NotificationTriggers+from zerver.lib.user_groups import get_user_group_direct_member_ids+from zerver.models import NotificationTriggers, UserGroup, UserProfile@dataclass@@ -187,3 +189,42 @@ def get_user_group_mentions_data(mentioned_user_groups_map[member_id] = group_idreturn mentioned_user_groups_map+++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 = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)+ current_user_group_size = len(get_user_group_direct_member_ids(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