Skip to content

Commit

Permalink
helper/model: Updated logic for footer notification for sent message.
Browse files Browse the repository at this point in the history
This commit generalizes the code for footer notification when a message
is sent outside current narrow. The code is now more robust than
before with seperate function that checks when to notify. Error-
handling in case message was not sent is also taken care of.

Tests amended.
  • Loading branch information
zee-bit committed Jan 25, 2021
1 parent e791b4b commit 8c22b1b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
28 changes: 27 additions & 1 deletion tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import zulipterminal.helper
from zulipterminal.helper import (
canonicalize_color, classify_unread_counts, display_error_if_present,
get_unused_fence, hash_util_decode, index_messages, notify, powerset,
get_unused_fence, hash_util_decode, index_messages, notify,
notify_if_message_sent_outside_narrow, powerset,
)


Expand Down Expand Up @@ -303,6 +304,31 @@ def test_display_error_if_present(mocker, response, footer_updated):
set_footer_text.assert_not_called()


@pytest.mark.parametrize(['req', 'narrow', 'footer_updated'], [
({'type': 'private', 'to': 'foo@gmail.com', 'content': 'bar'},
[['is', 'private']], False),
({'type': 'private', 'to': 'bar@chat.zulip.com', 'content': 'foo'},
[['pm_with', 'foo@chat.zulip.com']], True),
({'type': 'stream', 'to': 'ZT', 'subject': '1', 'content': 'foo'},
[['stream', 'ZT'], ['topic', '1']], False),
({'type': 'stream', 'to': 'here', 'subject': 'pytest', 'content': 'py'},
[['stream', 'test here']], True)
])
def test_notify_if_message_sent_outside_narrow(mocker, req, narrow,
footer_updated):
controller = mocker.Mock()
set_footer_text = controller.view.set_footer_text
controller.model.narrow = narrow

notify_if_message_sent_outside_narrow(req, controller)

if footer_updated:
set_footer_text.assert_called_once_with(
'Message is sent outside of current narrow.', 3)
else:
set_footer_text.assert_not_called()


@pytest.mark.parametrize('quoted_string, expected_unquoted_string', [
('(no.20topic)', '(no topic)'),
('.3Cstrong.3Exss.3C.2Fstrong.3E', '<strong>xss</strong>'),
Expand Down
9 changes: 9 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def mock_external_classes(self, mocker: Any) -> None:
mocker.patch('zulipterminal.model.Model._start_presence_updates')
self.display_error_if_present = mocker.patch(
'zulipterminal.model.display_error_if_present')
self.notify_if_message_sent_outside_narrow = mocker.patch(
'zulipterminal.model.notify_if_message_sent_outside_narrow')

@pytest.fixture
def model(self, mocker, initial_data, user_profile,
Expand Down Expand Up @@ -481,6 +483,9 @@ def test_send_private_message(self, mocker, model,
assert result == return_value
self.display_error_if_present.assert_called_once_with(response,
self.controller)
if result == 'success':
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
req, self.controller)

def test_send_private_message_with_no_recipients(self, model,
content="hi!",
Expand All @@ -507,6 +512,10 @@ def test_send_stream_message(self, mocker, model,
self.display_error_if_present.assert_called_once_with(response,
self.controller)

if result == 'success':
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
req, self.controller)

@pytest.mark.parametrize('response, return_value', [
({'result': 'success'}, True),
({'result': 'some_failure'}, False),
Expand Down
22 changes: 22 additions & 0 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,28 @@ def display_error_if_present(response: Dict[str, Any], controller: Any
controller.view.set_footer_text(response['msg'], 3)


def notify_if_message_sent_outside_narrow(request: Dict[str, Any],
controller: Any) -> None:
current_narrow = controller.model.narrow

if request['type'] == 'stream':
stream_narrow = [['stream', request['to']]]
topic_narrow = stream_narrow + [['topic', request['subject']]]

if (current_narrow != stream_narrow
and current_narrow != topic_narrow):
controller.view.set_footer_text(
'Message is sent outside of current narrow.', 3)
elif request['type'] == 'private':
pm_narrow = [['is', 'private']]
pm_with_narrow = [['pm_with', request['to']]]

if (current_narrow != pm_narrow
and current_narrow != pm_with_narrow):
controller.view.set_footer_text(
'Message is sent outside of current narrow.', 3)


def hash_util_decode(string: str) -> str:
"""
Returns a decoded string given a hash_util_encode() [present in
Expand Down
30 changes: 12 additions & 18 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from zulipterminal.helper import (
Message, NamedEmojiData, StreamData, asynch, canonicalize_color,
classify_unread_counts, display_error_if_present, index_messages,
initial_index, notify, set_count,
initial_index, notify, notify_if_message_sent_outside_narrow, set_count,
)
from zulipterminal.ui_tools.utils import create_msg_box_list

Expand Down Expand Up @@ -357,6 +357,9 @@ def send_private_message(self, recipients: List[str],
}
response = self.client.send_message(request)
display_error_if_present(response, self.controller)
# FIXME: Is there a better way?
if response['result'] == 'success':
notify_if_message_sent_outside_narrow(request, self.controller)
return response['result'] == 'success'
else:
raise RuntimeError('Empty recipients list.')
Expand All @@ -371,17 +374,8 @@ def send_stream_message(self, stream: str, topic: str,
}
response = self.client.send_message(request)
display_error_if_present(response, self.controller)

curr_narrow = self.narrow
view = self.controller.view
if ((len(curr_narrow) == 1
and curr_narrow[0][1] != request['to'])
or (len(curr_narrow) == 2
and (curr_narrow[0][1] != request['to']
or curr_narrow[1][1] != request['subject']))):
view.set_footer_text(
'Message is sent outside of current narrow.', 3)

if response['result'] == 'success':
notify_if_message_sent_outside_narrow(request, self.controller)
return response['result'] == 'success'

def update_private_message(self, msg_id: int, content: str) -> bool:
Expand All @@ -406,12 +400,12 @@ def update_stream_message(self, topic: str, message_id: int,

response = self.client.update_message(request)
display_error_if_present(response, self.controller)

old_topic = self.index['messages'][message_id].get('subject', None)
new_topic = request['topic']
view = self.controller.view
if old_topic != new_topic:
view.set_footer_text('Topic changed', 3)
if response['result'] == 'success':
old_topic = self.index['messages'][message_id].get('subject', None)
new_topic = request['topic']
view = self.controller.view
if old_topic != new_topic:
view.set_footer_text('Topic changed', 3)

return response['result'] == 'success'

Expand Down

0 comments on commit 8c22b1b

Please sign in to comment.