2018-10-27 16:37:29 +02:00
|
|
|
from typing import Any, Dict, List
|
2021-10-14 01:45:34 +02:00
|
|
|
from urllib.parse import quote, urlsplit
|
2018-07-29 00:05:45 +02:00
|
|
|
|
2018-11-10 22:50:28 +01:00
|
|
|
from zerver.lib.topic import get_topic_from_message_info
|
2018-11-12 15:04:03 +01:00
|
|
|
from zerver.models import Realm, Stream, UserProfile
|
2018-07-29 00:05:45 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2018-07-29 00:05:45 +02:00
|
|
|
def hash_util_encode(string: str) -> str:
|
2022-02-25 19:34:01 +01:00
|
|
|
# Do the same encoding operation as shared internal_url.encodeHashComponent
|
|
|
|
# on the frontend.
|
2018-07-29 00:05:45 +02:00
|
|
|
# `safe` has a default value of "/", but we want those encoded, too.
|
2021-10-14 01:45:34 +02:00
|
|
|
return quote(string, safe=b"").replace(".", "%2E").replace("%", ".")
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-07-29 00:05:45 +02:00
|
|
|
|
|
|
|
def encode_stream(stream_id: int, stream_name: str) -> str:
|
|
|
|
# We encode streams for urls as something like 99-Verona.
|
2021-02-12 08:20:45 +01:00
|
|
|
stream_name = stream_name.replace(" ", "-")
|
|
|
|
return str(stream_id) + "-" + hash_util_encode(stream_name)
|
2018-07-29 00:05:45 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-12 15:04:03 +01:00
|
|
|
def personal_narrow_url(realm: Realm, sender: UserProfile) -> str:
|
2020-06-10 06:41:04 +02:00
|
|
|
base_url = f"{realm.uri}/#narrow/pm-with/"
|
2021-02-12 08:20:45 +01:00
|
|
|
email_user = sender.email.split("@")[0].lower()
|
|
|
|
pm_slug = str(sender.id) + "-" + hash_util_encode(email_user)
|
2018-11-12 15:04:03 +01:00
|
|
|
return base_url + pm_slug
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-11-12 15:51:37 +01:00
|
|
|
def huddle_narrow_url(realm: Realm, other_user_ids: List[int]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
pm_slug = ",".join(str(user_id) for user_id in sorted(other_user_ids)) + "-group"
|
2020-06-10 06:41:04 +02:00
|
|
|
base_url = f"{realm.uri}/#narrow/pm-with/"
|
2018-11-12 15:51:37 +01:00
|
|
|
return base_url + pm_slug
|
2018-07-29 00:05:45 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-07-29 00:05:45 +02:00
|
|
|
def stream_narrow_url(realm: Realm, stream: Stream) -> str:
|
2020-06-10 06:41:04 +02:00
|
|
|
base_url = f"{realm.uri}/#narrow/stream/"
|
2018-07-29 00:05:45 +02:00
|
|
|
return base_url + encode_stream(stream.id, stream.name)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-07-29 00:05:45 +02:00
|
|
|
def topic_narrow_url(realm: Realm, stream: Stream, topic: str) -> str:
|
2020-06-10 06:41:04 +02:00
|
|
|
base_url = f"{realm.uri}/#narrow/stream/"
|
|
|
|
return f"{base_url}{encode_stream(stream.id, stream.name)}/topic/{hash_util_encode(topic)}"
|
2018-10-27 16:37:29 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def near_message_url(realm: Realm, message: Dict[str, Any]) -> str:
|
2018-10-27 16:37:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if message["type"] == "stream":
|
2018-10-27 16:37:29 +02:00
|
|
|
url = near_stream_message_url(
|
|
|
|
realm=realm,
|
|
|
|
message=message,
|
|
|
|
)
|
|
|
|
return url
|
|
|
|
|
|
|
|
url = near_pm_message_url(
|
|
|
|
realm=realm,
|
|
|
|
message=message,
|
|
|
|
)
|
|
|
|
return url
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def near_stream_message_url(realm: Realm, message: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
message_id = str(message["id"])
|
|
|
|
stream_id = message["stream_id"]
|
|
|
|
stream_name = message["display_recipient"]
|
2018-11-10 22:50:28 +01:00
|
|
|
topic_name = get_topic_from_message_info(message)
|
2018-10-27 16:37:29 +02:00
|
|
|
encoded_topic = hash_util_encode(topic_name)
|
|
|
|
encoded_stream = encode_stream(stream_id=stream_id, stream_name=stream_name)
|
|
|
|
|
|
|
|
parts = [
|
|
|
|
realm.uri,
|
2021-02-12 08:20:45 +01:00
|
|
|
"#narrow",
|
|
|
|
"stream",
|
2018-10-27 16:37:29 +02:00
|
|
|
encoded_stream,
|
2021-02-12 08:20:45 +01:00
|
|
|
"topic",
|
2018-10-27 16:37:29 +02:00
|
|
|
encoded_topic,
|
2021-02-12 08:20:45 +01:00
|
|
|
"near",
|
2018-10-27 16:37:29 +02:00
|
|
|
message_id,
|
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
full_url = "/".join(parts)
|
2018-10-27 16:37:29 +02:00
|
|
|
return full_url
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def near_pm_message_url(realm: Realm, message: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
message_id = str(message["id"])
|
|
|
|
str_user_ids = [str(recipient["id"]) for recipient in message["display_recipient"]]
|
2018-10-27 16:37:29 +02:00
|
|
|
|
|
|
|
# Use the "perma-link" format here that includes the sender's
|
|
|
|
# user_id, so they're easier to share between people.
|
2021-02-12 08:20:45 +01:00
|
|
|
pm_str = ",".join(str_user_ids) + "-pm"
|
2018-10-27 16:37:29 +02:00
|
|
|
|
|
|
|
parts = [
|
|
|
|
realm.uri,
|
2021-02-12 08:20:45 +01:00
|
|
|
"#narrow",
|
|
|
|
"pm-with",
|
2018-10-27 16:37:29 +02:00
|
|
|
pm_str,
|
2021-02-12 08:20:45 +01:00
|
|
|
"near",
|
2018-10-27 16:37:29 +02:00
|
|
|
message_id,
|
|
|
|
]
|
2021-02-12 08:20:45 +01:00
|
|
|
full_url = "/".join(parts)
|
2018-10-27 16:37:29 +02:00
|
|
|
return full_url
|
2020-03-02 19:38:16 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-10-14 01:45:34 +02:00
|
|
|
def append_url_query_string(original_url: str, query: str) -> str:
|
|
|
|
u = urlsplit(original_url)
|
|
|
|
query = u.query + ("&" if u.query and query else "") + query
|
|
|
|
return u._replace(query=query).geturl()
|