Skip to content

Commit 81ce518

Browse files
committed
model: Add toggle_topic_resolved_status function to (un)resolve topics.
Fixes zulip#1075 The function calls get_latest_message_in_topic to fetch recent message in topic to be (un)resolved. It verifies user and editing conditions using can_user_edit_topic function and finally add or remove RESOLVED_TOPIC_PREFIX from topic name.
1 parent 270cf37 commit 81ce518

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

tests/model/test_model.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pytest import param as case
88
from zulip import Client, ZulipError
99

10+
from zulipterminal.api_types import RESOLVED_TOPIC_PREFIX
1011
from zulipterminal.config.symbols import STREAM_TOPIC_SEPARATOR
1112
from zulipterminal.helper import initial_index, powerset
1213
from zulipterminal.model import (
@@ -1009,6 +1010,48 @@ def test_can_user_edit_topic(
10091010
else:
10101011
report_error.assert_called_once_with(expected_response[case])
10111012

1013+
@pytest.mark.parametrize(
1014+
"update_response, user_response, msg_response, return_value",
1015+
[
1016+
(
1017+
{"result": "success"},
1018+
True,
1019+
{
1020+
"subject": "hi!",
1021+
"stream_id": 1,
1022+
"timestamp": 11662271397,
1023+
"id": 1,
1024+
},
1025+
RESOLVED_TOPIC_PREFIX + "hi!",
1026+
),
1027+
],
1028+
)
1029+
def test_toggle_topic_resolved_status(
1030+
self,
1031+
mocker,
1032+
model,
1033+
initial_data,
1034+
update_response,
1035+
user_response,
1036+
msg_response,
1037+
return_value,
1038+
):
1039+
model.initial_data = initial_data
1040+
initial_data["realm_community_topic_editing_limit_seconds"] = 259200
1041+
model.can_user_edit_topic = mocker.Mock(return_value=user_response)
1042+
model.get_latest_message_in_topic = mocker.Mock(return_value=msg_response)
1043+
model.update_stream_message = mocker.Mock(return_value=update_response)
1044+
report_error = model.controller.report_error
1045+
1046+
model.toggle_topic_resolved_status(
1047+
msg_response["stream_id"], msg_response["subject"]
1048+
)
1049+
model.update_stream_message.assert_called_once_with(
1050+
message_id=msg_response["id"],
1051+
topic=return_value,
1052+
propagate_mode="change_all",
1053+
)
1054+
10121055
@pytest.mark.parametrize(
10131056
"response, return_value",
10141057
[

zulipterminal/model.py

Lines changed: 30 additions & 0 deletions
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,
@@ -638,6 +639,35 @@ def can_user_edit_topic(self) -> bool:
638639
self.controller.report_error("User not found")
639640
return False
640641

642+
def toggle_topic_resolved_status(self, stream_id: int, topic_name: str) -> None:
643+
if self.can_user_edit_topic():
644+
response = self.get_latest_message_in_topic(stream_id, topic_name)
645+
if response:
646+
time_since_msg_sent = time.time() - response["timestamp"]
647+
# ZFL < 11, community_topic_editing_limit_seconds
648+
# was hardcoded as int value in secs eg. 86400s (1 day)
649+
if self.server_feature_level is None or self.server_feature_level >= 11:
650+
edit_time_limit = self.initial_data[
651+
"realm_community_topic_editing_limit_seconds"
652+
]
653+
else:
654+
edit_time_limit = 259200
655+
# Don't allow editing topic if time-limit exceeded.
656+
if time_since_msg_sent >= edit_time_limit:
657+
self.controller.report_error(
658+
" Time limit for editing topic has been exceeded."
659+
)
660+
else:
661+
if topic_name.startswith(RESOLVED_TOPIC_PREFIX):
662+
topic_name = topic_name[2:]
663+
else:
664+
topic_name = RESOLVED_TOPIC_PREFIX + topic_name
665+
self.update_stream_message(
666+
message_id=response["id"],
667+
topic=topic_name,
668+
propagate_mode="change_all",
669+
)
670+
641671
def update_stream_message(
642672
self,
643673
topic: str,

0 commit comments

Comments
 (0)