Skip to content

Commit b91b618

Browse files
committed
Add unread_msg_ids datastructure to index
Fixes #496
1 parent a1faf08 commit b91b618

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

tests/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ def empty_index():
480480
stream_msg_template['id']: stream_msg_template,
481481
pm_template['id']: pm_template,
482482
group_pm_template['id']: group_pm_template,
483-
})
483+
}),
484+
'unread_msg_ids': set()
484485
})
485486

486487

@@ -704,10 +705,10 @@ def stream_dict(streams_fixture):
704705
@pytest.fixture
705706
def classified_unread_counts():
706707
"""
707-
Unread counts return by
708+
Unread counts and unread_msg_ids returned by
708709
helper.classify_unread_counts function.
709710
"""
710-
return {
711+
return ({
711712
'all_msg': 12,
712713
'all_pms': 8,
713714
'unread_topics': {
@@ -726,4 +727,4 @@ def classified_unread_counts():
726727
1000: 3,
727728
99: 1
728729
}
729-
}
730+
}, {1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 101, 102})

tests/helper/test_helper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ def test_classify_unread_counts(mocker, initial_data, stream_dict,
183183
model.initial_data = initial_data
184184
model.muted_topics = muted_topics
185185
model.muted_streams = muted_streams
186-
assert classify_unread_counts(model) == dict(classified_unread_counts,
187-
**vary_in_unreads)
186+
assert classify_unread_counts(model) == (dict(classified_unread_counts[0],
187+
**vary_in_unreads),
188+
classified_unread_counts[1])
188189

189190

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

tests/model/test_model.py

Lines changed: 6 additions & 6 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=([], set()))
3838
self.client.get_profile.return_value = user_profile
3939
model = Model(self.controller)
4040
return model
@@ -436,7 +436,7 @@ def test_success_get_messages(self, mocker, messages_successful_response,
436436
return_value=({}, set(), [], []))
437437
self.classify_unread_counts = mocker.patch(
438438
'zulipterminal.model.classify_unread_counts',
439-
return_value=[])
439+
return_value=([], set()))
440440

441441
# Setup mocks before calling get_messages
442442
self.client.get_messages.return_value = messages_successful_response
@@ -476,7 +476,7 @@ def test_get_message_false_first_anchor(
476476
return_value=({}, set(), [], []))
477477
self.classify_unread_counts = mocker.patch(
478478
'zulipterminal.model.classify_unread_counts',
479-
return_value=[])
479+
return_value=([], set()))
480480

481481
# Setup mocks before calling get_messages
482482
messages_successful_response['anchor'] = 0
@@ -509,7 +509,7 @@ def test_fail_get_messages(self, mocker, error_response,
509509
return_value=({}, set(), [], []))
510510
self.classify_unread_counts = mocker.patch(
511511
'zulipterminal.model.classify_unread_counts',
512-
return_value=[])
512+
return_value=([], set()))
513513

514514
# Setup mock before calling get_messages
515515
# FIXME This has no influence on the result
@@ -588,7 +588,7 @@ def test__update_initial_data_raises_exception(self, mocker, initial_data):
588588
return_value=({}, set(), [], []))
589589
self.classify_unread_counts = mocker.patch(
590590
'zulipterminal.model.classify_unread_counts',
591-
return_value=[])
591+
return_value=([], set()))
592592

593593
# Setup mocks before calling get_messages
594594
self.client.register.return_value = initial_data
@@ -622,7 +622,7 @@ def test_get_all_users(self, mocker, initial_data, user_list, user_dict,
622622
return_value=({}, set(), [], []))
623623
self.classify_unread_counts = mocker.patch(
624624
'zulipterminal.model.classify_unread_counts',
625-
return_value=[])
625+
return_value=([], set()))
626626
model = Model(self.controller)
627627
assert model.user_dict == user_dict
628628
assert model.users == user_list

zulipterminal/helper.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
'search': Set[int], # {message_id, ...}
3939
# Downloaded message data
4040
'messages': Dict[int, Message], # message_id: Message
41+
# unread message data; additional data in model.initial_data['unread_msgs']
42+
'unread_msg_ids': Set[int] # {message_ids, ...}
4143
})
4244

4345
initial_index = Index(
@@ -52,6 +54,7 @@
5254
topics=defaultdict(list),
5355
search=set(),
5456
messages=defaultdict(dict),
57+
unread_msg_ids=set(),
5558
)
5659

5760

@@ -351,9 +354,10 @@ def index_messages(messages: List[Message],
351354
return index
352355

353356

354-
def classify_unread_counts(model: Any) -> UnreadCounts:
357+
def classify_unread_counts(model: Any) -> Tuple[UnreadCounts, Set[int]]:
355358
# TODO: support group pms
356359
unread_msg_counts = model.initial_data['unread_msgs']
360+
unread_msg_ids: Set[int] = set()
357361

358362
unread_counts = UnreadCounts(
359363
all_msg=0,
@@ -365,13 +369,17 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
365369
)
366370

367371
for pm in unread_msg_counts['pms']:
368-
count = len(pm['unread_message_ids'])
372+
message_ids = pm['unread_message_ids']
373+
unread_msg_ids.update(message_ids)
374+
count = len(message_ids)
369375
unread_counts['unread_pms'][pm['sender_id']] = count
370376
unread_counts['all_msg'] += count
371377
unread_counts['all_pms'] += count
372378

373379
for stream in unread_msg_counts['streams']:
374-
count = len(stream['unread_message_ids'])
380+
message_ids = stream['unread_message_ids']
381+
unread_msg_ids.update(message_ids)
382+
count = len(message_ids)
375383
stream_id = stream['stream_id']
376384
if [model.stream_dict[stream_id]['name'],
377385
stream['topic']] in model.muted_topics:
@@ -386,14 +394,16 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
386394

387395
# store unread count of group pms in `unread_huddles`
388396
for group_pm in unread_msg_counts['huddles']:
389-
count = len(group_pm['unread_message_ids'])
397+
message_ids = group_pm['unread_message_ids']
398+
unread_msg_ids.update(message_ids)
399+
count = len(message_ids)
390400
user_ids = group_pm['user_ids_string'].split(',')
391401
user_ids = frozenset(map(int, user_ids))
392402
unread_counts['unread_huddles'][user_ids] = count
393403
unread_counts['all_msg'] += count
394404
unread_counts['all_pms'] += count
395405

396-
return unread_counts
406+
return unread_counts, unread_msg_ids
397407

398408

399409
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_msg_ids'] = unread_data
120121

121122
self.fetch_all_topics(workers=5)
122123

0 commit comments

Comments
 (0)