32
32
import requests
33
33
from typing_extensions import Literal , ParamSpec , TypedDict
34
34
35
- from zulipterminal .api_types import Composition , EmojiType , Message
35
+ from zulipterminal .api_types import (
36
+ RESOLVED_TOPIC_PREFIX ,
37
+ Composition ,
38
+ EmojiType ,
39
+ Message ,
40
+ )
36
41
from zulipterminal .config .keys import primary_key_for_command
37
42
from zulipterminal .config .regexes import (
38
43
REGEX_COLOR_3_DIGIT ,
@@ -110,6 +115,7 @@ class Index(TypedDict):
110
115
topic_msg_ids : Dict [int , Dict [str , Set [int ]]]
111
116
# Extra cached information
112
117
edited_messages : Set [int ] # {message_id, ...}
118
+ moved_messages : Set [int ]
113
119
topics : Dict [int , List [str ]] # {topic names, ...}
114
120
search : Set [int ] # {message_id, ...}
115
121
# Downloaded message data by message id
@@ -126,6 +132,7 @@ class Index(TypedDict):
126
132
stream_msg_ids_by_stream_id = defaultdict (set ),
127
133
topic_msg_ids = defaultdict (dict ),
128
134
edited_messages = set (),
135
+ moved_messages = set (),
129
136
topics = defaultdict (list ),
130
137
search = set (),
131
138
# mypy bug: https://github.com/python/mypy/issues/7217
@@ -305,6 +312,44 @@ def set_count(id_list: List[int], controller: Any, new_count: int) -> None:
305
312
controller .update_screen ()
306
313
307
314
315
+ def analyse_edit_history (
316
+ msg_id : int ,
317
+ index : Index ,
318
+ content_changed : bool ,
319
+ stream_changed : bool ,
320
+ current_topic : Any = None ,
321
+ old_topic : Any = None ,
322
+ ) -> None :
323
+ resolve_change = False
324
+ resolved_prefix = RESOLVED_TOPIC_PREFIX + " "
325
+ if content_changed or stream_changed :
326
+ index ["edited_messages" ].add (msg_id )
327
+ elif old_topic :
328
+ old_topic_resolved = old_topic .startswith (resolved_prefix )
329
+ current_topic_resolved = current_topic .startswith (resolved_prefix )
330
+ if not current_topic_resolved :
331
+ if old_topic_resolved :
332
+ if old_topic [2 :] != current_topic :
333
+ index ["moved_messages" ].add (msg_id )
334
+ if old_topic [2 :] == current_topic :
335
+ resolve_change = True
336
+ if not old_topic_resolved and not current_topic_resolved :
337
+ index ["moved_messages" ].add (msg_id )
338
+ else :
339
+ if old_topic_resolved and old_topic [2 :] != current_topic [2 :]:
340
+ index ["moved_messages" ].add (msg_id )
341
+ if not old_topic_resolved :
342
+ if current_topic [2 :] != old_topic :
343
+ index ["moved_messages" ].add (msg_id )
344
+ if current_topic [2 :] == old_topic :
345
+ resolve_change = True
346
+
347
+ else :
348
+ index ["edited_messages" ].add (msg_id )
349
+ if msg_id not in index ["moved_messages" ] and not resolve_change :
350
+ index ["edited_messages" ].add (msg_id )
351
+
352
+
308
353
def index_messages (messages : List [Message ], model : Any , index : Index ) -> Index :
309
354
"""
310
355
STRUCTURE OF INDEX
@@ -433,8 +478,26 @@ def index_messages(messages: List[Message], model: Any, index: Index) -> Index:
433
478
narrow = model .narrow
434
479
for msg in messages :
435
480
if "edit_history" in msg :
436
- index ["edited_messages" ].add (msg ["id" ])
437
-
481
+ stream_changed = False
482
+ content_changed = False
483
+ current_topic = None
484
+ old_topic = None
485
+ for edit_history_event in msg ["edit_history" ]:
486
+ if "prev_content" in edit_history_event :
487
+ content_changed = True
488
+ if "prev_stream" in edit_history_event :
489
+ stream_changed = True
490
+ if "prev_topic" in edit_history_event :
491
+ current_topic = edit_history_event ["topic" ]
492
+ old_topic = edit_history_event ["prev_topic" ]
493
+ analyse_edit_history (
494
+ msg ["id" ],
495
+ index ,
496
+ content_changed ,
497
+ stream_changed ,
498
+ current_topic ,
499
+ old_topic ,
500
+ )
438
501
index ["messages" ][msg ["id" ]] = msg
439
502
if not narrow :
440
503
index ["all_msg_ids" ].add (msg ["id" ])
0 commit comments