2024-06-17 16:24:50 +02:00
|
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
|
|
def is_same_server_message_link(url: str) -> bool:
|
|
|
|
split_result = urlsplit(url)
|
|
|
|
hostname = split_result.hostname
|
|
|
|
fragment = split_result.fragment
|
|
|
|
|
|
|
|
if hostname not in {None, settings.EXTERNAL_HOST_WITHOUT_PORT}:
|
|
|
|
return False
|
|
|
|
|
2024-10-04 16:54:16 +02:00
|
|
|
# A message link always has category `narrow`, section `channel`
|
2024-06-17 16:24:50 +02:00
|
|
|
# or `dm`, and ends with `/near/<message_id>`, where <message_id>
|
|
|
|
# is a sequence of digits. The URL fragment of a message link has
|
|
|
|
# at least 5 parts. e.g. '#narrow/dm/9,15-dm/near/43'
|
|
|
|
fragment_parts = fragment.split("/")
|
|
|
|
if len(fragment_parts) < 5:
|
|
|
|
return False
|
|
|
|
|
|
|
|
category = fragment_parts[0]
|
|
|
|
section = fragment_parts[1]
|
|
|
|
ends_with_near_message_id = fragment_parts[-2] == "near" and fragment_parts[-1].isdigit()
|
|
|
|
|
2024-10-04 16:54:16 +02:00
|
|
|
return category == "narrow" and section in {"channel", "dm"} and ends_with_near_message_id
|