Skip to content

Commit 18bfb3c

Browse files
committed
helper: Store unread message data in index.
Add 'unread_msgs' attribute in index to store all data pertaining to unread messages obtained from initial_data. Modify classify_unread_counts to return this unread_msgs data structure. Tests amended.
1 parent 013f898 commit 18bfb3c

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

tests/conftest.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ def empty_index():
481481
stream_msg_template['id']: stream_msg_template,
482482
pm_template['id']: pm_template,
483483
group_pm_template['id']: group_pm_template,
484-
})
484+
}),
485+
'unread_msgs': defaultdict(dict),
485486
})
486487

487488

@@ -720,10 +721,10 @@ def stream_dict(streams_fixture):
720721
@pytest.fixture
721722
def classified_unread_counts():
722723
"""
723-
Unread counts return by
724-
helper.classify_unread_counts function.
724+
Tuple of unread counts and unread_msg data
725+
returned by helper.classify_unread_counts function.
725726
"""
726-
return {
727+
return ({
727728
'all_msg': 12,
728729
'all_pms': 8,
729730
'unread_topics': {
@@ -742,4 +743,26 @@ def classified_unread_counts():
742743
1000: 3,
743744
99: 1
744745
}
745-
}
746+
}, {
747+
1: {'type': 'private', 'display_recipient': 1},
748+
2: {'type': 'private', 'display_recipient': 1},
749+
3: {'type': 'private', 'display_recipient': 2},
750+
4: {'type': 'stream', 'display_recipient': 'general',
751+
'stream_id': 1000, 'subject': 'Some general unread topic'},
752+
5: {'type': 'stream', 'display_recipient': 'general',
753+
'stream_id': 1000, 'subject': 'Some general unread topic'},
754+
6: {'type': 'stream', 'display_recipient': 'general',
755+
'stream_id': 1000, 'subject': 'Some general unread topic'},
756+
7: {'type': 'stream', 'display_recipient': 'announce',
757+
'stream_id': 99, 'subject': 'Some private unread topic'},
758+
11: {'type': 'private', 'display_recipient':
759+
frozenset({11, 12, 1001})},
760+
12: {'type': 'private', 'display_recipient':
761+
frozenset({11, 12, 1001})},
762+
13: {'type': 'private', 'display_recipient':
763+
frozenset({11, 12, 1001})},
764+
101: {'type': 'private', 'display_recipient':
765+
frozenset({11, 12, 13, 1001})},
766+
102: {'type': 'private', 'display_recipient':
767+
frozenset({11, 12, 13, 1001})},
768+
})

tests/helper/test_helper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ def test_classify_unread_counts(mocker, initial_data, stream_dict,
213213
model.initial_data = initial_data
214214
model.muted_topics = muted_topics
215215
model.muted_streams = muted_streams
216-
assert classify_unread_counts(model) == dict(classified_unread_counts,
217-
**vary_in_unreads)
216+
assert classify_unread_counts(model) == (dict(classified_unread_counts[0],
217+
**vary_in_unreads),
218+
classified_unread_counts[1])
218219

219220

220221
@pytest.mark.parametrize('color', [

tests/model/test_model.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def model(self, mocker, initial_data, user_profile):
3434
# NOTE: PATCH WHERE USED NOT WHERE DEFINED
3535
self.classify_unread_counts = mocker.patch(
3636
'zulipterminal.model.classify_unread_counts',
37-
return_value=[])
37+
return_value=([], {}))
3838
self.client.get_profile.return_value = user_profile
3939
model = Model(self.controller)
4040
return model
@@ -82,7 +82,7 @@ def test_init_InvalidAPIKey_response(self, mocker, initial_data):
8282
return_value=({}, set(), [], []))
8383
self.classify_unread_counts = mocker.patch(
8484
'zulipterminal.model.classify_unread_counts',
85-
return_value=[])
85+
return_value=([], {}))
8686

8787
with pytest.raises(ServerConnectionFailure) as e:
8888
model = Model(self.controller)
@@ -104,7 +104,7 @@ def test_init_ZulipError_exception(self, mocker, initial_data,
104104
return_value=({}, set(), [], []))
105105
self.classify_unread_counts = mocker.patch(
106106
'zulipterminal.model.classify_unread_counts',
107-
return_value=[])
107+
return_value=([], {}))
108108

109109
with pytest.raises(ServerConnectionFailure) as e:
110110
model = Model(self.controller)
@@ -441,7 +441,7 @@ def test_success_get_messages(self, mocker, messages_successful_response,
441441
return_value=({}, set(), [], []))
442442
self.classify_unread_counts = mocker.patch(
443443
'zulipterminal.model.classify_unread_counts',
444-
return_value=[])
444+
return_value=([], {}))
445445

446446
# Setup mocks before calling get_messages
447447
self.client.get_messages.return_value = messages_successful_response
@@ -481,7 +481,7 @@ def test_get_message_false_first_anchor(
481481
return_value=({}, set(), [], []))
482482
self.classify_unread_counts = mocker.patch(
483483
'zulipterminal.model.classify_unread_counts',
484-
return_value=[])
484+
return_value=([], {}))
485485

