Skip to content

Commit eb71b54

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 96a49c1 commit eb71b54

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,23 @@ def _handle_message_event(self, event: Event) -> None:
754754
if narrow_recipients == message_recipients:
755755
self.msg_list.log.append(msg_w)
756756
if 'read' not in response['flags']:
757+
if response['type'] == 'stream':
758+
unread_data = {'type': 'stream', 'display_recipient':
759+
response['display_recipient'],
760+
'stream_id': response['stream_id'],
761+
'subject': response['subject']}
762+
elif len(response['display_recipient']) <= 2:
763+
# pm and self-pm
764+
unread_data = {'type': 'private',
765+
'sender_id': response['sender_id']}
766+
else:
767+
# huddles
768+
message_recipients = frozenset(
769+
[user['id'] for user in response['display_recipient']])
770+
unread_data = {'type': 'private',
771+
'display_recipient': message_recipients}
772+
self.index['unread_msgs'].update(
773+
{int(response['id']): unread_data})
757774
set_count([response['id']], self.controller, 1)
758775
self.controller.update_screen()
759776

0 commit comments

Comments
 (0)