Skip to content

Commit b4cdf24

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 06ddfb7 commit b4cdf24

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_append_message_with_Falsey_log(self, mocker, model,
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()
655655
create_msg_box_list = mocker.patch('zulipterminal.model.'
656656
'create_msg_box_list',
@@ -672,7 +672,7 @@ def test_append_message_with_valid_log(self, mocker, model,
672672
model.found_newest = True
673673
mocker.patch('zulipterminal.model.Model.update_topic_index')
674674
index_msg = mocker.patch('zulipterminal.model.index_messages',
675-
return_value={})
675+
return_value=initial_index)
676676
model.msg_list = mocker.Mock()
677677
create_msg_box_list = mocker.patch('zulipterminal.model.'
678678
'create_msg_box_list',
@@ -691,11 +691,36 @@ def test_append_message_with_valid_log(self, mocker, model,
691691
assert_called_once_with(model, [message_fixture['id']],
692692
last_message=expected_last_msg))
693693

694+
@pytest.mark.parameter('unread_msgs', [
695+
({'type': 'stream', 'stream_id': 5140,
696+
'subject': 'Test', 'display_recipient': 'PTEST'}),
697+
({'type': 'private', 'sender_id': '5140'}),
698+
({'type': 'private', 'display_recipient':
699+
[{
700+
'id': 5179,
701+
'is_mirror_dummy': False,
702+
'full_name': 'Boo Boo',
703+
'short_name': 'boo',
704+
'email': 'boo@zulip.com',
705+
}, {
706+
'short_name': 'foo',
707+
'id': 5140,
708+
'is_mirror_dummy': False,
709+
'full_name': 'Foo Foo',
710+
'email': 'foo@zulip.com',
711+
}, {
712+
'short_name': 'bar',
713+
'id': 5180,
714+
'is_mirror_dummy': False,
715+
'full_name': 'Bar Bar',
716+
'email': 'bar@zulip.com',
717+
}]}),
718+
])
694719
def test_append_message_event_flags(self, mocker, model, message_fixture):
695720
model.found_newest = True
696721
mocker.patch('zulipterminal.model.Model.update_topic_index')
697722
index_msg = mocker.patch('zulipterminal.model.index_messages',
698-
return_value={})
723+
return_value=initial_index)
699724
model.msg_list = mocker.Mock()
700725
create_msg_box_list = mocker.patch('zulipterminal.model.'
701726
'create_msg_box_list',
@@ -721,8 +746,9 @@ def test_append_message_event_flags(self, mocker, model, message_fixture):
721746

722747
@pytest.mark.parametrize('response, narrow, recipients, log', [
723748
({'type': 'stream', 'stream_id': 1, 'subject': 'FOO',
724-
'id': 1}, [], frozenset(), ['msg_w']),
725-
({'type': 'private', 'id': 1},
749+
'id': 1, 'display_recipient': 'a'}, [], frozenset(), ['msg_w']),
750+
({'type': 'private', 'id': 1, 'sender_id': 1,
751+
'display_recipient': []},
726752
[['is', 'private']], frozenset(), ['msg_w']),
727753
({'type': 'stream', 'id': 1, 'stream_id': 1, 'subject': 'FOO',
728754
'display_recipient': 'a'},
@@ -735,14 +761,15 @@ def test_append_message_event_flags(self, mocker, model, message_fixture):
735761
'display_recipient': 'a'},
736762
[['stream', 'c'], ['topic', 'b']],
737763
frozenset(), []),
738-
({'type': 'private', 'id': 1,
764+
({'type': 'private', 'id': 1, 'sender_id': 5827,
739765
'display_recipient': [{'id': 5827}, {'id': 5}]},
740766
[['pm_with', 'notification-bot@zulip.com']],
741767
frozenset({5827, 5}), ['msg_w']),
742-
({'type': 'private', 'id': 1},
768+
({'type': 'private', 'id': 1, 'sender_id': 5827,
769+
'display_recipient': []},
743770
[['is', 'search']],
744771
frozenset(), []),
745-
({'type': 'private', 'id': 1,
772+
({'type': 'private', 'id': 1, 'sender_id': 5827,
746773
'display_recipient': [{'id': 5827}, {'id': 3212}]},
747774
[['pm_with', 'notification-bot@zulip.com']],
748775
frozenset({5827, 5}), []),
@@ -756,7 +783,7 @@ def test_append_message(self, mocker, user_profile, response,
756783
model.found_newest = True
757784
mocker.patch('zulipterminal.model.Model.update_topic_index')
758785
index_msg = mocker.patch('zulipterminal.model.index_messages',
759-
return_value={})
786+
return_value=initial_index)
760787
create_msg_box_list = mocker.patch('zulipterminal.model.'
761788
'create_msg_box_list',
762789
return_value=["msg_w"])

zulipterminal/model.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,23 @@ def append_message(self, event: Event) -> None:
746746
if narrow_recipients == message_recipients:
747747
self.msg_list.log.append(msg_w)
748748
if 'read' not in response['flags']:
749+
if response['type'] == 'stream':
750+
unread_data = {'type': 'stream', 'display_recipient':
751+
response['display_recipient'],
752+
'stream_id': response['stream_id'],
753+
'subject': response['subject']}
754+
elif len(response['display_recipient']) <= 2:
755+
# pm and self-pm
756+
unread_data = {'type': 'private',
757+
'sender_id': response['sender_id']}
758+
else:
759+
# huddles
760+
message_recipients = frozenset(
761+
[user['id'] for user in response['display_recipient']])
762+
unread_data = {'type': 'private',
763+
'display_recipient': message_recipients}
764+
self.index['unread_msgs'].update(
765+
{int(response['id']): unread_data})
749766
set_count([response['id']], self.controller, 1)
750767
self.controller.update_screen()
751768

0 commit comments

Comments
 (0)