url_encoding: Refactor a encode_user_full_name_and_id.

This refactors out a function that encode user name and user id into URL
compatible format.
This commit is contained in:
PieterCK
2025-06-26 20:53:08 +07:00
committed by Tim Abbott
parent 29d42ab6ad
commit 75f2e27500
2 changed files with 40 additions and 4 deletions

View File

@@ -74,11 +74,30 @@ def encode_user_ids(
return direct_message_slug
def encode_user_full_name_and_id(full_name: str, user_id: int, with_operator: bool = False) -> str:
"""
This encodes the given `full_name` and `user_id` into a
recipient slug string that can be used to construct a
narrow URL.
e.g., 9, "King Hamlet" -> "9-King-Hamlet"
The `with_operator` parameter decides whether to append
the "dm" operator to the recipient slug or not.
e.g., "dm/9-King-Hamlet"
"""
encoded_user_name = re2.sub(r'[ "%\/<>`\p{C}]+', "-", full_name.strip())
direct_message_slug = str(user_id) + "-" + encoded_user_name
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)
pm_slug = str(sender.id) + "-" + encoded_user_name
return base_url + pm_slug
direct_message_slug = encode_user_full_name_and_id(sender.full_name, sender.id)
return base_url + direct_message_slug
def direct_message_group_narrow_url(