narrow urls: Avoid complicated optional types.

We no longer have to reason about the 12 possible
ways of invoking get_narrow_url. We also avoid
double computation in a couple places.

Finally, we get stricter type checks by just inlining
the calls.
This commit is contained in:
Steve Howell
2023-08-10 14:50:04 +00:00
committed by Tim Abbott
parent 6ff7c17f82
commit 257b32a4a4
2 changed files with 37 additions and 48 deletions

View File

@@ -4,6 +4,7 @@ from urllib.parse import quote, urlsplit
import re2
from zerver.lib.topic import get_topic_from_message_info
from zerver.lib.types import UserDisplayRecipient
from zerver.models import Realm, Stream, UserProfile
@@ -20,14 +21,16 @@ def encode_stream(stream_id: int, stream_name: str) -> str:
return str(stream_id) + "-" + hash_util_encode(stream_name)
def personal_narrow_url(realm: Realm, sender: UserProfile) -> str:
def personal_narrow_url(*, realm: Realm, sender: UserProfile) -> str:
base_url = f"{realm.uri}/#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
def huddle_narrow_url(realm: Realm, other_user_ids: List[int]) -> str:
def huddle_narrow_url(*, user: UserProfile, display_recipient: List[UserDisplayRecipient]) -> str:
realm = user.realm
other_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"
base_url = f"{realm.uri}/#narrow/dm/"
return base_url + pm_slug
@@ -38,7 +41,7 @@ def stream_narrow_url(realm: Realm, stream: Stream) -> str:
return base_url + encode_stream(stream.id, stream.name)
def topic_narrow_url(realm: Realm, stream: Stream, topic: str) -> str:
def topic_narrow_url(*, realm: Realm, stream: Stream, topic: str) -> str:
base_url = f"{realm.uri}/#narrow/stream/"
return f"{base_url}{encode_stream(stream.id, stream.name)}/topic/{hash_util_encode(topic)}"