Skip to content

Commit

Permalink
model: Add toggle_topic_resolved_status function to (un)resolve topics.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
srdeotarse committed Sep 4, 2022
1 parent 270cf37 commit 81ce518
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pytest import param as case
from zulip import Client, ZulipError

from zulipterminal.api_types import RESOLVED_TOPIC_PREFIX
from zulipterminal.config.symbols import STREAM_TOPIC_SEPARATOR
from zulipterminal.helper import initial_index, powerset
from zulipterminal.model import (
Expand Down Expand Up @@ -1009,6 +1010,48 @@ def test_can_user_edit_topic(
else:
report_error.assert_called_once_with(expected_response[case])

@pytest.mark.parametrize(
"update_response, user_response, msg_response, return_value",
[
(
{"result": "success"},
True,
{
"subject": "hi!",
"stream_id": 1,
"timestamp": 11662271397,
"id": 1,
},
RESOLVED_TOPIC_PREFIX + "hi!",
),
],
)
def test_toggle_topic_resolved_status(
self,
mocker,
model,
initial_data,
update_response,
user_response,
msg_response,
return_value,
):
model.initial_data = initial_data
initial_data["realm_community_topic_editing_limit_seconds"] = 259200
model.can_user_edit_topic = mocker.Mock(return_value=user_response)
model.get_latest_message_in_topic = mocker.Mock(return_value=msg_response)
model.update_stream_message = mocker.Mock(return_value=update_response)
report_error = model.controller.report_error

model.toggle_topic_resolved_status(
msg_response["stream_id"], msg_response["subject"]
)
model.update_stream_message.assert_called_once_with(
message_id=msg_response["id"],
topic=return_value,
propagate_mode="change_all",
)

@pytest.mark.parametrize(
"response, return_value",
[
Expand Down
30 changes: 30 additions & 0 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from zulipterminal import unicode_emojis
from zulipterminal.api_types import (
RESOLVED_TOPIC_PREFIX,
Composition,
EditPropagateMode,
Event,
Expand Down Expand Up @@ -638,6 +639,35 @@ def can_user_edit_topic(self) -> bool:
self.controller.report_error("User not found")
return False

def toggle_topic_resolved_status(self, stream_id: int, topic_name: str) -> None:
if self.can_user_edit_topic():
response = self.get_latest_message_in_topic(stream_id, topic_name)
if response:
time_since_msg_sent = time.time() - response["timestamp"]
# ZFL < 11, community_topic_editing_limit_seconds
# was hardcoded as int value in secs eg. 86400s (1 day)
if self.server_feature_level is None or self.server_feature_level >= 11:
edit_time_limit = self.initial_data[
"realm_community_topic_editing_limit_seconds"
]
else:
edit_time_limit = 259200
# Don't allow editing topic if time-limit exceeded.
if time_since_msg_sent >= edit_time_limit:
self.controller.report_error(
" Time limit for editing topic has been exceeded."
)
else:
if topic_name.startswith(RESOLVED_TOPIC_PREFIX):
topic_name = topic_name[2:]
else:
topic_name = RESOLVED_TOPIC_PREFIX + topic_name
self.update_stream_message(
message_id=response["id"],
topic=topic_name,
propagate_mode="change_all",
)

def update_stream_message(
self,
topic: str,
Expand Down

0 comments on commit 81ce518

Please sign in to comment.