|
21 | 21 | LINUX = platform.system() == "Linux"
|
22 | 22 | WSL = 'microsoft' in platform.release().lower()
|
23 | 23 |
|
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) |
25 | 47 |
|
26 | 48 | Index = TypedDict('Index', {
|
27 | 49 | 'pointer': Dict[str, Union[int, Set[None]]], # narrow_str, message_id
|
|
53 | 75 | edited_messages=set(),
|
54 | 76 | topics=defaultdict(list),
|
55 | 77 | search=set(),
|
56 |
| - messages=defaultdict(dict), |
| 78 | + messages=defaultdict(lambda: Message()), |
57 | 79 | )
|
58 | 80 |
|
59 | 81 |
|
@@ -92,30 +114,32 @@ def _set_count_in_model(new_count: int, changed_messages: List[Message],
|
92 | 114 | but updates `unread_counts` (which can update the model
|
93 | 115 | if it's passed in, but is not tied to it).
|
94 | 116 | """
|
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') |
109 | 120 |
|
110 |
| - # broader unread counts (for all_* and streams) are updated |
111 |
| - # later conditionally. |
| 121 | + def update_unreads(unreads: Dict[KeyT, int], key: KeyT) -> None: |
112 | 122 | if key in unreads:
|
113 | 123 | unreads[key] += new_count
|
114 | 124 | if unreads[key] == 0:
|
115 | 125 | unreads.pop(key)
|
116 | 126 | elif new_count == 1:
|
117 | 127 | unreads[key] = new_count
|
118 | 128 |
|
| 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 | + |
119 | 143 |
|
120 | 144 | def _set_count_in_view(controller: Any, new_count: int,
|
121 | 145 | changed_messages: List[Message],
|
|
0 commit comments