Skip to content

Commit fcf1c56

Browse files
committed
model: Add un_resolve_topic 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 6954773 commit fcf1c56

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

tests/model/test_model.py

+72
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 (
@@ -1198,6 +1199,77 @@ def test_can_user_edit_topic(
11981199
else:
11991200
report_error.assert_called_once_with(expected_response[user_type][0])
12001201

1202+
@pytest.mark.parametrize(
1203+
"topic_name, msg_response, server_feature_level, return_value, expect_footer_error",
1204+
[
1205+
(
1206+
"hi!",
1207+
{
1208+
"subject": "hi!",
1209+
"timestamp": 11662271397,
1210+
"id": 1,
1211+
},
1212+
12,
1213+
RESOLVED_TOPIC_PREFIX + "hi!",
1214+
False,
1215+
),
1216+
(
1217+
"hi!",
1218+
{
1219+
"subject": "hi!",
1220+
"timestamp": 0,
1221+
"id": 1,
1222+
},
1223+
None,
1224+
" Time limit for editing topic has been exceeded.",
1225+
True,
1226+
),
1227+
(
1228+
"✔ hi!",
1229+
{
1230+
"subject": "✔ hi!",
1231+
"timestamp": 11662271397,
1232+
"id": 1,
1233+
},
1234+
10,
1235+
"hi!",
1236+
False,
1237+
),
1238+
],
1239+
)
1240+
def test_un_resolve_topic(
1241+
self,
1242+
mocker,
1243+
model,
1244+
initial_data,
1245+
topic_name,
1246+
msg_response,
1247+
server_feature_level,
1248+
return_value,
1249+
expect_footer_error,
1250+
stream_id=1,
1251+
):
1252+
model.initial_data = initial_data
1253+
model.server_feature_level = server_feature_level
1254+
initial_data["realm_community_topic_editing_limit_seconds"] = 86400
1255+
# If user can't edit topic, topic (un)resolve is disabled. Therefore,
1256+
# default return_value=True
1257+
model.can_user_edit_topic = mocker.Mock(return_value=True)
1258+
model.get_latest_message_in_topic = mocker.Mock(return_value=msg_response)
1259+
model.update_stream_message = mocker.Mock(return_value={"result": "success"})
1260+
report_error = model.controller.report_error
1261+
1262+
model.un_resolve_topic(stream_id, topic_name)
1263+
1264+
if not expect_footer_error:
1265+
model.update_stream_message.assert_called_once_with(
1266+
message_id=msg_response["id"],
1267+
topic=return_value,
1268+
propagate_mode="change_all",
1269+
)
1270+
else:
1271+
report_error.assert_called_once_with(return_value)
1272+
12011273
# NOTE: This tests only getting next-unread, not a fixed anchor
12021274
def test_success_get_messages(
12031275
self,

zulipterminal/model.py

+30
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,
@@ -695,6 +696,35 @@ def can_user_edit_topic(self) -> bool:
695696
self.controller.report_error("User not found")
696697
return False
697698

699+
def un_resolve_topic(self, stream_id: int, topic_name: str) -> None:
700+
if self.can_user_edit_topic():
701+
response = self.get_latest_message_in_topic(stream_id, topic_name)
702+
if response:
703+
time_since_msg_sent = time.time() - response["timestamp"]
704+
# ZFL < 11, community_topic_editing_limit_seconds
705+
# was hardcoded as int value in secs eg. 86400s (1 day) or None
706+
if self.server_feature_level is None or self.server_feature_level >= 11:
707+
edit_time_limit = self.initial_data[
708+
"realm_community_topic_editing_limit_seconds"
709+
]
710+
else:
711+
edit_time_limit = 86400
712+
# Don't allow editing topic if time-limit exceeded.
713+
if time_since_msg_sent >= edit_time_limit:
714+
self.controller.report_error(
715+
" Time limit for editing topic has been exceeded."
716+
)
717+
else:
718+
if topic_name.startswith(RESOLVED_TOPIC_PREFIX):
719+
topic_name = topic_name[2:]
720+
else:
721+
topic_name = RESOLVED_TOPIC_PREFIX + topic_name
722+
self.update_stream_message(
723+
message_id=response["id"],
724+
topic=topic_name,
725+
propagate_mode="change_all",
726+
)
727+
698728
def generate_all_emoji_data(
699729
self, custom_emoji: Dict[str, RealmEmojiData]
700730
) -> Tuple[NamedEmojiData, List[str]]:

0 commit comments

Comments
 (0)