Skip to content

Commit fd30d4a

Browse files
committed
helper/model: Updated logic for footer notification for sent message.
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.
1 parent ec7e0d6 commit fd30d4a

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

tests/helper/test_helper.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import zulipterminal.helper
44
from zulipterminal.helper import (
55
canonicalize_color, classify_unread_counts, display_error_if_present,
6-
hash_util_decode, index_messages, notify, powerset,
6+
hash_util_decode, index_messages, notify,
7+
notify_if_message_sent_outside_narrow, powerset,
78
)
89

910

@@ -303,6 +304,31 @@ def test_display_error_if_present(mocker, response, footer_updated):
303304
set_footer_text.assert_not_called()
304305

305306

307+
@pytest.mark.parametrize(['req', 'narrow', 'footer_updated'], [
308+
({'type': 'private', 'to': '[email protected]', 'content': 'bar'},
309+
[['is', 'private']], False),
310+
({'type': 'private', 'to': '[email protected]', 'content': 'foo'},
311+
[['pm_with', '[email protected]']], True),
312+
({'type': 'stream', 'to': 'ZT', 'subject': '1', 'content': 'foo'},
313+
[['stream', 'ZT'], ['topic', '1']], False),
314+
({'type': 'stream', 'to': 'here', 'subject': 'pytest', 'content': 'py'},
315+
[['stream', 'test here']], True)
316+
])
317+
def test_notify_if_message_sent_outside_narrow(mocker, req, narrow,
318+
footer_updated):
319+
controller = mocker.Mock()
320+
set_footer_text = controller.view.set_footer_text
321+
controller.model.narrow = narrow
322+
323+
notify_if_message_sent_outside_narrow(req, controller)
324+
325+
if footer_updated:
326+
set_footer_text.assert_called_once_with(
327+
'Message is sent outside of current narrow.', 3)
328+
else:
329+
set_footer_text.assert_not_called()
330+
331+
306332
@pytest.mark.parametrize('quoted_string, expected_unquoted_string', [
307333
('(no.20topic)', '(no topic)'),
308334
('.3Cstrong.3Exss.3C.2Fstrong.3E', '<strong>xss</strong>'),

tests/model/test_model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def mock_external_classes(self, mocker: Any) -> None:
2323
mocker.patch('zulipterminal.model.Model._start_presence_updates')
2424
self.display_error_if_present = mocker.patch(
2525
'zulipterminal.model.display_error_if_present')
26+
self.notify_if_message_sent_outside_narrow = mocker.patch(
27+
'zulipterminal.model.notify_if_message_sent_outside_narrow')
2628

2729
@pytest.fixture
2830
def model(self, mocker, initial_data, user_profile,
@@ -455,6 +457,9 @@ def test_send_private_message(self, mocker, model,
455457
assert result == return_value
456458
self.display_error_if_present.assert_called_once_with(response,
457459
self.controller)
460+
if result == 'success':
461+
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
462+
req, self.controller)
458463

459464
def test_send_private_message_with_no_recipients(self, model,
460465
content="hi!",
@@ -481,6 +486,10 @@ def test_send_stream_message(self, mocker, model,
481486
self.display_error_if_present.assert_called_once_with(response,
482487
self.controller)
483488

489+
if result == 'success':
490+
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
491+
req, self.controller)
492+
484493
@pytest.mark.parametrize('response, return_value', [
485494
({'result': 'success'}, True),
486495
({'result': 'some_failure'}, False),

zulipterminal/helper.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,28 @@ def display_error_if_present(response: Dict[str, Any], controller: Any
647647
controller.view.set_footer_text(response['msg'], 3)
648648

649649

650+
def notify_if_message_sent_outside_narrow(request: Dict[str, Any],
651+
controller: Any) -> None:
652+
current_narrow = controller.model.narrow
653+
654+
if request['type'] == 'stream':
655+
stream_narrow = [['stream', request['to']]]
656+
topic_narrow = stream_narrow + [['topic', request['subject']]]
657+
658+
if (current_narrow != stream_narrow
659+
and current_narrow != topic_narrow):
660+
controller.view.set_footer_text(
661+
'Message is sent outside of current narrow.', 3)
662+
elif request['type'] == 'private':
663+
pm_narrow = [['is', 'private']]
664+
pm_with_narrow = [['pm_with', request['to']]]
665+
666+
if (current_narrow != pm_narrow
667+
and current_narrow != pm_with_narrow):
668+
controller.view.set_footer_text(
669+
'Message is sent outside of current narrow.', 3)
670+
671+
650672
def hash_util_decode(string: str) -> str:
651673
"""
652674
Returns a decoded string given a hash_util_encode() [present in

zulipterminal/model.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from zulipterminal.helper import (
1818
Message, NamedEmojiData, StreamData, asynch, canonicalize_color,
1919
classify_unread_counts, display_error_if_present, index_messages,
20-
initial_index, notify, set_count,
20+
initial_index, notify, notify_if_message_sent_outside_narrow, set_count,
2121
)
2222
from zulipterminal.ui_tools.utils import create_msg_box_list
2323

@@ -343,6 +343,9 @@ def send_private_message(self, recipients: List[str],
343343
}
344344
response = self.client.send_message(request)
345345
display_error_if_present(response, self.controller)
346+
# FIXME: Is there a better way?
347+
if response['result'] == 'success':
348+
notify_if_message_sent_outside_narrow(request, self.controller)
346349
return response['result'] == 'success'
347350
else:
348351
raise RuntimeError('Empty recipients list.')
@@ -357,17 +360,8 @@ def send_stream_message(self, stream: str, topic: str,
357360
}
358361
response = self.client.send_message(request)
359362
display_error_if_present(response, self.controller)
360-
361-
curr_narrow = self.narrow
362-
view = self.controller.view
363-
if ((len(curr_narrow) == 1
364-
and curr_narrow[0][1] != request['to'])
365-
or (len(curr_narrow) == 2
366-
and (curr_narrow[0][1] != request['to']
367-
or curr_narrow[1][1] != request['subject']))):
368-
view.set_footer_text(
369-
'Message is sent outside of current narrow.', 3)
370-
363+
if response['result'] == 'success':
364+
notify_if_message_sent_outside_narrow(request, self.controller)
371365
return response['result'] == 'success'
372366

373367
def update_private_message(self, msg_id: int, content: str) -> bool:
@@ -392,12 +386,12 @@ def update_stream_message(self, topic: str, message_id: int,
392386

393387
response = self.client.update_message(request)
394388
display_error_if_present(response, self.controller)
395-
396-
old_topic = self.index['messages'][message_id].get('subject', None)
397-
new_topic = request['topic']
398-
view = self.controller.view
399-
if old_topic != new_topic:
400-
view.set_footer_text('Topic changed', 3)
389+
if response['result'] == 'success':
390+
old_topic = self.index['messages'][message_id].get('subject', None)
391+
new_topic = request['topic']
392+
view = self.controller.view
393+
if old_topic != new_topic:
394+
view.set_footer_text('Topic changed', 3)
401395

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

0 commit comments

Comments
 (0)