2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-05-26 21:20:11 +02:00
|
|
|
const {mock_esm, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_community_topic_editing_limit_seconds = 259200;
|
2016-12-29 23:50:24 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const message_edit = zrequire("message_edit");
|
2021-05-26 12:21:37 +02:00
|
|
|
const settings_config = zrequire("settings_config");
|
2016-12-29 23:50:24 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const get_editability = message_edit.get_editability;
|
|
|
|
const editability_types = message_edit.editability_types;
|
2016-12-29 23:50:24 +01:00
|
|
|
|
2021-05-26 21:20:11 +02:00
|
|
|
const settings_data = mock_esm("../../static/js/settings_data");
|
|
|
|
|
|
|
|
run_test("get_editability", (override) => {
|
|
|
|
override(settings_data, "user_can_edit_topic_of_any_message", () => true);
|
2016-12-29 23:50:24 +01:00
|
|
|
// You can't edit a null message
|
|
|
|
assert.equal(get_editability(null), editability_types.NO);
|
|
|
|
// You can't edit a message you didn't send
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
get_editability({
|
|
|
|
sent_by_me: false,
|
|
|
|
}),
|
|
|
|
editability_types.NO,
|
|
|
|
);
|
2016-12-29 23:50:24 +01:00
|
|
|
|
2017-07-18 14:44:29 +02:00
|
|
|
// Failed request are currently not editable (though we want to
|
|
|
|
// change this back).
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
get_editability({
|
|
|
|
sent_by_me: true,
|
|
|
|
failed_request: true,
|
|
|
|
}),
|
|
|
|
editability_types.NO,
|
|
|
|
);
|
2016-12-29 23:50:24 +01:00
|
|
|
|
|
|
|
// Locally echoed messages are not editable, since the message hasn't
|
|
|
|
// finished being sent yet.
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
get_editability({
|
|
|
|
sent_by_me: true,
|
|
|
|
local_id: "25",
|
|
|
|
}),
|
|
|
|
editability_types.NO,
|
|
|
|
);
|
2016-12-29 23:50:24 +01:00
|
|
|
|
|
|
|
// 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)
|
2019-11-02 00:06:25 +01:00
|
|
|
let message = {
|
2017-01-12 00:17:43 +01:00
|
|
|
sent_by_me: true,
|
2016-12-29 23:50:24 +01:00
|
|
|
};
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_allow_message_editing = false;
|
2016-12-29 23:50:24 +01:00
|
|
|
assert.equal(get_editability(message), editability_types.NO);
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
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 = 0;
|
2016-12-29 23:50:24 +01:00
|
|
|
assert.equal(get_editability(message), editability_types.FULL);
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_message_content_edit_limit_seconds = 10;
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = new Date();
|
|
|
|
const current_timestamp = now / 1000;
|
2016-12-29 23:50:24 +01:00
|
|
|
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.FULL);
|
|
|
|
// 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.
|
2020-07-15 01:29:15 +02:00
|
|
|
message.type = "stream";
|
2016-12-29 23:50:24 +01:00
|
|
|
assert.equal(get_editability(message, 45), editability_types.TOPIC_ONLY);
|
2021-01-31 18:23:48 +01:00
|
|
|
// Right now, we prevent users from editing widgets.
|
|
|
|
message.submessages = ["/poll"];
|
|
|
|
assert.equal(get_editability(message, 45), editability_types.TOPIC_ONLY);
|
|
|
|
delete message.submessages;
|
2020-07-15 01:29:15 +02:00
|
|
|
message.type = "private";
|
2016-12-29 23:50:24 +01:00
|
|
|
assert.equal(get_editability(message, 45), editability_types.NO_LONGER);
|
|
|
|
// If we don't pass a second argument, treat it as 0
|
|
|
|
assert.equal(get_editability(message), editability_types.NO_LONGER);
|
2018-04-25 23:15:04 +02:00
|
|
|
|
|
|
|
message = {
|
|
|
|
sent_by_me: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
type: "stream",
|
2018-04-25 23:15:04 +02:00
|
|
|
};
|
2021-05-26 12:21:37 +02:00
|
|
|
page_params.realm_edit_topic_policy =
|
|
|
|
settings_config.common_message_policy_values.by_everyone.code;
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.realm_allow_message_editing = true;
|
|
|
|
page_params.realm_message_content_edit_limit_seconds = 0;
|
|
|
|
page_params.realm_community_topic_editing_limit_seconds = 259200;
|
|
|
|
page_params.is_admin = false;
|
2018-04-25 23:15:04 +02:00
|
|
|
message.timestamp = current_timestamp - 60;
|
|
|
|
assert.equal(get_editability(message), editability_types.TOPIC_ONLY);
|
|
|
|
|
|
|
|
// Test `message_edit.is_topic_editable()`
|
|
|
|
assert.equal(message_edit.is_topic_editable(message), true);
|
|
|
|
|
|
|
|
message.sent_by_me = true;
|
2021-05-26 21:20:11 +02:00
|
|
|
override(settings_data, "user_can_edit_topic_of_any_message", () => false);
|
2018-04-25 23:15:04 +02:00
|
|
|
assert.equal(message_edit.is_topic_editable(message), true);
|
|
|
|
|
|
|
|
message.sent_by_me = false;
|
|
|
|
assert.equal(message_edit.is_topic_editable(message), false);
|
2018-10-09 09:54:57 +02:00
|
|
|
|
|
|
|
message.sent_by_me = false;
|
2020-12-01 00:57:57 +01:00
|
|
|
page_params.is_admin = true;
|
2018-10-09 09:54:57 +02:00
|
|
|
assert.equal(message_edit.is_topic_editable(message), true);
|
|
|
|
|
2021-05-26 21:20:11 +02:00
|
|
|
page_params.is_admin = false;
|
|
|
|
override(settings_data, "user_can_edit_topic_of_any_message", () => true);
|
|
|
|
assert.equal(message_edit.is_topic_editable(message), true);
|
|
|
|
|
|
|
|
message.timestamp = current_timestamp - 600000;
|
|
|
|
assert.equal(message_edit.is_topic_editable(message), false);
|
|
|
|
|
|
|
|
page_params.is_moderator = true;
|
|
|
|
assert.equal(message_edit.is_topic_editable(message), true);
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
page_params.realm_allow_message_editing = false;
|
2018-10-09 09:54:57 +02:00
|
|
|
assert.equal(message_edit.is_topic_editable(message), false);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-05-02 14:01:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_deletability", () => {
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.is_admin = true;
|
|
|
|
page_params.realm_allow_message_deleting = false;
|
|
|
|
page_params.realm_message_content_delete_limit_seconds = 0;
|
2018-05-02 14:01:18 +02:00
|
|
|
const message = {
|
|
|
|
sent_by_me: false,
|
|
|
|
locally_echoed: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Admin can always delete any message
|
|
|
|
assert.equal(message_edit.get_deletability(message), true);
|
|
|
|
|
|
|
|
// Non-admin can't delete message sent by others
|
2020-12-01 00:57:57 +01:00
|
|
|
page_params.is_admin = false;
|
2018-05-02 14:01:18 +02:00
|
|
|
assert.equal(message_edit.get_deletability(message), false);
|
|
|
|
|
|
|
|
// Locally echoed messages are not deletable
|
|
|
|
message.sent_by_me = true;
|
|
|
|
assert.equal(message_edit.get_deletability(message), false);
|
|
|
|
|
|
|
|
message.locally_echoed = false;
|
|
|
|
assert.equal(message_edit.get_deletability(message), false);
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
page_params.realm_allow_message_deleting = true;
|
2018-05-02 14:01:18 +02:00
|
|
|
assert.equal(message_edit.get_deletability(message), true);
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const current_timestamp = now / 1000;
|
|
|
|
message.timestamp = current_timestamp - 5;
|
|
|
|
|
2020-12-01 00:57:57 +01:00
|
|
|
page_params.realm_message_content_delete_limit_seconds = 10;
|
2018-05-02 14:01:18 +02:00
|
|
|
assert.equal(message_edit.get_deletability(message), true);
|
|
|
|
|
|
|
|
message.timestamp = current_timestamp - 60;
|
|
|
|
assert.equal(message_edit.get_deletability(message), false);
|
|
|
|
});
|