Skip to content

Commit 602004e

Browse files
committed
helper/model: Update repr lookup for _have_last_message and pointer.
Also added narrow_with_canonical_topic().
1 parent 7e27abc commit 602004e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

zulipterminal/helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def index_messages(messages: List[Message],
245245
{
246246
'pointer': {
247247
'[]': 30 # str(ZulipModel.narrow)
248+
# NOTE: canonicalize_topic() is used for indexing.
248249
'[["stream", "verona"]]': 32,
249250
...
250251
}

zulipterminal/model.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
from collections import OrderedDict, defaultdict
44
from concurrent.futures import Future, ThreadPoolExecutor, wait
5+
from copy import deepcopy
56
from typing import (
67
Any, Callable, DefaultDict, Dict, FrozenSet, Iterable, List, Optional, Set,
78
Tuple, Union,
@@ -139,15 +140,26 @@ def __init__(self, controller: Any) -> None:
139140
self.new_user_input = True
140141
self._start_presence_updates()
141142

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+
142153
def get_focus_in_current_narrow(self) -> Union[int, Set[None]]:
143154
"""
144155
Returns the focus in the current narrow.
145156
For no existing focus this returns {}, otherwise the message ID.
146157
"""
147-
return self.index['pointer'][repr(self.narrow)]
158+
return self.index['pointer'][repr(self.narrow_with_canonical_topic())]
148159

149160
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
151163

152164
def is_search_narrow(self) -> bool:
153165
"""
@@ -373,7 +385,7 @@ def get_messages(self, *,
373385
response = self.client.get_messages(message_filters=request)
374386
if response['result'] == 'success':
375387
self.index = index_messages(response['messages'], self, self.index)
376-
narrow_str = repr(self.narrow)
388+
narrow_str = repr(self.narrow_with_canonical_topic())
377389
if first_anchor and response['anchor'] != 10000000000000000:
378390
self.index['pointer'][narrow_str] = response['anchor']
379391
if 'found_newest' in response:
@@ -863,8 +875,9 @@ def _handle_message_event(self, event: Event) -> None:
863875
if 'read' not in message['flags']:
864876
set_count([message['id']], self.controller, 1)
865877

878+
narrow_str = repr(self.narrow_with_canonical_topic())
866879
if (hasattr(self.controller, 'view')
867-
and self._have_last_message[repr(self.narrow)]):
880+
and self._have_last_message[narrow_str]):
868881
msg_log = self.controller.view.message_view.log
869882
if msg_log:
870883
last_message = msg_log[-1].original_widget.message

0 commit comments

Comments
 (0)