message_edit: Show modal if user cannot resolve topics.

We show a modal if user is not allowed to resolve or unresolve
topics due to time limit. The modal just contains the text
mentioning user cannot resolve topic without stating the
count of messages that are within the time limit as we do
not recommend partial resolving of topics.

This commit does not include any changes for resolving or
unresolving topic using "Move topic" or "Move message" modals,
as we will still consider them as simple topic move and show
the same modal that is shown in general for moving message.
This commit is contained in:
Sahil Batra 2023-04-12 09:12:09 +05:30 committed by Tim Abbott
parent e08535ab3e
commit 8293bbea28
3 changed files with 123 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import * as resolved_topic from "../shared/src/resolved_topic";
import render_delete_message_modal from "../templates/confirm_dialog/confirm_delete_message.hbs";
import render_confirm_moving_messages_modal from "../templates/confirm_dialog/confirm_moving_messages.hbs";
import render_message_edit_form from "../templates/message_edit_form.hbs";
import render_resolve_topic_time_limit_error_modal from "../templates/resolve_topic_time_limit_error_modal.hbs";
import render_topic_edit_form from "../templates/topic_edit_form.hbs";
import * as blueslip from "./blueslip";
@ -30,6 +31,7 @@ import * as resize from "./resize";
import * as rows from "./rows";
import * as settings_data from "./settings_data";
import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
import * as ui_report from "./ui_report";
import * as upload from "./upload";
import * as util from "./util";
@ -603,9 +605,94 @@ export function start($row, edit_box_open_callback) {
});
}
function get_resolve_topic_time_limit_error_string(time_limit, time_limit_unit, topic_is_resolved) {
if (topic_is_resolved) {
if (time_limit_unit === "minute") {
return $t(
{
defaultMessage:
"You do not have permission to unresolve topics with messages older than {N, plural, one {# minute} other {# minutes}} in this organization.",
},
{N: time_limit},
);
} else if (time_limit_unit === "hour") {
return $t(
{
defaultMessage:
"You do not have permission to unresolve topics with messages older than {N, plural, one {# hour} other {# hours}} in this organization.",
},
{N: time_limit},
);
}
return $t(
{
defaultMessage:
"You do not have permission to unresolve topics with messages older than {N, plural, one {# day} other {# days}} in this organization.",
},
{N: time_limit},
);
}
if (time_limit_unit === "minute") {
return $t(
{
defaultMessage:
"You do not have permission to resolve topics with messages older than {N, plural, one {# minute} other {# minutes}} in this organization.",
},
{N: time_limit},
);
} else if (time_limit_unit === "hour") {
return $t(
{
defaultMessage:
"You do not have permission to resolve topics with messages older than {N, plural, one {# hour} other {# hours}} in this organization.",
},
{N: time_limit},
);
}
return $t(
{
defaultMessage:
"You do not have permission to resolve topics with messages older than {N, plural, one {# day} other {# days}} in this organization.",
},
{N: time_limit},
);
}
function handle_resolve_topic_failure_due_to_time_limit(topic_is_resolved) {
const time_limit_for_resolving_topic = timerender.get_time_limit_setting_in_appropriate_unit(
page_params.realm_move_messages_within_stream_limit_seconds,
);
const resolve_topic_time_limit_error_string = get_resolve_topic_time_limit_error_string(
time_limit_for_resolving_topic.value,
time_limit_for_resolving_topic.unit,
topic_is_resolved,
);
const html_body = render_resolve_topic_time_limit_error_modal({
topic_is_resolved,
resolve_topic_time_limit_error_string,
});
let modal_heading;
if (topic_is_resolved) {
modal_heading = $t_html({defaultMessage: "Could not unresolve topic"});
} else {
modal_heading = $t_html({defaultMessage: "Could not resolve topic"});
}
dialog_widget.launch({
html_heading: modal_heading,
html_body,
html_submit_button: $t_html({defaultMessage: "Close"}),
on_click() {},
single_footer_button: true,
focus_submit_on_open: true,
});
}
export function toggle_resolve_topic(message_id, old_topic_name) {
let new_topic_name;
if (resolved_topic.is_resolved(old_topic_name)) {
const topic_is_resolved = resolved_topic.is_resolved(old_topic_name);
if (topic_is_resolved) {
new_topic_name = resolved_topic.unresolve_name(old_topic_name);
} else {
new_topic_name = resolved_topic.resolve_name(old_topic_name);
@ -621,6 +708,11 @@ export function toggle_resolve_topic(message_id, old_topic_name) {
channel.patch({
url: "/json/messages/" + message_id,
data: request,
error(xhr) {
if (xhr.responseJSON.code === "MOVE_MESSAGES_TIME_LIMIT_EXCEEDED") {
handle_resolve_topic_failure_due_to_time_limit(topic_is_resolved);
}
},
});
}

View File

@ -455,3 +455,25 @@ export function get_full_datetime(time: Date): string {
return $t({defaultMessage: "{date} at {time}"}, {date: date_string, time: time_string});
}
type TimeLimitSetting = {
value: number;
unit: string;
};
export function get_time_limit_setting_in_appropriate_unit(
time_limit_in_seconds: number,
): TimeLimitSetting {
const time_limit_in_minutes = Math.floor(time_limit_in_seconds / 60);
if (time_limit_in_minutes < 60) {
return {value: time_limit_in_minutes, unit: "minute"};
}
const time_limit_in_hours = Math.floor(time_limit_in_minutes / 60);
if (time_limit_in_hours < 24) {
return {value: time_limit_in_hours, unit: "hour"};
}
const time_limit_in_days = Math.floor(time_limit_in_hours / 24);
return {value: time_limit_in_days, unit: "day"};
}

View File

@ -0,0 +1,8 @@
<p>
{{resolve_topic_time_limit_error_string}}
{{#if topic_is_resolved}}
{{t "Contact a moderator to unresolve this topic."}}
{{else}}
{{t "Contact a moderator to resolve this topic." }}
{{/if}}
</p>