Skip to content

Commit 9d9068e

Browse files
committed
model: Handle (un)resolving topics.
Fixes zulip#1075 Add new get_latest_message_in_topic function to get most recent message sent by user in the topic for which TopicInfoView is toggled. Then check realm_community_topic_editing_limit_seconds before using update_stream_message to edit the topic with propagate mode as change_all to (un)resolve topics.
1 parent f668dea commit 9d9068e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

zulipterminal/model.py

+48
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from zulipterminal import unicode_emojis
3232
from zulipterminal.api_types import (
33+
RESOLVED_TOPIC_PREFIX,
3334
Composition,
3435
EditPropagateMode,
3536
Event,
@@ -564,6 +565,53 @@ def update_private_message(self, msg_id: int, content: str) -> bool:
564565
display_error_if_present(response, self.controller)
565566
return response["result"] == "success"
566567

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

0 commit comments

Comments
 (0)