Skip to content

Commit 0169add

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 c2a1380 commit 0169add

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
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': 'boo@zulip.com',
703+
}, {
704+
'short_name': 'foo',
705+
'id': 5140,
706+
'is_mirror_dummy': False,
707+
'full_name': 'Foo Foo',
708+
'email': 'foo@zulip.com',
709+
}, {
710+
'short_name': 'bar',
711+
'id': 5180,
712+
'is_mirror_dummy': False,
713+
'full_name': 'Bar Bar',
714+
'email': 'bar@zulip.com',
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', 'notification-bot@zulip.com']],
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', 'notification-bot@zulip.com']],
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,28 @@ def _handle_message_event(self, event: Event) -> None:
732732
self.controller.update_screen()
733733
self._notified_user_of_notification_failure = True
734734

735-
# Index messages before calling set_count.
736735
self.index = index_messages([message], self, self.index)
737736
if 'read' not in message['flags']:
737+
if message['type'] == 'stream':
738+
unread_data = {'type': 'stream', 'display_recipient':
739+
message['display_recipient'],
740+
'stream_id': message['stream_id'],
741+
'subject': message['subject'],
742+
'flags': message['flags']}
743+
elif len(message['display_recipient']) <= 2:
744+
# pm and self-pm
745+
unread_data = {'type': 'private',
746+
'sender_id': message['sender_id'],
747+
'flags': message['flags']}
748+
else:
749+
# huddles
750+
message_recipients = frozenset(
751+
[user['id'] for user in message['display_recipient']])
752+
unread_data = {'type': 'private',
753+
'display_recipient': message_recipients,
754+
'flags': message['flags']}
755+
self.index['unread_msgs'].update(
756+
{int(message['id']): unread_data})
738757
set_count([message['id']], self.controller, 1)
739758

740759
if hasattr(self.controller, 'view') and self.found_newest:

0 commit comments

Comments
 (0)