Skip to content

Commit

Permalink
buttons: Add support for topic narrow in MessageLinkButton.
Browse files Browse the repository at this point in the history
This extends the existing helper methods, _parse_narrow_link(),
_validate_narrow_link() and _switch_narrow_to(), to add support for
topic narrow.

Tests amended.
  • Loading branch information
preetmishra committed Aug 21, 2020
1 parent 8f94de8 commit 989a0d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/ui_tools/test_buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,23 @@ def test__decode_stream_data(self, stream_data, expected_response):
(SERVER_URL + '/#narrow/stream/Stream.201',
{'narrow': 'stream',
'stream': {'stream_id': None, 'stream_name': 'Stream 1'}}),
(SERVER_URL + '/#narrow/stream/1-Stream-1/topic/foo.20bar',
{'narrow': 'stream:topic', 'topic_name': 'foo bar',
'stream': {'stream_id': 1, 'stream_name': None}}),
(SERVER_URL + '/#narrow/foo',
{}),
(SERVER_URL + '/#narrow/stream/',
{}),
(SERVER_URL + '/#narrow/stream/1-Stream-1/topic/',
{}),
],
ids=[
'modern_stream_narrow_link',
'deprecated_stream_narrow_link',
'topic_narrow_link',
'invalid_narrow_link_1',
'invalid_narrow_link_2',
'invalid_narrow_link_3',
]
)
def test__parse_narrow_link(self, link, expected_parsed_link):
Expand All @@ -125,30 +132,48 @@ def test__parse_narrow_link(self, link, expected_parsed_link):
'parsed_link',
'is_user_subscribed_to_stream',
'is_valid_stream',
'topics_in_stream',
'expected_error'
],
[
({'narrow': 'stream',
'stream': {'stream_id': 1, 'stream_name': None}},
True,
None,
None,
''),
({'narrow': 'stream',
'stream': {'stream_id': 462, 'stream_name': None}},
False,
None,
None,
'The stream seems to be either unknown or unsubscribed'),
({'narrow': 'stream',
'stream': {'stream_id': None, 'stream_name': 'Stream 1'}},
None,
True,
None,
''),
({'narrow': 'stream',
'stream': {'stream_id': None, 'stream_name': 'foo'}},
None,
False,
None,
'The stream seems to be either unknown or unsubscribed'),
({'narrow': 'stream:topic', 'topic_name': 'Valid',
'stream': {'stream_id': 1, 'stream_name': None}},
True,
None,
['Valid'],
''),
({'narrow': 'stream:topic', 'topic_name': 'Invalid',
'stream': {'stream_id': 1, 'stream_name': None}},
True,
None,
[],
'Invalid topic name'),
({},
None,
None,
None,
'The narrow link seems to be either broken or unsupported'),
Expand All @@ -158,18 +183,22 @@ def test__parse_narrow_link(self, link, expected_parsed_link):
'invalid_modern_stream_narrow_parsed_link',
'valid_deprecated_stream_narrow_parsed_link',
'invalid_deprecated_stream_narrow_parsed_link',
'valid_topic_narrow_parsed_link',
'invalid_topic_narrow_parsed_link',
'invalid_narrow_link',
]
)
def test__validate_narrow_link(self, stream_dict, parsed_link,
is_user_subscribed_to_stream,
is_valid_stream,
topics_in_stream,
expected_error):
self.controller.model.stream_dict = stream_dict
self.controller.model.is_user_subscribed_to_stream.return_value = (
is_user_subscribed_to_stream
)
self.controller.model.is_valid_stream.return_value = is_valid_stream
self.controller.model.topics_in_stream.return_value = topics_in_stream
mocked_button = self.message_link_button()

return_value = mocked_button._validate_narrow_link(parsed_link)
Expand Down Expand Up @@ -237,24 +266,34 @@ def test__validate_and_patch_stream_data(self, stream_dict, parsed_link,
@pytest.mark.parametrize([
'parsed_link',
'narrow_to_stream_called',
'narrow_to_topic_called',
],
[
({'narrow': 'stream',
'stream': {'stream_id': 1, 'stream_name': 'Stream 1'}},
True,
False),
({'narrow': 'stream:topic', 'topic_name': 'Foo',
'stream': {'stream_id': 1, 'stream_name': 'Stream 1'}},
False,
True),
],
ids=[
'stream_narrow',
'topic_narrow',
]
)
def test__switch_narrow_to(self, parsed_link, narrow_to_stream_called,
narrow_to_topic_called,
):
mocked_button = self.message_link_button()

mocked_button._switch_narrow_to(parsed_link)

assert (mocked_button.controller.narrow_to_stream.called
== narrow_to_stream_called)
assert (mocked_button.controller.narrow_to_topic.called
== narrow_to_topic_called)

@pytest.mark.parametrize(['error', 'set_footer_text_called',
'_switch_narrow_to_called',
Expand Down
21 changes: 21 additions & 0 deletions zulipterminal/ui_tools/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def __init__(self, user_id: int, email: str) -> None:
ParsedNarrowLink = TypedDict('ParsedNarrowLink', {
'narrow': str,
'stream': StreamData,
'topic_name': str,
}, total=False)


Expand Down Expand Up @@ -333,6 +334,13 @@ def _parse_narrow_link(cls, link: str) -> ParsedNarrowLink:
stream_data = cls._decode_stream_data(fragments[2])
parsed_link = dict(narrow='stream', stream=stream_data)

elif (len_fragments == 5 and fragments[1] == 'stream'
and fragments[3] == 'topic'):
stream_data = cls._decode_stream_data(fragments[2])
topic_name = hash_util_decode(fragments[4])
parsed_link = dict(narrow='stream:topic', stream=stream_data,
topic_name=topic_name)

return parsed_link

def _validate_and_patch_stream_data(self,
Expand Down Expand Up @@ -380,6 +388,14 @@ def _validate_narrow_link(self, parsed_link: ParsedNarrowLink) -> str:
if error:
return error

# Validate topic name.
if 'topic_name' in parsed_link:
topic_name = parsed_link['topic_name']
stream_id = parsed_link['stream']['stream_id']

if topic_name not in self.model.topics_in_stream(stream_id):
return 'Invalid topic name'

return ''

def _switch_narrow_to(self, parsed_link: ParsedNarrowLink) -> None:
Expand All @@ -391,6 +407,11 @@ def _switch_narrow_to(self, parsed_link: ParsedNarrowLink) -> None:
self.stream_id = parsed_link['stream']['stream_id']
self.stream_name = parsed_link['stream']['stream_name']
self.controller.narrow_to_stream(self)
elif 'stream:topic' == narrow:
self.stream_id = parsed_link['stream']['stream_id']
self.stream_name = parsed_link['stream']['stream_name']
self.topic_name = parsed_link['topic_name']
self.controller.narrow_to_topic(self)

def handle_narrow_link(self) -> None:
"""
Expand Down

0 comments on commit 989a0d7

Please sign in to comment.