Skip to content

Commit 81007e1

Browse files
committed
boxes/server_url: Relocated URL generation code to server_url module.
The URL generation part from the previous commit has been moved to a new module server_url.py to segregate the url component from the rest of the code in helper.py This will allow to track changes in the server and make constructing and deconstructing server URL's easier. This module can also be later integrated into the API package.
1 parent 85d3442 commit 81007e1

File tree

3 files changed

+85
-62
lines changed

3 files changed

+85
-62
lines changed

zulipterminal/helper.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Set, Tuple,
1212
TypeVar, Union,
1313
)
14-
from urllib.parse import quote, unquote
14+
from urllib.parse import unquote
1515

1616
import lxml.html
1717
from typing_extensions import Literal, TypedDict
@@ -668,61 +668,3 @@ def get_unused_fence(content: Any) -> Any:
668668
len(max(matches, key=len)) + 1)
669669

670670
return '`' * max_length_fence
671-
672-
673-
def all_user_ids_in_pm(message: Any) -> Any:
674-
"""
675-
Returns a sorted list of all user_ids invloved in
676-
a private chat, else None
677-
"""
678-
if (message['type'] != 'private'
679-
or len(message['display_recipient']) == 0):
680-
return None
681-
682-
user_ids = map(lambda x: x['id'], message['display_recipient'])
683-
sorted_user_ids = sorted(user_ids)
684-
return sorted_user_ids
685-
686-
687-
def pm_perma_link(message: Any) -> Any:
688-
"""
689-
Returns the link to a message from PM's
690-
"""
691-
user_ids = all_user_ids_in_pm(message)
692-
693-
if user_ids is not None:
694-
suffix = 'group' if len(user_ids) >= 3 else 'pm'
695-
slug = ','.join(map(str, user_ids)) + '-' + suffix
696-
uri = '#narrow/pm-with/' + slug
697-
return uri
698-
699-
700-
def encode_hash_component(string: str) -> str:
701-
"""
702-
Hide URI-encoding by replacing % with .
703-
[referred from zulip/static/js/hash_util.js]
704-
"""
705-
return quote(string, safe='~()*!.\'').replace('.', '%2E').replace('%', '.')
706-
707-
708-
def absolute_url_of_message(model: Any, message: Any) -> str:
709-
"""
710-
Returns the absolute url of any message as present
711-
in the webapp. Currently only used to get the url
712-
of a quoted message.
713-
"""
714-
absolute_url = model.server_url
715-
suffix = '/near/' + str(message['id'])
716-
717-
if message['type'] == 'stream':
718-
absolute_url += '#narrow/stream/'
719-
stream_name = message['display_recipient'].replace(' ', '-')
720-
prefix = encode_hash_component(str(message['stream_id'])
721-
+ '-' + stream_name)
722-
prefix += '/topic/' + encode_hash_component(message['subject'])
723-
724-
absolute_url += prefix + suffix
725-
return absolute_url
726-
727-
absolute_url += pm_perma_link(message) + suffix
728-
return absolute_url

zulipterminal/server_url.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import urllib
2+
3+
from zulipterminal.helper import Message
4+
5+
6+
def hash_util_encode(string: str) -> str:
7+
"""
8+
Hide URI-encoding by replacing % with .
9+
Referred from zerver/lib/url_encoding.py
10+
"""
11+
# `safe` has a default value of "/", but we want those encoded, too.
12+
return urllib.parse.quote(
13+
string, safe=b"").replace(".", "%2E").replace("%", ".")
14+
15+
16+
def encode_stream(stream_id: int, stream_name: str) -> str:
17+
# We encode streams for urls as something like 99-Verona.
18+
stream_name = stream_name.replace(' ', '-')
19+
return str(stream_id) + '-' + hash_util_encode(stream_name)
20+
21+
22+
def near_stream_message_url(server_url: str, message: Message) -> str:
23+
"""
24+
Returns the encoded URL of a message from #narrow/stream.
25+
"""
26+
message_id = str(message['id'])
27+
stream_id = message['stream_id']
28+
stream_name = message['display_recipient']
29+
topic_name = message['subject']
30+
encoded_stream = encode_stream(stream_id, stream_name)
31+
encoded_topic = hash_util_encode(topic_name)
32+
33+
parts = [
34+
server_url,
35+
'#narrow',
36+
'stream',
37+
encoded_stream,
38+
'topic',
39+
encoded_topic,
40+
'near',
41+
message_id,
42+
]
43+
full_url = '/'.join(parts)
44+
return full_url
45+
46+
47+
def near_pm_message_url(server_url: str, message: Message) -> str:
48+
"""
49+
Returns the encoded URL of a message from #narrow/pm-with.
50+
"""
51+
message_id = str(message['id'])
52+
str_user_ids = [
53+
str(recipient['id'])
54+
for recipient in message['display_recipient']
55+
]
56+
57+
pm_str = ','.join(str_user_ids) + '-pm'
58+
parts = [
59+
server_url,
60+
'#narrow',
61+
'pm-with',
62+
pm_str,
63+
'near',
64+
message_id,
65+
]
66+
full_url = '/'.join(parts)
67+
return full_url
68+
69+
70+
def near_message_url(server_url: str, message: Message) -> str:
71+
"""
72+
Returns the correct encoded URL of a message, if
73+
it is present in stream/pm-with accordingly.
74+
"""
75+
if message['type'] == 'stream':
76+
url = near_stream_message_url(server_url, message)
77+
else:
78+
url = near_pm_message_url(server_url, message)
79+
return url

zulipterminal/ui_tools/boxes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
STREAM_TOPIC_SEPARATOR, TIME_MENTION_MARKER,
2323
)
2424
from zulipterminal.helper import (
25-
Message, absolute_url_of_message, format_string, get_unused_fence,
26-
match_emoji, match_group, match_stream, match_topics, match_user,
25+
Message, format_string, get_unused_fence, match_emoji, match_group,
26+
match_stream, match_topics, match_user,
2727
)
28+
from zulipterminal.server_url import near_message_url
2829
from zulipterminal.ui_tools.buttons import EditModeButton
2930
from zulipterminal.ui_tools.tables import render_table
3031
from zulipterminal.urwid_types import urwid_Size
@@ -1200,7 +1201,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
12001201
# ```quote
12011202
# message_content
12021203
# ```
1203-
absolute_url = absolute_url_of_message(self.model, self.message)
1204+
absolute_url = near_message_url(
1205+
self.model.server_url[:-1], self.message)
12041206
fence = get_unused_fence(self.model.client.get_raw_message(
12051207
self.message['id'])['raw_content'])
12061208

0 commit comments

Comments
 (0)