diff --git a/zerver/lib/url_encoding.py b/zerver/lib/url_encoding.py index ffb60ab7e3..a217e35ec9 100644 --- a/zerver/lib/url_encoding.py +++ b/zerver/lib/url_encoding.py @@ -1,7 +1,8 @@ -from email.headerregistry import Address from typing import Any, Dict, List from urllib.parse import quote, urlsplit +import re2 + from zerver.lib.topic import get_topic_from_message_info from zerver.models import Realm, Stream, UserProfile @@ -21,8 +22,8 @@ def encode_stream(stream_id: int, stream_name: str) -> str: def personal_narrow_url(realm: Realm, sender: UserProfile) -> str: base_url = f"{realm.uri}/#narrow/pm-with/" - email_user = Address(addr_spec=sender.email).username.lower() - pm_slug = str(sender.id) + "-" + hash_util_encode(email_user) + encoded_user_name = re2.sub(r'[ "%\/<>`\p{C}]+', "-", sender.full_name) + pm_slug = str(sender.id) + "-" + encoded_user_name return base_url + pm_slug diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py index 5e9e6b2e66..8c0e9712ee 100644 --- a/zerver/tests/test_email_notifications.py +++ b/zerver/tests/test_email_notifications.py @@ -1183,6 +1183,21 @@ class TestMissedMessages(ZulipTestCase): msg_id, verify_body_include, email_subject, send_as_user=False, verify_html_body=True ) + def test_pm_link_in_missed_message_header(self) -> None: + cordelia = self.example_user("cordelia") + msg_id = self.send_personal_message( + cordelia, + self.example_user("hamlet"), + "Let's test PM link in email notifications", + ) + + encoded_name = "Cordelia,-Lear's-daughter" + verify_body_include = [ + f"view it in Zulip Dev Zulip: http://zulip.testserver/#narrow/pm-with/{cordelia.id}-{encoded_name}" + ] + email_subject = "PMs with Cordelia, Lear's daughter" + self._test_cases(msg_id, verify_body_include, email_subject, send_as_user=False) + def test_sender_name_in_missed_message(self) -> None: hamlet = self.example_user("hamlet") msg_id_1 = self.send_stream_message(