stream notifications: Add helper for silent user mention syntax.

In many of our stream notification messages, we make use of the
same silent user mention syntax, the template for which was always
hardcoded. This commit adds a helper function that all relevant
callers can call to get the right syntax when mentioning users.

Thanks to Tim Abbott for this suggestion!
This commit is contained in:
Eeshan Garg
2021-12-07 15:41:45 -05:00
committed by Tim Abbott
parent 8ebe05f644
commit 3714a30e63
3 changed files with 13 additions and 8 deletions

View File

@@ -100,7 +100,7 @@ from zerver.lib.hotspots import get_next_hotspots
from zerver.lib.i18n import get_language_name from zerver.lib.i18n import get_language_name
from zerver.lib.markdown import MessageRenderingResult, topic_links from zerver.lib.markdown import MessageRenderingResult, topic_links
from zerver.lib.markdown import version as markdown_version from zerver.lib.markdown import version as markdown_version
from zerver.lib.mention import MentionData from zerver.lib.mention import MentionData, silent_mention_syntax_for_user
from zerver.lib.message import ( from zerver.lib.message import (
MessageDict, MessageDict,
SendMessageRequest, SendMessageRequest,
@@ -376,7 +376,7 @@ def notify_new_user(user_profile: UserProfile) -> None:
is_first_user = user_count == 1 is_first_user = user_count == 1
if not is_first_user: if not is_first_user:
message = _("{user} just signed up for Zulip. (total: {user_count})").format( message = _("{user} just signed up for Zulip. (total: {user_count})").format(
user=f"@_**{user_profile.full_name}|{user_profile.id}**", user_count=user_count user=silent_mention_syntax_for_user(user_profile), user_count=user_count
) )
if settings.BILLING_ENABLED: if settings.BILLING_ENABLED:
@@ -4989,7 +4989,7 @@ def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) -
stream, stream,
Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, Realm.STREAM_EVENTS_NOTIFICATION_TOPIC,
_("{user_name} renamed stream {old_stream_name} to {new_stream_name}.").format( _("{user_name} renamed stream {old_stream_name} to {new_stream_name}.").format(
user_name=f"@_**{user_profile.full_name}|{user_profile.id}**", user_name=silent_mention_syntax_for_user(user_profile),
old_stream_name=f"**{old_name}**", old_stream_name=f"**{old_name}**",
new_stream_name=f"**{new_name}**", new_stream_name=f"**{new_name}**",
), ),
@@ -5020,7 +5020,7 @@ def send_change_stream_message_retention_days_notification(
user_profile: UserProfile, stream: Stream, old_value: Optional[int], new_value: Optional[int] user_profile: UserProfile, stream: Stream, old_value: Optional[int], new_value: Optional[int]
) -> None: ) -> None:
sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id) sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id)
user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**" user_mention = silent_mention_syntax_for_user(user_profile)
# If switching from or to the organization's default retention policy, # If switching from or to the organization's default retention policy,
# we want to take the realm's default into account. # we want to take the realm's default into account.
@@ -6048,7 +6048,7 @@ def maybe_send_resolve_topic_notifications(
return return
sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id) sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id)
user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**" user_mention = silent_mention_syntax_for_user(user_profile)
with override_language(stream.realm.default_language): with override_language(stream.realm.default_language):
if topic_resolved: if topic_resolved:
notification_string = _("{user} has marked this topic as resolved.") notification_string = _("{user} has marked this topic as resolved.")
@@ -6082,7 +6082,7 @@ def send_message_moved_breadcrumbs(
if new_topic is None: if new_topic is None:
new_topic = old_topic new_topic = old_topic
user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**" user_mention = silent_mention_syntax_for_user(user_profile)
old_topic_link = f"#**{old_stream.name}>{old_topic}**" old_topic_link = f"#**{old_stream.name}>{old_topic}**"
new_topic_link = f"#**{new_stream.name}>{new_topic}**" new_topic_link = f"#**{new_stream.name}>{new_topic}**"

View File

@@ -152,3 +152,7 @@ def get_stream_name_info(realm: Realm, stream_names: Set[str]) -> Dict[str, Full
dct = {row["name"]: row for row in rows} dct = {row["name"]: row for row in rows}
return dct return dct
def silent_mention_syntax_for_user(user_profile: UserProfile) -> str:
return f"@_**{user_profile.full_name}|{user_profile.id}**"

View File

@@ -50,6 +50,7 @@ from zerver.lib.exceptions import (
OrganizationOwnerRequired, OrganizationOwnerRequired,
ResourceNotFoundError, ResourceNotFoundError,
) )
from zerver.lib.mention import silent_mention_syntax_for_user
from zerver.lib.request import REQ, has_request_variables from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.retention import parse_message_retention_days from zerver.lib.retention import parse_message_retention_days
@@ -649,7 +650,7 @@ def send_messages_for_new_subscribers(
topic = _("new streams") topic = _("new streams")
content = content.format( content = content.format(
user_name=f"@_**{user_profile.full_name}|{user_profile.id}**", user_name=silent_mention_syntax_for_user(user_profile),
stream_str=", ".join(f"#**{s.name}**" for s in created_streams), stream_str=", ".join(f"#**{s.name}**" for s in created_streams),
) )
@@ -674,7 +675,7 @@ def send_messages_for_new_subscribers(
stream=stream, stream=stream,
topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC, topic=Realm.STREAM_EVENTS_NOTIFICATION_TOPIC,
content=_("Stream created by {user_name}.").format( content=_("Stream created by {user_name}.").format(
user_name=f"@_**{user_profile.full_name}|{user_profile.id}**", user_name=silent_mention_syntax_for_user(user_profile),
), ),
), ),
) )