Skip to content

Commit 2732498

Browse files
committed
model: Update index['unread_msgs'] when appending messages.
Entries are added to unread message datastructure when new messsages are added, so that unread count can be updated. Tests amended.
1 parent 393e7d2 commit 2732498

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

tests/model/test_model.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ def test__handle_message_event_with_Falsey_log(self, mocker,
650650
model.found_newest = True
651651
mocker.patch('zulipterminal.model.Model._update_topic_index')
652652
index_msg = mocker.patch('zulipterminal.model.index_messages',
653-
return_value={})
653+
return_value=initial_index)
654654
model.msg_list = mocker.Mock(log=[])
655655
create_msg_box_list = mocker.patch('zulipterminal.model.'
656656
'create_msg_box_list',
@@ -671,7 +671,7 @@ def test__handle_message_event_with_valid_log(self, mocker,
671671
model.found_newest = True
672672
mocker.patch('zulipterminal.model.Model._update_topic_index')
673673
index_msg = mocker.patch('zulipterminal.model.index_messages',
674-
return_value={})
674+
return_value=initial_index)
675675
model.msg_list = mocker.Mock(log=[mocker.Mock()])
676676
create_msg_box_list = mocker.patch('zulipterminal.model.'
677677
'create_msg_box_list',
@@ -689,12 +689,37 @@ def test__handle_message_event_with_valid_log(self, mocker,
689689
assert_called_once_with(model, [message_fixture['id']],
690690
last_message=expected_last_msg))
691691

692+
@pytest.mark.parameter('unread_msgs', [
693+
({'type': 'stream', 'stream_id': 5140,
694+
'subject': 'Test', 'display_recipient': 'PTEST'}),
695+
({'type': 'private', 'sender_id': '5140'}),
696+
({'type': 'private', 'display_recipient':
697+
[{
698+
'id': 5179,
699+
'is_mirror_dummy': False,
700+
'full_name': 'Boo Boo',
701+
'short_name': 'boo',
702+
'email': '[email protected]',
703+
}, {
704+
'short_name': 'foo',
705+
'id': 5140,
706+
'is_mirror_dummy': False,
707+
'full_name': 'Foo Foo',
708+
'email': '[email protected]',
709+
}, {
710+
'short_name': 'bar',
711+
'id': 5180,
712+
'is_mirror_dummy': False,
713+
'full_name': 'Bar Bar',
714+
'email': '[email protected]',
715+
}]}),
716+
])
692717
def test__handle_message_event_with_flags(self, mocker,
693718
model, message_fixture):
694719
model.found_newest = True
695720
mocker.patch('zulipterminal.model.Model._update_topic_index')
696721
index_msg = mocker.patch('zulipterminal.model.index_messages',
697-
return_value={})
722+
return_value=initial_index)
698723
model.msg_list = mocker.Mock()
699724
create_msg_box_list = mocker.patch('zulipterminal.model.'
700725
'create_msg_box_list',
@@ -720,8 +745,9 @@ def test__handle_message_event_with_flags(self, mocker,
720745

721746
@pytest.mark.parametrize('response, narrow, recipients, log', [
722747
({'type': 'stream', 'stream_id': 1, 'subject': 'FOO',
723-
'id': 1}, [], frozenset(), ['msg_w']),
724-
({'type': 'private', 'id': 1},
748+
'id': 1, 'display_recipient': 'a'}, [], frozenset(), ['msg_w']),
749+
({'type': 'private', 'id': 1, 'sender_id': 1,
750+
'display_recipient': []},
725751
[['is', 'private']], frozenset(), ['msg_w']),
726752
({'type': 'stream', 'id': 1, 'stream_id': 1, 'subject': 'FOO',
727753
'display_recipient': 'a'},
@@ -734,14 +760,15 @@ def test__handle_message_event_with_flags(self, mocker,
734760
'display_recipient': 'a'},
735761
[['stream', 'c'], ['topic', 'b']],
736762
frozenset(), []),
737-
({'type': 'private', 'id': 1,
763+
({'type': 'private', 'id': 1, 'sender_id': 5827,
738764
'display_recipient': [{'id': 5827}, {'id': 5}]},
739765
[['pm_with', '[email protected]']],
740766
frozenset({5827, 5}), ['msg_w']),
741-
({'type': 'private', 'id': 1},
767+
({'type': 'private', 'id': 1, 'sender_id': 5827,
768+
'display_recipient': []},
742769
[['is', 'search']],
743770
frozenset(), []),
744-
({'type': 'private', 'id': 1,
771+
({'type': 'private', 'id': 1, 'sender_id': 5827,
745772
'display_recipient': [{'id': 5827}, {'id': 3212}]},
746773
[['pm_with', '[email protected]']],
747774
frozenset({5827, 5}), []),
@@ -759,7 +786,7 @@ def test__handle_message_event(self, mocker, user_profile, response,
759786
model.found_newest = True
760787
mocker.patch('zulipterminal.model.Model._update_topic_index')
761788
index_msg = mocker.patch('zulipterminal.model.index_messages',
762-
return_value={})
789+
return_value=initial_index)
763790
create_msg_box_list = mocker.patch('zulipterminal.model.'
764791
'create_msg_box_list',
765792
return_value=["msg_w"])

zulipterminal/model.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,26 @@ def _handle_message_event(self, event: Event) -> None:
772772
if narrow_recipients == message_recipients:
773773
self.msg_list.log.append(msg_w)
774774
if 'read' not in message['flags']:
775+
if message['type'] == 'stream':
776+
unread_data = {'type': 'stream', 'display_recipient':
777+
message['display_recipient'],
778+
'stream_id': message['stream_id'],
779+
'subject': message['subject'],
780+
'flags': message['flags']}
781+
elif len(message['display_recipient']) <= 2:
782+
# pm and self-pm
783+
unread_data = {'type': 'private',
784+
'sender_id': message['sender_id'],
785+
'flags': message['flags']}
786+
else:
787+
# huddles
788+
message_recipients = frozenset(
789+
[user['id'] for user in message['display_recipient']])
790+
unread_data = {'type': 'private',
791+
'display_recipient': message_recipients,
792+
'flags': message['flags']}
793+
self.index['unread_msgs'].update(
794+
{int(message['id']): unread_data})
775795
set_count([message['id']], self.controller, 1)
776796
self.controller.update_screen()
777797

0 commit comments

Comments
 (0)