From 2e9ef5bd72da7a6764d8f195b11da4d14a80830f Mon Sep 17 00:00:00 2001 From: mounilKshah Date: Sat, 26 Aug 2023 21:04:05 +0530 Subject: [PATCH] buttons: Add support for old format for narrow links. 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. --- tests/ui_tools/test_buttons.py | 33 +++++++++++++++++++++++++++++-- zulipterminal/ui_tools/buttons.py | 13 +++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/tests/ui_tools/test_buttons.py b/tests/ui_tools/test_buttons.py index 80f274991b..3102d70d4c 100644 --- a/tests/ui_tools/test_buttons.py +++ b/tests/ui_tools/test_buttons.py @@ -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( @@ -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(), @@ -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( diff --git a/zulipterminal/ui_tools/buttons.py b/zulipterminal/ui_tools/buttons.py index fe2116981c..3fd8b9772c 100644 --- a/zulipterminal/ui_tools/buttons.py +++ b/zulipterminal/ui_tools/buttons.py @@ -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) @@ -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]) @@ -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])