message_edit: Remove get_editability function.

This commit removes get_editability function and we can instead
use is_content_editable and is_topic_editable/can_move_message
functions to check the permissions. We can remove get_editability
since there is no longer a sort order to the possibilities and
it is not necessary that if a user has permission to edit content
then they can edit the topics also.

So, this commit adds is_message_editable_ignoring_permissions
check to is_content_editable and also modifies the tests accordingly.
This commit is contained in:
Sahil Batra 2022-12-12 20:07:14 +05:30 committed by Tim Abbott
parent 98c4e11d0b
commit d0626bbaef
4 changed files with 28 additions and 101 deletions

View File

@ -9,104 +9,75 @@ const {page_params} = require("../zjsunit/zpage_params");
page_params.realm_community_topic_editing_limit_seconds = 259200;
const message_edit = zrequire("message_edit");
const settings_config = zrequire("settings_config");
const get_editability = message_edit.get_editability;
const editability_types = message_edit.editability_types;
const is_content_editable = message_edit.is_content_editable;
const settings_data = mock_esm("../../static/js/settings_data");
run_test("get_editability", ({override}) => {
override(settings_data, "user_can_move_messages_to_another_topic", () => true);
run_test("is_content_editable", () => {
// You can't edit a null message
assert.equal(get_editability(null), editability_types.NO);
assert.equal(is_content_editable(null), false);
// You can't edit a message you didn't send
assert.equal(
get_editability({
is_content_editable({
sent_by_me: false,
}),
editability_types.NO,
false,
);
// Failed request are currently not editable (though we want to
// change this back).
assert.equal(
get_editability({
is_content_editable({
sent_by_me: true,
failed_request: true,
}),
editability_types.NO,
false,
);
// Locally echoed messages are not editable, since the message hasn't
// finished being sent yet.
assert.equal(
get_editability({
is_content_editable({
sent_by_me: true,
locally_echoed: true,
}),
editability_types.NO,
false,
);
// For the rest of these tests, we only consider messages sent by the
// user, and that were successfully sent (i.e. no failed_request or local_id)
let message = {
const message = {
sent_by_me: true,
};
page_params.realm_allow_message_editing = false;
assert.equal(get_editability(message), editability_types.NO);
assert.equal(is_content_editable(message), false);
page_params.realm_allow_message_editing = true;
// Limit of 0 means no time limit on editing messages
page_params.realm_message_content_edit_limit_seconds = null;
assert.equal(get_editability(message), editability_types.CONTENT_ONLY);
assert.equal(is_content_editable(message), true);
page_params.realm_message_content_edit_limit_seconds = 10;
const now = new Date();
const current_timestamp = now / 1000;
message.timestamp = current_timestamp - 60;
// Have 55+10 > 60 seconds from message.timestamp to edit the message; we're good!
assert.equal(get_editability(message, 55), editability_types.CONTENT_ONLY);
assert.equal(is_content_editable(message, 55), true);
// It's been 60 > 45+10 since message.timestamp. When realm_allow_message_editing
// is true, we can edit the topic if there is one.
assert.equal(get_editability(message, 45), editability_types.NO);
assert.equal(is_content_editable(message, 45), false);
// Right now, we prevent users from editing widgets.
message.submessages = ["/poll"];
assert.equal(get_editability(message, 55), editability_types.NO);
assert.equal(is_content_editable(message, 55), false);
delete message.submessages;
message.type = "private";
assert.equal(get_editability(message, 45), editability_types.NO);
assert.equal(is_content_editable(message, 45), false);
assert.equal(get_editability(message, 55), editability_types.CONTENT_ONLY);
assert.equal(is_content_editable(message, 55), true);
// If we don't pass a second argument, treat it as 0
assert.equal(get_editability(message), editability_types.NO);
message = {
sent_by_me: false,
type: "stream",
};
page_params.realm_edit_topic_policy =
settings_config.common_message_policy_values.by_everyone.code;
page_params.realm_allow_message_editing = true;
page_params.realm_message_content_edit_limit_seconds = null;
page_params.realm_community_topic_editing_limit_seconds = 50;
page_params.is_admin = false;
message.timestamp = current_timestamp - 60;
assert.equal(get_editability(message), editability_types.NO);
page_params.is_admin = true;
assert.equal(get_editability(message), editability_types.TOPIC_ONLY);
page_params.is_admin = false;
page_params.realm_community_topic_editing_limit_seconds = 259200;
assert.equal(get_editability(message), editability_types.TOPIC_ONLY);
message.sent_by_me = true;
assert.equal(get_editability(message), editability_types.FULL);
page_params.realm_allow_message_editing = false;
assert.equal(get_editability(message), editability_types.TOPIC_ONLY);
assert.equal(is_content_editable(message), false);
});
run_test("is_topic_editable", ({override}) => {

View File

@ -44,19 +44,6 @@ export let notify_old_thread_default = false;
export let notify_new_thread_default = true;
export const editability_types = {
NO: 1,
// Note: TOPIC_ONLY does not include stream messages with no topic sent
// by someone else. You can edit the topic of such a message by editing
// the topic of the whole recipient_row it appears in, but you can't
// directly edit the topic of such a message.
// Similar story for messages whose topic you can change only because
// you are an admin.
TOPIC_ONLY: 3,
CONTENT_ONLY: 4,
FULL: 5,
};
export function is_topic_editable(message, edit_limit_seconds_buffer = 0) {
if (!is_message_editable_ignoring_permissions(message)) {
return false;
@ -125,7 +112,11 @@ export function is_message_editable_ignoring_permissions(message) {
return true;
}
function is_content_editable(message, edit_limit_seconds_buffer = 0) {
export function is_content_editable(message, edit_limit_seconds_buffer = 0) {
if (!is_message_editable_ignoring_permissions(message)) {
return false;
}
if (!page_params.realm_allow_message_editing) {
return false;
}
@ -153,29 +144,6 @@ function is_content_editable(message, edit_limit_seconds_buffer = 0) {
return false;
}
export function get_editability(message, edit_limit_seconds_buffer = 0) {
if (!is_message_editable_ignoring_permissions(message)) {
return editability_types.NO;
}
const can_edit_topic = is_topic_editable(message, edit_limit_seconds_buffer);
const can_edit_content = is_content_editable(message, edit_limit_seconds_buffer);
if (can_edit_content && can_edit_topic) {
return editability_types.FULL;
}
if (can_edit_topic && !can_edit_content) {
return editability_types.TOPIC_ONLY;
}
if (can_edit_content && !can_edit_topic) {
return editability_types.CONTENT_ONLY;
}
return editability_types.NO;
}
export function get_deletability(message) {
if (page_params.is_admin) {
return true;
@ -441,7 +409,6 @@ function edit_message($row, raw_content) {
// If you change this number also change edit_limit_buffer in
// zerver.actions.message_edit.check_update_message
const seconds_left_buffer = 5;
const editability = get_editability(message, seconds_left_buffer);
const max_file_upload_size = page_params.max_file_upload_size_mib;
let file_upload_enabled = false;
@ -449,8 +416,7 @@ function edit_message($row, raw_content) {
file_upload_enabled = true;
}
const is_editable =
editability === editability_types.FULL || editability === editability_types.CONTENT_ONLY;
const is_editable = is_content_editable(message, seconds_left_buffer);
const $form = $(
render_message_edit_form({
@ -900,11 +866,7 @@ export function edit_last_sent_message() {
return;
}
const msg_editability_type = get_editability(msg, 5);
if (
msg_editability_type !== editability_types.FULL &&
msg_editability_type !== editability_types.CONTENT_ONLY
) {
if (!is_content_editable(msg, 5)) {
return;
}

View File

@ -19,17 +19,14 @@ export function get_actions_popover_content_context(message_id) {
muted_users.is_user_muted(message.sender_id) &&
!message_container.is_hidden &&
not_spectator;
const editability = message_edit.get_editability(message);
const is_content_editable = message_edit.is_content_editable(message);
const can_move_message = message_edit.can_move_message(message);
let editability_menu_item;
let move_message_menu_item;
let view_source_menu_item;
if (
editability === message_edit.editability_types.FULL ||
editability === message_edit.editability_types.CONTENT_ONLY
) {
if (is_content_editable) {
editability_menu_item = $t({defaultMessage: "Edit message"});
} else {
view_source_menu_item = $t({defaultMessage: "View message source"});

View File

@ -142,10 +142,7 @@ function message_hover($message_row) {
}
// But the message edit hover icon is determined by whether the message is still editable
const editability = message_edit.get_editability(message);
const is_content_editable =
editability === message_edit.editability_types.FULL ||
editability === message_edit.editability_types.CONTENT_ONLY;
const is_content_editable = message_edit.is_content_editable(message);
const can_move_message = message_edit.can_move_message(message);
const args = {