Skip to content

Commit 7a1b14b

Browse files
committed
refactor: helper: Extract updating unread to function in set_count.
Having a nested function allows us to discontinue the use of the `key` variable which can be useful if multiple sections of unread_counts need to be updated for a message type. It includes type annotation changes such as: * `key` passed into the nested function is typed using TypeVar. * Message has been typed using a TypedDict so that mypy can determine the type of `key`. Note the use of Totality here because stream messages/pms/huddles do not have the same attributes. This change cannot be split into a prior commit because that will require multiple type definitions of the same `key` variable. * An alternate declaration of `messages` in initial_index is necessary due to a mypy bug - python/mypy#7217 - while using TypeDict with defaultdict.
1 parent 49d3fa5 commit 7a1b14b

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

zulipterminal/helper.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@
2121
LINUX = platform.system() == "Linux"
2222
WSL = 'microsoft' in platform.release().lower()
2323

24-
Message = Dict[str, Any]
24+
Message = TypedDict('Message', {
25+
'id': int,
26+
'sender_id': int,
27+
'content': str,
28+
'recipient_id': int,
29+
'timestamp': int,
30+
'client': str,
31+
'subject': str,
32+
'topic_links': List[str],
33+
'is_me_message': bool,
34+
'reactions': List[Dict[str, Any]],
35+
'submessages': List[Dict[str, Any]],
36+
'flags': List[str],
37+
'sender_full_name': str,
38+
'sender_short_name': str,
39+
'sender_email': str,
40+
'sender_realm_str': str,
41+
'display_recipient': Any,
42+
'type': str,
43+
'stream_id': int,
44+
'avatar_url': str,
45+
'content_type': str,
46+
}, total=False)
2547

2648
Index = TypedDict('Index', {
2749
'pointer': Dict[str, Union[int, Set[None]]], # narrow_str, message_id
@@ -53,7 +75,7 @@
5375
edited_messages=set(),
5476
topics=defaultdict(list),
5577
search=set(),
56-
messages=defaultdict(dict),
78+
messages=defaultdict(lambda: Message()),
5779
)
5880

5981

@@ -92,30 +114,32 @@ def _set_count_in_model(new_count: int, changed_messages: List[Message],
92114
but updates `unread_counts` (which can update the model
93115
if it's passed in, but is not tied to it).
94116
"""
95-
for message in changed_messages:
96-
if message['type'] == 'stream':
97-
key = (message['stream_id'], message['subject'])
98-
unreads = unread_counts['unread_topics']
99-
# self-pm has only one display_recipient
100-
# 1-1 pms have 2 display_recipient
101-
elif len(message['display_recipient']) <= 2:
102-
key = message['sender_id']
103-
unreads = unread_counts['unread_pms'] # type: ignore
104-
else: # If it's a group pm
105-
key = frozenset( # type: ignore
106-
recipient['id'] for recipient in message['display_recipient']
107-
)
108-
unreads = unread_counts['unread_huddles'] # type: ignore
117+
# broader unread counts (for all_*) are updated
118+
# later conditionally in _set_count_in_view.
119+
KeyT = TypeVar('KeyT')
109120

110-
# broader unread counts (for all_* and streams) are updated
111-
# later conditionally.
121+
def update_unreads(unreads: Dict[KeyT, int], key: KeyT) -> None:
112122
if key in unreads:
113123
unreads[key] += new_count
114124
if unreads[key] == 0:
115125
unreads.pop(key)
116126
elif new_count == 1:
117127
unreads[key] = new_count
118128

129+
for message in changed_messages:
130+
if message['type'] == 'stream':
131+
stream_id = message['stream_id']
132+
update_unreads(unread_counts['unread_topics'],
133+
(stream_id, message['subject']))
134+
# self-pm has only one display_recipient
135+
# 1-1 pms have 2 display_recipient
136+
elif len(message['display_recipient']) <= 2:
137+
update_unreads(unread_counts['unread_pms'], message['sender_id'])
138+
else: # If it's a group pm
139+
update_unreads(unread_counts['unread_huddles'],
140+
frozenset(recipient['id'] for recipient
141+
in message['display_recipient']))
142+
119143

120144
def _set_count_in_view(controller: Any, new_count: int,
121145
changed_messages: List[Message],

0 commit comments

Comments
 (0)