Skip to content

Commit

Permalink
helper: Introduce near_message_url to generate message links.
Browse files Browse the repository at this point in the history
This introduces a set of helper functions along with near_message_url to
generate message links.

The logic is borrowed from Zulip's zerver/lib/url_encoding.py.
  • Loading branch information
preetmishra committed Jul 31, 2020
1 parent 2e3d02f commit a7e4b4c
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import platform
import subprocess
import time
import urllib
from collections import OrderedDict, defaultdict
from contextlib import contextmanager
from functools import wraps
Expand Down Expand Up @@ -637,3 +638,63 @@ def suppress_output() -> Iterator[None]:
finally:
os.dup2(out, 1)
os.dup2(err, 2)


def hash_util_encode(string: str) -> str:
return urllib.parse.quote(
string.encode('utf-8'), safe=b'').replace('.', '%2E').replace('%', '.')


def encode_stream(stream_id: int, stream_name: str) -> str:
stream_name = stream_name.replace(' ', '-')
return str(stream_id) + '-' + hash_util_encode(stream_name)


def near_stream_message_url(server_url: str, message: Message) -> str:
message_id = str(message['id'])
stream_id = message['stream_id']
stream_name = message['display_recipient']
topic_name = message['subject']
encoded_stream = encode_stream(stream_id, stream_name)
encoded_topic = hash_util_encode(topic_name)

parts = [
server_url,
'#narrow',
'stream',
encoded_stream,
'topic',
encoded_topic,
'near',
message_id,
]
full_url = '/'.join(parts)
return full_url


def near_pm_message_url(server_url: str, message: Message) -> str:
message_id = str(message['id'])
str_user_ids = [
str(recipient['id'])
for recipient in message['display_recipient']
]

pm_str = ','.join(str_user_ids) + '-pm'
parts = [
server_url,
'#narrow',
'pm-with',
pm_str,
'near',
message_id,
]
full_url = '/'.join(parts)
return full_url


def near_message_url(server_url: str, message: Message) -> str:
if message['type'] == 'stream':
url = near_stream_message_url(server_url, message)
else:
url = near_pm_message_url(server_url, message)
return url

0 comments on commit a7e4b4c

Please sign in to comment.