|
2 | 2 | import time |
3 | 3 | from collections import OrderedDict, defaultdict |
4 | 4 | from concurrent.futures import Future, ThreadPoolExecutor, wait |
| 5 | +from copy import deepcopy |
5 | 6 | from typing import ( |
6 | 7 | Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Optional, Set, |
7 | 8 | Tuple, Union, |
@@ -139,15 +140,26 @@ def __init__(self, controller: Any) -> None: |
139 | 140 | self.new_user_input = True |
140 | 141 | self._start_presence_updates() |
141 | 142 |
|
| 143 | + def narrow_with_canonical_topic(self) -> List[Any]: |
| 144 | + """ |
| 145 | + Returns the narrow with its topic name replaced with the invariant |
| 146 | + version that we maintain locally. |
| 147 | + """ |
| 148 | + narrow = deepcopy(self.narrow) |
| 149 | + if len(narrow) == 2 and narrow[1][0] == 'topic': |
| 150 | + narrow[1][1] = canonicalize_topic(narrow[1][1]) |
| 151 | + return narrow |
| 152 | + |
142 | 153 | def get_focus_in_current_narrow(self) -> Union[int, Set[None]]: |
143 | 154 | """ |
144 | 155 | Returns the focus in the current narrow. |
145 | 156 | For no existing focus this returns {}, otherwise the message ID. |
146 | 157 | """ |
147 | | - return self.index['pointer'][repr(self.narrow)] |
| 158 | + return self.index['pointer'][repr(self.narrow_with_canonical_topic())] |
148 | 159 |
|
149 | 160 | def set_focus_in_current_narrow(self, focus_message: int) -> None: |
150 | | - self.index['pointer'][repr(self.narrow)] = focus_message |
| 161 | + narrow_str = repr(self.narrow_with_canonical_topic()) |
| 162 | + self.index['pointer'][narrow_str] = focus_message |
151 | 163 |
|
152 | 164 | def is_search_narrow(self) -> bool: |
153 | 165 | """ |
@@ -373,7 +385,7 @@ def get_messages(self, *, |
373 | 385 | response = self.client.get_messages(message_filters=request) |
374 | 386 | if response['result'] == 'success': |
375 | 387 | self.index = index_messages(response['messages'], self, self.index) |
376 | | - narrow_str = repr(self.narrow) |
| 388 | + narrow_str = repr(self.narrow_with_canonical_topic()) |
377 | 389 | if first_anchor and response['anchor'] != 10000000000000000: |
378 | 390 | self.index['pointer'][narrow_str] = response['anchor'] |
379 | 391 | if 'found_newest' in response: |
@@ -863,8 +875,9 @@ def _handle_message_event(self, event: Event) -> None: |
863 | 875 | if 'read' not in message['flags']: |
864 | 876 | set_count([message['id']], self.controller, 1) |
865 | 877 |
|
| 878 | + narrow_str = repr(self.narrow_with_canonical_topic()) |
866 | 879 | if (hasattr(self.controller, 'view') |
867 | | - and self._have_last_message[repr(self.narrow)]): |
| 880 | + and self._have_last_message[narrow_str]): |
868 | 881 | msg_log = self.controller.view.message_view.log |
869 | 882 | if msg_log: |
870 | 883 | last_message = msg_log[-1].original_widget.message |
|
0 commit comments