Skip to content

Commit 5687b88

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 5687b88

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

tests/conftest.py

Lines changed: 19 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,17 @@ def classified_unread_counts():
742743
1000: 3,
743744
99: 1
744745
}
745-
}
746+
}, {
747+
1: {'category': 'pm', 'sender_id': 1},
748+
2: {'category': 'pm', 'sender_id': 1},
749+
3: {'category': 'pm', 'sender_id': 2},
750+
4: {'category': 'stream', 'stream_id': 1000},
751+
5: {'category': 'stream', 'stream_id': 1000},
752+
6: {'category': 'stream', 'stream_id': 1000},
753+
7: {'category': 'stream', 'stream_id': 99},
754+
11: {'category': 'huddle', 'user_ids': frozenset({11, 12, 1001})},
755+
12: {'category': 'huddle', 'user_ids': frozenset({11, 12, 1001})},
756+
13: {'category': 'huddle', 'user_ids': frozenset({11, 12, 1001})},
757+
101: {'category': 'huddle', 'user_ids': frozenset({11, 12, 13, 1001})},
758+
102: {'category': 'huddle', 'user_ids': frozenset({11, 12, 13, 1001})},
759+
})

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: 20 additions & 6 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,
@@ -376,14 +380,21 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
376380
)
377381

378382
for pm in unread_msg_counts['pms']:
379-
count = len(pm['unread_message_ids'])
380-
unread_counts['unread_pms'][pm['sender_id']] = count
383+
unread_message_ids = pm['unread_message_ids']
384+
count = len(unread_message_ids)
385+
sender_id = pm['sender_id']
386+
pm_data = {'category': 'pm', 'sender_id': sender_id}
387+
unread_msgs.update(dict(zip(unread_message_ids, [pm_data]*count)))
388+
unread_counts['unread_pms'][sender_id] = count
381389
unread_counts['all_msg'] += count
382390
unread_counts['all_pms'] += count
383391

384392
for stream in unread_msg_counts['streams']:
385-
count = len(stream['unread_message_ids'])
393+
unread_message_ids = stream['unread_message_ids']
394+
count = len(unread_message_ids)
386395
stream_id = stream['stream_id']
396+
stream_data = {'category': 'stream', 'stream_id': stream_id}
397+
unread_msgs.update(dict(zip(unread_message_ids, [stream_data]*count)))
387398
if [model.stream_dict[stream_id]['name'],
388399
stream['topic']] in model.muted_topics:
389400
continue
@@ -397,14 +408,17 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
397408

398409
# store unread count of group pms in `unread_huddles`
399410
for group_pm in unread_msg_counts['huddles']:
400-
count = len(group_pm['unread_message_ids'])
411+
unread_message_ids = group_pm['unread_message_ids']
412+
count = len(unread_message_ids)
401413
user_ids = group_pm['user_ids_string'].split(',')
402414
user_ids = frozenset(map(int, user_ids))
415+
huddle_data = {'category': 'huddle', 'user_ids': user_ids}
416+
unread_msgs.update(dict(zip(unread_message_ids, [huddle_data]*count)))
403417
unread_counts['unread_huddles'][user_ids] = count
404418
unread_counts['all_msg'] += count
405419
unread_counts['all_pms'] += count
406420

407-
return unread_counts
421+
return unread_counts, unread_msgs
408422

409423

410424
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)