Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
url_encoding: Refactor a `encode_user_ids`.
PieterCK29d42ab6adbe
diff --git a/zerver/lib/url_encoding.py b/zerver/lib/url_encoding.pyindex 5b579fd4b39..0f28ac8cd3f 100644--- a/zerver/lib/url_encoding.py+++ b/zerver/lib/url_encoding.py@@ -41,6 +41,39 @@ def encode_channel(channel_id: int, channel_name: str, with_operator: bool = Falreturn encoded_channel+def encode_user_ids(+ user_ids: list[int],+ with_operator: bool = False,+) -> str:+ """+ This encodes the given `user_ids` into recipient slug+ string that can be used to construct a narrow URL.++ e.g., [13, 23, 9] -> "13,23,9-group"++ The `with_operator` parameter decides whether to append+ the "dm" operator to the recipient slug or not.++ e.g., "dm/13,23,9-group"++ """+ assert len(user_ids) > 0++ # For 3 or more user ids we use the "-group" decoration tag.+ # If we're only working with 1-2 user ids, it's either a+ # one-on-one direct message or direct message to ones self.+ # In this case, we don't include any decoration tag to the+ # slug.+ decoration_tag = ""+ if len(user_ids) >= 3:+ decoration_tag = "-group"++ direct_message_slug = ",".join([str(user_id) for user_id in sorted(user_ids)]) + decoration_tag+ if with_operator:+ return f"dm/{direct_message_slug}"+ return direct_message_slug++def personal_narrow_url(*, realm: Realm, sender: UserProfile) -> str:base_url = f"{realm.url}/#narrow/dm/"encoded_user_name = re2.sub(r'[ "%\/<>`\p{C}]+', "-", sender.full_name)@@ -53,9 +86,9 @@ def direct_message_group_narrow_url() -> str:realm = user.realmother_user_ids = [r["id"] for r in display_recipient if r["id"] != user.id]- pm_slug = ",".join(str(user_id) for user_id in sorted(other_user_ids)) + "-group"+ direct_message_slug = encode_user_ids(other_user_ids)base_url = f"{realm.url}/#narrow/dm/"- return base_url + pm_slug+ return base_url + direct_message_slugdef stream_narrow_url(realm: Realm, stream: Stream) -> str:@@ -124,17 +157,15 @@ def pm_message_url(with_or_near = "near"message_id = str(message["id"])- str_user_ids = [str(recipient["id"]) for recipient in message["display_recipient"]]+ user_ids = [recipient["id"] for recipient in message["display_recipient"]]- # Use the "perma-link" format here that includes the sender's- # user_id, so they're easier to share between people.- pm_str = ",".join(str_user_ids) + "-pm"+ direct_message_slug = encode_user_ids(user_ids)parts = [realm.url,"#narrow","dm",- pm_str,+ direct_message_slug,with_or_near,message_id,]diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 34c722359ac..fefe2e418d3 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -5652,11 +5652,11 @@ class PersonalMessagesTest(ZulipTestCase):realm=realm,message=message,)- self.assertEqual(url, "http://zulip.testserver/#narrow/dm/77,80-pm/near/555")+ self.assertEqual(url, "http://zulip.testserver/#narrow/dm/77,80/near/555")url = message_link_url(realm=realm,message=message,conversation_link=True,)- self.assertEqual(url, "http://zulip.testserver/#narrow/dm/77,80-pm/with/555")+ self.assertEqual(url, "http://zulip.testserver/#narrow/dm/77,80/with/555")diff --git a/zerver/tests/test_reminders.py b/zerver/tests/test_reminders.pyindex f1176deef32..2bdad673f2d 100644--- a/zerver/tests/test_reminders.py+++ b/zerver/tests/test_reminders.py@@ -56,7 +56,7 @@ class RemindersTest(ZulipTestCase):def get_dm_reminder_content(self, msg_content: str, msg_id: int) -> str:return ("You requested a reminder for the following direct message.\n\n"- f"@_**King Hamlet|10** [said](http://zulip.testserver/#narrow/dm/10,12-pm/near/{msg_id}):\n```quote\n{msg_content}\n```"+ f"@_**King Hamlet|10** [said](http://zulip.testserver/#narrow/dm/10,12/near/{msg_id}):\n```quote\n{msg_content}\n```")def get_channel_message_reminder_content(self, msg_content: str, msg_id: int) -> str:@@ -376,7 +376,7 @@ class RemindersTest(ZulipTestCase):delivered_message.content,"You requested a reminder for the following direct message.""\n\n"- f"@_**King Hamlet|10** [sent](http://zulip.testserver/#narrow/dm/10,12-pm/near/{reminder.reminder_target_message_id}) a poll.",+ f"@_**King Hamlet|10** [sent](http://zulip.testserver/#narrow/dm/10,12/near/{reminder.reminder_target_message_id}) a poll.",)self.assertEqual(delivered_message.date_sent, more_than_scheduled_delivery_datetime)@@ -411,6 +411,6 @@ class RemindersTest(ZulipTestCase):delivered_message.content,"You requested a reminder for the following direct message.""\n\n"- f"@_**King Hamlet|10** [sent](http://zulip.testserver/#narrow/dm/10,12-pm/near/{reminder.reminder_target_message_id}) a todo list.",+ f"@_**King Hamlet|10** [sent](http://zulip.testserver/#narrow/dm/10,12/near/{reminder.reminder_target_message_id}) a todo list.",)self.assertEqual(delivered_message.date_sent, more_than_scheduled_delivery_datetime)diff --git a/zerver/tests/test_url_encoding.py b/zerver/tests/test_url_encoding.pyindex bb8732580c5..2965b147b8f 100644--- a/zerver/tests/test_url_encoding.py+++ b/zerver/tests/test_url_encoding.py@@ -1,5 +1,5 @@from zerver.lib.test_classes import ZulipTestCase-from zerver.lib.url_encoding import encode_channel+from zerver.lib.url_encoding import encode_channel, encode_user_idsclass URLEncodeTest(ZulipTestCase):@@ -9,3 +9,18 @@ class URLEncodeTest(ZulipTestCase):self.assertEqual(encode_channel(123, "General"), "123-General")self.assertEqual(encode_channel(7, "random_channel"), "7-random_channel")self.assertEqual(encode_channel(9, "Verona", with_operator=True), "channel/9-Verona")++ def test_encode_user_ids(self) -> None:+ # Group narrow URL has 3 or more user IDs+ self.assertEqual(encode_user_ids([1, 2, 3]), "1,2,3-group")+ self.assertEqual(encode_user_ids([3, 1, 2]), "1,2,3-group")++ # One-on-one narrow URL has 2 user IDs+ self.assertEqual(encode_user_ids([1, 2]), "1,2")++ # Narrow URL to ones own direct message conversation+ self.assertEqual(encode_user_ids([1]), "1")++ self.assertEqual(encode_user_ids([1, 2, 3], with_operator=True), "dm/1,2,3-group")+ with self.assertRaises(AssertionError):+ encode_user_ids([])