Skip to content

Commit 0fdf941

Browse files
committed
model: Handle (un)resolving topics.
Fixes zulip#1075 First using get_messages api method to get most recent message sent by user in the topic for which TopicInfoView is toggled. Then using fetch message to edit the topic and propagate mode as change_all to (un)resolve topics.
1 parent 84ce986 commit 0fdf941

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

zulipterminal/model.py

+45
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from zulipterminal import unicode_emojis
2828
from zulipterminal.api_types import (
29+
RESOLVED_TOPIC_PREFIX,
2930
Composition,
3031
EditPropagateMode,
3132
Event,
@@ -559,6 +560,50 @@ def update_private_message(self, msg_id: int, content: str) -> bool:
559560
display_error_if_present(response, self.controller)
560561
return response["result"] == "success"
561562

563+
def toggle_topic_resolved_status(self, stream_id: int, topic_name: str) -> bool:
564+
if self.initial_data.get("realm_allow_community_topic_editing", None):
565+
self.controller.report_error(" Editing topic is disabled")
566+
else:
567+
request: Dict[str, Any] = {
568+
"anchor": "newest",
569+
"num_before": 1,
570+
"num_after": 0,
571+
"narrow": [
572+
{
573+
"operator": "stream",
574+
"operand": self.stream_dict[stream_id]["name"],
575+
},
576+
{"operator": "topic", "operand": topic_name},
577+
],
578+
}
579+
response = self.client.get_messages(request)
580+
if response["messages"] != []:
581+
time_since_msg_sent = time.time() - response["messages"][0]["timestamp"]
582+
edit_time_limit = self.initial_data[
583+
"realm_community_topic_editing_limit_seconds"
584+
]
585+
# Don't allow editing topic if time-limit exceeded.
586+
if time_since_msg_sent <= edit_time_limit:
587+
topic_msg_id = response["messages"][0]["id"]
588+
if topic_name.startswith(RESOLVED_TOPIC_PREFIX):
589+
request = {
590+
"message_id": topic_msg_id,
591+
"topic": topic_name[2:],
592+
"propagate_mode": "change_all",
593+
}
594+
else:
595+
request = {
596+
"message_id": topic_msg_id,
597+
"topic": RESOLVED_TOPIC_PREFIX + topic_name,
598+
"propagate_mode": "change_all",
599+
}
600+
response = self.client.update_message(request)
601+
else:
602+
self.controller.report_error(
603+
" Time limit for editing topic has been exceeded."
604+
)
605+
return response["result"] == "success"
606+
562607
def update_stream_message(
563608
self,
564609
topic: str,

0 commit comments

Comments
 (0)