486486
# Setup mocks before calling get_messages
487487
messages_successful_response['anchor'] = 0
@@ -514,7 +514,7 @@ def test_fail_get_messages(self, mocker, error_response,
514514
return_value=({}, set(), [], []))
515515
self.classify_unread_counts = mocker.patch(
516516
'zulipterminal.model.classify_unread_counts',
517-
return_value=[])
517+
return_value=([], {}))
518518

519519
# Setup mock before calling get_messages
520520
# FIXME This has no influence on the result
@@ -593,7 +593,7 @@ def test__update_initial_data_raises_exception(self, mocker, initial_data):
593593
return_value=({}, set(), [], []))
594594
self.classify_unread_counts = mocker.patch(
595595
'zulipterminal.model.classify_unread_counts',
596-
return_value=[])
596+
return_value=([], {}))
597597

598598
# Setup mocks before calling get_messages
599599
self.client.register.return_value = initial_data
@@ -627,7 +627,7 @@ def test_get_all_users(self, mocker, initial_data, user_list, user_dict,
627627
return_value=({}, set(), [], []))
628628
self.classify_unread_counts = mocker.patch(
629629
'zulipterminal.model.classify_unread_counts',
630-
return_value=[])
630+
return_value=([], {}))
631631
model = Model(self.controller)
632632
assert model.user_dict == user_dict
633633
assert model.users == user_list

zulipterminal/helper.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'search': Set[int], # {message_id, ...}
4040
# Downloaded message data
4141
'messages': Dict[int, Message], # message_id: Message
42+
'unread_msgs': Dict[int, Dict[str, Any]], # message_id: Dict
4243
})
4344

4445
initial_index = Index(
@@ -54,6 +55,7 @@
5455
topics=defaultdict(list),
5556
search=set(),
5657
messages=defaultdict(dict),
58+
unread_msgs=defaultdict(dict),
5759
)
5860

5961

@@ -362,9 +364,11 @@ def index_messages(messages: List[Message],
362364
return index
363365

364366

365-
def classify_unread_counts(model: Any) -> UnreadCounts:
367+
def classify_unread_counts(model: Any) -> Tuple[UnreadCounts,
368+
Dict[int, Dict[str, Any]]]:
366369
# TODO: support group pms
367370
unread_msg_counts = model.initial_data['unread_msgs']
371+
unread_msgs = {} # type: Dict[int, Dict[str, Any]]
368372

369373
unread_counts = UnreadCounts(
370374
all_msg=0,
@@ -377,13 +381,22 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
377381

378382
for pm in unread_msg_counts['pms']:
379383
count = len(pm['unread_message_ids'])
384+
pm_data = {'type': 'private', 'display_recipient': pm['sender_id']}
385+
unread_msgs.update(dict(zip(pm['unread_message_ids'], [pm_data]*count)))
380386
unread_counts['unread_pms'][pm['sender_id']] = count
381387
unread_counts['all_msg'] += count
382388
unread_counts['all_pms'] += count
383389

384390
for stream in unread_msg_counts['streams']:
385391
count = len(stream['unread_message_ids'])
386392
stream_id = stream['stream_id']
393+
stream_name = model.stream_dict[stream_id]['name']
394+
sender_ids = stream['sender_ids']
395+
sender_ids = frozenset(map(int, sender_ids))
396+
stream_data = {'type': 'stream', 'stream_id': stream_id, 'subject':
397+
stream['topic'], 'display_recipient': stream_name,
398+
'sender_ids': sender_ids}
399+
unread_msgs.update(dict(zip(stream['unread_message_ids'], [stream_data]*count)))
387400
if [model.stream_dict[stream_id]['name'],
388401
stream['topic']] in model.muted_topics:
389402
continue
@@ -400,11 +413,13 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
400413
count = len(group_pm['unread_message_ids'])
401414
user_ids = group_pm['user_ids_string'].split(',')
402415
user_ids = frozenset(map(int, user_ids))
416+
huddle_data = {'type': 'private', 'display_recipient': user_ids}
417+
unread_msgs.update(dict(zip(group_pm['unread_message_ids'], [huddle_data]*count)))
403418
unread_counts['unread_huddles'][user_ids] = count
404419
unread_counts['all_msg'] += count
405420
unread_counts['all_pms'] += count
406421

407-
return unread_counts
422+
return unread_counts, unread_msgs
408423

409424

410425
def match_user(user: Any, text: str) -> bool:

zulipterminal/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def __init__(self, controller: Any) -> None:
116116
self.user_group_by_id = {} # type: Dict[int, Dict[str, Any]]
117117
self.user_group_names = self._group_info_from_realm_user_groups(groups)
118118

119-
self.unread_counts = classify_unread_counts(self)
119+
unread_data = classify_unread_counts(self)
120+
self.unread_counts, self.index['unread_msgs'] = unread_data
120121

121122
self.fetch_all_topics(workers=5)
122123

0 commit comments

Comments
 (0)