2022-12-10 23:09:41 +01:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2024-03-28 21:00:04 +01:00
|
|
|
const $ = require("./lib/zjquery");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {page_params} = require("./lib/zpage_params");
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
const {Filter} = zrequire("filter");
|
|
|
|
const {MessageList} = zrequire("message_list");
|
|
|
|
const message_lists = zrequire("message_lists");
|
2024-02-05 12:59:04 +01:00
|
|
|
|
2022-12-10 23:09:41 +01:00
|
|
|
const popover_menus_data = zrequire("popover_menus_data");
|
|
|
|
const people = zrequire("people");
|
2023-10-02 22:56:07 +02:00
|
|
|
const compose_state = zrequire("compose_state");
|
2024-08-15 07:29:15 +02:00
|
|
|
const user_groups = zrequire("user_groups");
|
2024-09-25 01:28:50 +02:00
|
|
|
const {MessageListData} = zrequire("message_list_data");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_current_user, set_realm} = zrequire("state_data");
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
const noop = function () {};
|
|
|
|
|
|
|
|
// Define MessageList stuff
|
|
|
|
function MessageListView() {
|
|
|
|
return {
|
|
|
|
maybe_rerender: noop,
|
|
|
|
append: noop,
|
|
|
|
prepend: noop,
|
|
|
|
clear_rendering_state: noop,
|
|
|
|
get_row: () => ({
|
|
|
|
find: () => ({
|
|
|
|
is: () => false,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
message_containers: new Map(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
mock_esm("../src/message_list_view", {
|
|
|
|
MessageListView,
|
|
|
|
});
|
|
|
|
mock_esm("../src/ui_util", {
|
|
|
|
listener_for_preferred_color_scheme_change: noop,
|
|
|
|
});
|
|
|
|
mock_esm("../src/hash_util", {
|
|
|
|
by_conversation_and_time_url: () => "conversation_and_time_url",
|
|
|
|
});
|
|
|
|
mock_esm("../src/stream_data", {
|
|
|
|
is_subscribed: () => true,
|
2024-09-27 14:33:13 +02:00
|
|
|
is_stream_archived: () => false,
|
2022-12-10 23:09:41 +01:00
|
|
|
});
|
2024-09-06 16:41:41 +02:00
|
|
|
mock_esm("../src/group_permission_settings", {
|
|
|
|
get_group_permission_setting_config() {
|
|
|
|
return {
|
|
|
|
allow_everyone_group: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2022-12-10 23:09:41 +01:00
|
|
|
|
2024-10-09 22:44:13 +02:00
|
|
|
const current_user = {};
|
|
|
|
set_current_user(current_user);
|
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
|
|
|
|
2022-12-10 23:09:41 +01:00
|
|
|
// Define test users
|
|
|
|
const mike = {
|
|
|
|
user_id: 1000,
|
|
|
|
full_name: "Test Mike",
|
|
|
|
email: "mike@example.com",
|
|
|
|
is_admin: false,
|
|
|
|
is_guest: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const bob = {
|
|
|
|
user_id: 2000,
|
|
|
|
full_name: "Test Bob",
|
|
|
|
email: "bob@example.com",
|
|
|
|
is_admin: false,
|
|
|
|
is_guest: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const me = {
|
|
|
|
user_id: 999,
|
|
|
|
full_name: "Test Myself",
|
|
|
|
email: "me@example.com",
|
|
|
|
is_admin: false,
|
|
|
|
is_guest: false,
|
|
|
|
};
|
|
|
|
|
2024-08-15 07:29:15 +02:00
|
|
|
const everyone = {
|
|
|
|
name: "role:everyone",
|
|
|
|
id: 2,
|
|
|
|
members: new Set([999, 1000, 2000]),
|
|
|
|
is_system_group: true,
|
|
|
|
direct_subgroup_ids: new Set([]),
|
|
|
|
};
|
|
|
|
user_groups.initialize({realm_user_groups: [everyone]});
|
|
|
|
|
2022-12-10 23:09:41 +01:00
|
|
|
// Helper functions:
|
|
|
|
function add_initialize_users() {
|
|
|
|
// Initialize people
|
|
|
|
people.init();
|
|
|
|
|
|
|
|
// Add users
|
|
|
|
people.add_active_user(mike);
|
|
|
|
people.add_active_user(bob);
|
|
|
|
people.add_active_user(me);
|
|
|
|
|
|
|
|
// Initialize current user
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function init_message_list() {
|
2024-01-07 16:38:29 +01:00
|
|
|
const filter = new Filter([]);
|
2022-12-10 23:09:41 +01:00
|
|
|
const list = new MessageList({
|
2024-09-25 01:28:50 +02:00
|
|
|
data: new MessageListData({
|
|
|
|
excludes_muted_topics: false,
|
|
|
|
filter,
|
|
|
|
}),
|
2022-12-10 23:09:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(list.empty(), true);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append message to message_list, also add container to message_lists
|
|
|
|
function add_message_with_view(list, messages) {
|
|
|
|
list.append(messages, true);
|
|
|
|
for (const message of messages) {
|
|
|
|
message_lists.current.view.message_containers.set(message.id, {
|
|
|
|
is_hidden: message.is_hidden,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function sets page parameters with no time constraints on editing the message.
|
|
|
|
// User is assumed to not be an admin.
|
2024-10-09 22:20:06 +02:00
|
|
|
function set_page_params_no_edit_restrictions({override}) {
|
2022-12-10 23:09:41 +01:00
|
|
|
page_params.is_spectator = false;
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_allow_message_editing", true);
|
|
|
|
override(realm, "realm_message_content_edit_limit_seconds", null);
|
|
|
|
override(realm, "realm_allow_edit_history", true);
|
|
|
|
override(realm, "realm_message_content_delete_limit_seconds", null);
|
|
|
|
override(realm, "realm_enable_read_receipts", true);
|
|
|
|
override(realm, "realm_move_messages_within_stream_limit_seconds", null);
|
2022-12-10 23:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test init function
|
|
|
|
function test(label, f) {
|
|
|
|
run_test(label, (helpers) => {
|
2024-03-28 21:00:04 +01:00
|
|
|
// Stubs for calculate_timestamp_widths()
|
|
|
|
$("<div>").css = noop;
|
|
|
|
$(":root").css = noop;
|
|
|
|
$("<div>").width = noop;
|
|
|
|
$("<div>").remove = noop;
|
|
|
|
|
2022-12-10 23:09:41 +01:00
|
|
|
// Clear stuff for testing environment
|
|
|
|
add_initialize_users();
|
|
|
|
message_lists.initialize();
|
|
|
|
f(helpers);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test functions
|
2024-10-09 21:21:41 +02:00
|
|
|
test("my_message_all_actions", ({override}) => {
|
2022-12-10 23:09:41 +01:00
|
|
|
// Set page parameters.
|
2024-10-09 22:20:06 +02:00
|
|
|
set_page_params_no_edit_restrictions({override});
|
|
|
|
override(realm, "realm_can_delete_any_message_group", everyone.id);
|
|
|
|
override(realm, "realm_can_delete_own_message_group", everyone.id);
|
2024-10-27 17:01:01 +01:00
|
|
|
override(realm, "realm_can_move_messages_between_topics_group", everyone.id);
|
2024-10-09 21:21:41 +02:00
|
|
|
override(current_user, "user_id", me.user_id);
|
2022-12-10 23:09:41 +01:00
|
|
|
// Get message with maximum permissions available
|
|
|
|
// Initialize message list
|
|
|
|
const list = init_message_list();
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current(list);
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
// Assume message has been previously edited.
|
|
|
|
// Message is sent by me, and is a stream. I should have all permissions to this message.
|
|
|
|
const messages = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
type: "stream",
|
|
|
|
sender_id: me.user_id,
|
|
|
|
is_hidden: false,
|
|
|
|
sent_by_me: true,
|
|
|
|
locally_echoed: false,
|
|
|
|
is_stream: true,
|
|
|
|
stream_id: 1,
|
|
|
|
unread: false,
|
|
|
|
collapsed: false,
|
|
|
|
not_spectator: true,
|
|
|
|
edit_history: [
|
|
|
|
{
|
|
|
|
prev_content: "Previous content",
|
|
|
|
prev_stream: 0,
|
|
|
|
prev_topic: "Previous topic",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
add_message_with_view(list, messages);
|
|
|
|
const response = popover_menus_data.get_actions_popover_content_context(1);
|
|
|
|
assert.equal(response.message_id, 1);
|
|
|
|
assert.equal(response.stream_id, 1);
|
|
|
|
assert.equal(response.editability_menu_item, "translated: Edit message");
|
|
|
|
assert.equal(response.move_message_menu_item, "translated: Move messages");
|
|
|
|
assert.equal(response.should_display_mark_as_unread, true);
|
|
|
|
assert.equal(response.view_source_menu_item, undefined);
|
|
|
|
assert.equal(response.should_display_collapse, true);
|
|
|
|
assert.equal(response.should_display_uncollapse, false);
|
|
|
|
assert.equal(response.should_display_add_reaction_option, true);
|
|
|
|
assert.equal(response.should_display_hide_option, false);
|
|
|
|
assert.equal(response.conversation_time_url, "conversation_and_time_url");
|
|
|
|
assert.equal(response.should_display_delete_option, true);
|
|
|
|
assert.equal(response.should_display_read_receipts_option, true);
|
|
|
|
assert.equal(response.should_display_quote_and_reply, true);
|
|
|
|
});
|
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
test("not_my_message_view_actions", ({override}) => {
|
|
|
|
set_page_params_no_edit_restrictions({override});
|
2022-12-10 23:09:41 +01:00
|
|
|
// Get message that is only viewable
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_can_delete_any_message_group", everyone.id);
|
2022-12-10 23:09:41 +01:00
|
|
|
const list = init_message_list();
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current(list);
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
// Message is sent by somebody else and is a stream with previous history.
|
|
|
|
// I should only be able to view this message with no edit/move permissions.
|
|
|
|
const messages = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
sender_id: mike.user_id,
|
|
|
|
is_hidden: false,
|
|
|
|
sent_by_me: false,
|
|
|
|
locally_echoed: false,
|
|
|
|
is_stream: true,
|
|
|
|
stream_id: 1,
|
|
|
|
collapsed: false,
|
|
|
|
unread: false,
|
|
|
|
edit_history: [
|
|
|
|
{
|
|
|
|
prev_content: "Previous content",
|
|
|
|
prev_stream: 0,
|
|
|
|
prev_topic: "Previous topic",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
add_message_with_view(list, messages);
|
|
|
|
|
|
|
|
const response = popover_menus_data.get_actions_popover_content_context(1);
|
|
|
|
|
2024-04-02 08:58:53 +02:00
|
|
|
assert.equal(response.view_source_menu_item, "translated: View original message");
|
2022-12-10 23:09:41 +01:00
|
|
|
assert.equal(response.editability_menu_item, undefined);
|
|
|
|
assert.equal(response.move_message_menu_item, undefined);
|
|
|
|
});
|
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
test("not_my_message_view_source_and_move", ({override}) => {
|
|
|
|
set_page_params_no_edit_restrictions({override});
|
|
|
|
override(realm, "realm_can_delete_any_message_group", everyone.id);
|
2024-10-27 17:01:01 +01:00
|
|
|
override(realm, "realm_can_move_messages_between_topics_group", everyone.id);
|
|
|
|
override(current_user, "user_id", me.user_id);
|
2022-12-10 23:09:41 +01:00
|
|
|
// Get message that is movable with viewable source
|
|
|
|
|
|
|
|
const list = init_message_list();
|
2024-02-13 03:44:04 +01:00
|
|
|
message_lists.set_current(list);
|
2022-12-10 23:09:41 +01:00
|
|
|
|
|
|
|
// Message tests edge case where message it sent by someone else.
|
|
|
|
// Message is movable, however--I should have only view permissions with the exception of moving the message.
|
|
|
|
const messages = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
sender_id: mike.user_id,
|
|
|
|
is_hidden: false,
|
|
|
|
sent_by_me: false,
|
|
|
|
locally_echoed: false,
|
|
|
|
is_stream: true,
|
|
|
|
stream_id: 1,
|
|
|
|
type: "stream",
|
|
|
|
unread: false,
|
|
|
|
collapsed: false,
|
2023-10-02 22:56:07 +02:00
|
|
|
topic: compose_state.empty_topic_placeholder(),
|
2022-12-10 23:09:41 +01:00
|
|
|
edit_history: [
|
|
|
|
{
|
|
|
|
prev_content: "Previous content",
|
|
|
|
prev_stream: 0,
|
|
|
|
prev_topic: "Previous topic",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
add_message_with_view(list, messages);
|
|
|
|
|
|
|
|
const response = popover_menus_data.get_actions_popover_content_context(1);
|
2024-04-02 08:58:53 +02:00
|
|
|
assert.equal(response.view_source_menu_item, "translated: View original message");
|
2022-12-10 23:09:41 +01:00
|
|
|
assert.equal(response.editability_menu_item, undefined);
|
|
|
|
assert.equal(response.move_message_menu_item, "translated: Move messages");
|
|
|
|
});
|