Skip to content

Commit c510ebc

Browse files
committed
helper/model: Add footer notification for message sent outside narrow.
This commit generalizes the code for footer notification when a message is sent/edited outside current narrow. This will give the user a pointer as to why the message disappeared from the screen. The code is hooked to the part where message request is sent to the server, hence preventing the extra check for user_id being same as message_sender_id. Tests amended. Fixes #781.
1 parent 01fc24a commit c510ebc

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

tests/helper/test_helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
hash_util_decode,
1010
index_messages,
1111
notify,
12+
notify_if_message_sent_outside_narrow,
1213
powerset,
1314
)
1415

@@ -309,6 +310,31 @@ def test_display_error_if_present(mocker, response, footer_updated):
309310
set_footer_text.assert_not_called()
310311

311312

313+
@pytest.mark.parametrize(['req', 'narrow', 'footer_updated'], [
314+
({'type': 'private', 'to': '[email protected]', 'content': 'bar'},
315+
[['is', 'private']], False),
316+
({'type': 'private', 'to': '[email protected]', 'content': 'foo'},
317+
[['pm_with', '[email protected]']], True),
318+
({'type': 'stream', 'to': 'ZT', 'subject': '1', 'content': 'foo'},
319+
[['stream', 'ZT'], ['topic', '1']], False),
320+
({'type': 'stream', 'to': 'here', 'subject': 'pytest', 'content': 'py'},
321+
[['stream', 'test here']], True)
322+
])
323+
def test_notify_if_message_sent_outside_narrow(mocker, req, narrow,
324+
footer_updated):
325+
controller = mocker.Mock()
326+
set_footer_text = controller.view.set_footer_text
327+
controller.model.narrow = narrow
328+
329+
notify_if_message_sent_outside_narrow(req, controller)
330+
331+
if footer_updated:
332+
set_footer_text.assert_called_once_with(
333+
'Message is sent outside of current narrow.', 3)
334+
else:
335+
set_footer_text.assert_not_called()
336+
337+
312338
@pytest.mark.parametrize('quoted_string, expected_unquoted_string', [
313339
('(no.20topic)', '(no topic)'),
314340
('.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,
@@ -481,6 +483,9 @@ def test_send_private_message(self, mocker, model,
481483
assert result == return_value
482484
self.display_error_if_present.assert_called_once_with(response,
483485
self.controller)
486+
if result == 'success':
487+
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
488+
req, self.controller)
484489

485490
def test_send_private_message_with_no_recipients(self, model,
486491
content="hi!",
@@ -507,6 +512,10 @@ def test_send_stream_message(self, mocker, model,
507512
self.display_error_if_present.assert_called_once_with(response,
508513
self.controller)
509514

515+
if result == 'success':
516+
self.notify_if_message_sent_outside_narrow.assert_called_once_with(
517+
req, self.controller)
518+
510519
@pytest.mark.parametrize('response, return_value', [
511520
({'result': 'success'}, True),
512521
({'result': 'some_failure'}, False),

zulipterminal/helper.py

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

653653

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

zulipterminal/model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
index_messages,
3838
initial_index,
3939
notify,
40+
notify_if_message_sent_outside_narrow,
4041
set_count,
4142
)
4243
from zulipterminal.ui_tools.utils import create_msg_box_list
@@ -385,6 +386,9 @@ def send_private_message(self, recipients: List[str],
385386
}
386387
response = self.client.send_message(request)
387388
display_error_if_present(response, self.controller)
389+
# FIXME: Is there a better way?
390+
if response['result'] == 'success':
391+
notify_if_message_sent_outside_narrow(request, self.controller)
388392
return response['result'] == 'success'
389393
else:
390394
raise RuntimeError('Empty recipients list.')
@@ -399,6 +403,8 @@ def send_stream_message(self, stream: str, topic: str,
399403
}
400404
response = self.client.send_message(request)
401405
display_error_if_present(response, self.controller)
406+
if response['result'] == 'success':
407+
notify_if_message_sent_outside_narrow(request, self.controller)
402408
return response['result'] == 'success'
403409

404410
def update_private_message(self, msg_id: int, content: str) -> bool:
@@ -423,6 +429,13 @@ def update_stream_message(self, topic: str, message_id: int,
423429

424430
response = self.client.update_message(request)
425431
display_error_if_present(response, self.controller)
432+
if response['result'] == 'success':
433+
old_topic = self.index['messages'][message_id].get('subject', None)
434+
new_topic = request['topic']
435+
view = self.controller.view
436+
if old_topic != new_topic:
437+
view.set_footer_text('Topic changed', 3)
438+
426439
return response['result'] == 'success'
427440

428441
def fetch_custom_emojis(self) -> NamedEmojiData:

0 commit comments

Comments
 (0)