Skip to content

Commit

Permalink
buttons: Add support for old format for narrow links.
Browse files Browse the repository at this point in the history
This commit provides support for narrow links in message content
containing 'subject' instead of 'topic', which may be present in
messages before server version 2.1.0.

Test cases added.

Fixes #1422.
  • Loading branch information
mounilKshah authored and neiljp committed Sep 3, 2023
1 parent 2977bed commit 2e9ef5b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
33 changes: 31 additions & 2 deletions tests/ui_tools/test_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ def test__decode_message_id(
),
id="topic_narrow_link",
),
case(
"/#narrow/stream/1-Stream-1/subject/foo.20bar",
ParsedNarrowLink(
narrow="stream:topic",
topic_name="foo bar",
stream=DecodedStream(stream_id=1, stream_name=None),
),
id="subject_narrow_link",
),
case(
"/#narrow/stream/1-Stream-1/near/1",
ParsedNarrowLink(
Expand All @@ -696,6 +705,16 @@ def test__decode_message_id(
),
id="topic_near_narrow_link",
),
case(
"/#narrow/stream/1-Stream-1/subject/foo/near/1",
ParsedNarrowLink(
narrow="stream:topic:near",
topic_name="foo",
message_id=1,
stream=DecodedStream(stream_id=1, stream_name=None),
),
id="subject_near_narrow_link",
),
case(
"/#narrow/foo",
ParsedNarrowLink(),
Expand All @@ -712,15 +731,25 @@ def test__decode_message_id(
id="invalid_narrow_link_3",
),
case(
"/#narrow/stream/1-Stream-1//near/",
"/#narrow/stream/1-Stream-1/subject/",
ParsedNarrowLink(),
id="invalid_narrow_link_4",
),
case(
"/#narrow/stream/1-Stream-1/topic/foo/near/",
"/#narrow/stream/1-Stream-1//near/",
ParsedNarrowLink(),
id="invalid_narrow_link_5",
),
case(
"/#narrow/stream/1-Stream-1/topic/foo/near/",
ParsedNarrowLink(),
id="invalid_narrow_link_6",
),
case(
"/#narrow/stream/1-Stream-1/subject/foo/near/",
ParsedNarrowLink(),
id="invalid_narrow_link_7",
),
],
)
def test__parse_narrow_link(
Expand Down
13 changes: 10 additions & 3 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,17 @@ def _parse_narrow_link(cls, link: str) -> ParsedNarrowLink:
"""
# NOTE: The optional stream_id link version is deprecated. The extended
# support is for old messages.
# NOTE: Support for narrow links with subject instead of topic is also added
# We expect the fragment to be one of the following types:
# a. narrow/stream/[{stream_id}-]{stream-name}
# b. narrow/stream/[{stream_id}-]{stream-name}/near/{message_id}
# c. narrow/stream/[{stream_id}-]{stream-name}/topic/
# {encoded.20topic.20name}
# d. narrow/stream/[{stream_id}-]{stream-name}/topic/
# d. narrow/stream/[{stream_id}-]{stream-name}/subject/
# {encoded.20topic.20name}
# e. narrow/stream/[{stream_id}-]{stream-name}/topic/
# {encoded.20topic.20name}/near/{message_id}
# f. narrow/stream/[{stream_id}-]{stream-name}/subject/
# {encoded.20topic.20name}/near/{message_id}
fragments = urlparse(link.rstrip("/")).fragment.split("/")
len_fragments = len(fragments)
Expand All @@ -509,7 +514,9 @@ def _parse_narrow_link(cls, link: str) -> ParsedNarrowLink:
parsed_link = dict(narrow="stream", stream=stream_data)

elif (
len_fragments == 5 and fragments[1] == "stream" and fragments[3] == "topic"
len_fragments == 5
and fragments[1] == "stream"
and (fragments[3] == "topic" or fragments[3] == "subject")
):
stream_data = cls._decode_stream_data(fragments[2])
topic_name = hash_util_decode(fragments[4])
Expand All @@ -527,7 +534,7 @@ def _parse_narrow_link(cls, link: str) -> ParsedNarrowLink:
elif (
len_fragments == 7
and fragments[1] == "stream"
and fragments[3] == "topic"
and (fragments[3] == "topic" or fragments[3] == "subject")
and fragments[5] == "near"
):
stream_data = cls._decode_stream_data(fragments[2])
Expand Down

0 comments on commit 2e9ef5b

Please sign in to comment.