Skip to content

Commit b212f6f

Browse files
Subhasish-Beheraneiljp
authored andcommitted
helper: Add a function analyse_edit_history.
The function analyse_edit_history is called from index _messages of helper.py. It decouples the edit/moved label adding mechanism from index_messages to be used later in _handle_update_messages_event.Stream/content changes comes under "edited" while topic editing checks cases related to resolve/unresolve etc which can go either way of "moved"/"edited". But moved label is applied to specific cases while rest of the cases are "edited" using else conditional(expect the resolve_change case).
1 parent 6d48472 commit b212f6f

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ def empty_index(
10731073
stream_msg_ids_by_stream_id=defaultdict(set, {}),
10741074
topic_msg_ids=defaultdict(dict, {}),
10751075
edited_messages=set(),
1076+
moved_messages=set(),
10761077
topics=defaultdict(list),
10771078
search=set(),
10781079
messages=defaultdict(

zulipterminal/helper.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
import requests
3333
from typing_extensions import Literal, ParamSpec, TypedDict
3434

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+
)
3641
from zulipterminal.config.keys import primary_key_for_command
3742
from zulipterminal.config.regexes import (
3843
REGEX_COLOR_3_DIGIT,
@@ -110,6 +115,7 @@ class Index(TypedDict):
110115
topic_msg_ids: Dict[int, Dict[str, Set[int]]]
111116
# Extra cached information
112117
edited_messages: Set[int] # {message_id, ...}
118+
moved_messages: Set[int]
113119
topics: Dict[int, List[str]] # {topic names, ...}
114120
search: Set[int] # {message_id, ...}
115121
# Downloaded message data by message id
@@ -126,6 +132,7 @@ class Index(TypedDict):
126132
stream_msg_ids_by_stream_id=defaultdict(set),
127133
topic_msg_ids=defaultdict(dict),
128134
edited_messages=set(),
135+
moved_messages=set(),
129136
topics=defaultdict(list),
130137
search=set(),
131138
# 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:
305312
controller.update_screen()
306313

307314

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+
308353
def index_messages(messages: List[Message], model: Any, index: Index) -> Index:
309354
"""
310355
STRUCTURE OF INDEX
@@ -433,8 +478,26 @@ def index_messages(messages: List[Message], model: Any, index: Index) -> Index:
433478
narrow = model.narrow
434479
for msg in messages:
435480
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+
)
438501
index["messages"][msg["id"]] = msg
439502
if not narrow:
440503
index["all_msg_ids"].add(msg["id"])

0 commit comments

Comments
 (0)