Skip to content

Commit 0ac15e8

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 6740933 commit 0ac15e8

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

tests/conftest.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ def empty_index():
495495
stream_msg_template['id']: stream_msg_template,
496496
pm_template['id']: pm_template,
497497
group_pm_template['id']: group_pm_template,
498-
})
498+
}),
499+
'unread_msgs': defaultdict(dict),
499500
})
500501

501502

@@ -734,10 +735,10 @@ def stream_dict(streams_fixture):
734735
@pytest.fixture
735736
def classified_unread_counts():
736737
"""
737-
Unread counts return by
738-
helper.classify_unread_counts function.
738+
Tuple of unread counts and unread_msg data
739+
returned by helper.classify_unread_counts function.
739740
"""
740-
return {
741+
return ({
741742
'all_msg': 12,
742743
'all_pms': 8,
743744
'unread_topics': {
@@ -756,7 +757,33 @@ def classified_unread_counts():
756757
1000: 3,
757758
99: 1
758759
}
759-
}
760+
}, {
761+
1: {'type': 'private', 'sender_id': 1},
762+
2: {'type': 'private', 'sender_id': 1},
763+
3: {'type': 'private', 'sender_id': 2},
764+
4: {'type': 'stream', 'display_recipient': 'Some general stream',
765+
'stream_id': 1000, 'subject': 'Some general unread topic',
766+
'sender_ids': frozenset({1, 2})},
767+
5: {'type': 'stream', 'display_recipient': 'Some general stream',
768+
'stream_id': 1000, 'subject': 'Some general unread topic',
769+
'sender_ids': frozenset({1, 2})},
770+
6: {'type': 'stream', 'display_recipient': 'Some general stream',
771+
'stream_id': 1000, 'subject': 'Some general unread topic',
772+
'sender_ids': frozenset({1, 2})},
773+
7: {'type': 'stream', 'display_recipient': 'Secret stream',
774+
'stream_id': 99, 'subject': 'Some private unread topic',
775+
'sender_ids': frozenset({1, 2})},
776+
11: {'type': 'private', 'display_recipient':
777+
frozenset({11, 12, 1001})},
778+
12: {'type': 'private', 'display_recipient':
779+
frozenset({11, 12, 1001})},
780+
13: {'type': 'private', 'display_recipient':
781+
frozenset({11, 12, 1001})},
782+
101: {'type': 'private', 'display_recipient':
783+
frozenset({11, 12, 13, 1001})},
784+
102: {'type': 'private', 'display_recipient':
785+
frozenset({11, 12, 13, 1001})},
786+
})
760787

761788
# --------------- UI Fixtures -----------------------------------------
762789

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 & 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

@@ -381,9 +383,11 @@ def index_messages(messages: List[Message],
381383
return index
382384

383385

384-
def classify_unread_counts(model: Any) -> UnreadCounts:
386+
def classify_unread_counts(model: Any) -> Tuple[UnreadCounts,
387+
Dict[int, Dict[str, Any]]]:
385388
# TODO: support group pms
386389
unread_msg_counts = model.initial_data['unread_msgs']
390+
unread_msgs = {} # type: Dict[int, Dict[str, Any]]
387391

388392
unread_counts = UnreadCounts(
389393
all_msg=0,
@@ -396,13 +400,24 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
396400

397401
for pm in unread_msg_counts['pms']:
398402
count = len(pm['unread_message_ids'])
403+
pm_data = {'type': 'private', 'sender_id': pm['sender_id']}
404+
unread_msgs.update(dict(zip(pm['unread_message_ids'],
405+
[pm_data]*count)))
399406
unread_counts['unread_pms'][pm['sender_id']] = count
400407
unread_counts['all_msg'] += count
401408
unread_counts['all_pms'] += count
402409

403410
for stream in unread_msg_counts['streams']:
404411
count = len(stream['unread_message_ids'])
405412
stream_id = stream['stream_id']
413+
stream_name = model.stream_dict[stream_id]['name']
414+
sender_ids = stream['sender_ids']
415+
sender_ids = frozenset(sender_ids)
416+
stream_data = {'type': 'stream', 'stream_id': stream_id, 'subject':
417+
stream['topic'], 'display_recipient': stream_name,
418+
'sender_ids': sender_ids}
419+
unread_msgs.update(dict(zip(stream['unread_message_ids'],
420+
[stream_data]*count)))
406421
if [model.stream_dict[stream_id]['name'],
407422
stream['topic']] in model.muted_topics:
408423
continue
@@ -419,11 +434,14 @@ def classify_unread_counts(model: Any) -> UnreadCounts:
419434
count = len(group_pm['unread_message_ids'])
420435
user_ids = group_pm['user_ids_string'].split(',')
421436
user_ids = frozenset(map(int, user_ids))
437+
huddle_data = {'type': 'private', 'display_recipient': user_ids}
438+
unread_msgs.update(dict(zip(group_pm['unread_message_ids'],
439+
[huddle_data]*count)))
422440
unread_counts['unread_huddles'][user_ids] = count
423441
unread_counts['all_msg'] += count
424442
unread_counts['all_pms'] += count
425443

426-
return unread_counts
444+
return unread_counts, unread_msgs
427445

428446

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