2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
set_global("starred_messages", {
|
|
|
|
add: () => {},
|
|
|
|
remove: () => {},
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
rewiremock.enable();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("unread");
|
|
|
|
zrequire("unread_ops");
|
2021-02-10 04:53:22 +01:00
|
|
|
const message_flags = zrequire("message_flags");
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const ui = set_global("ui", {});
|
2021-02-28 00:39:51 +01:00
|
|
|
const channel = {__esModule: true};
|
|
|
|
|
|
|
|
rewiremock("../../static/js/channel").with(channel);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("starred", () => {
|
2018-07-03 01:30:43 +02:00
|
|
|
const message = {
|
|
|
|
id: 50,
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("current_msg_list", {
|
2019-07-10 02:03:41 +02:00
|
|
|
all_messages: () => [message],
|
|
|
|
is_search: () => false,
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
let ui_updated;
|
2018-07-03 01:30:43 +02:00
|
|
|
|
2018-08-01 19:02:38 +02:00
|
|
|
ui.update_starred_view = () => {
|
2018-07-03 01:30:43 +02:00
|
|
|
ui_updated = true;
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let posted_data;
|
2018-07-03 01:30:43 +02:00
|
|
|
|
|
|
|
channel.post = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts.url, "/json/messages/flags");
|
2018-07-03 01:30:43 +02:00
|
|
|
posted_data = opts.data;
|
|
|
|
};
|
|
|
|
|
2018-08-01 20:09:12 +02:00
|
|
|
message_flags.toggle_starred_and_update_server(message);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
|
|
|
assert(ui_updated);
|
|
|
|
|
|
|
|
assert.deepEqual(posted_data, {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[50]",
|
|
|
|
flag: "starred",
|
|
|
|
op: "add",
|
2018-07-03 01:30:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(message, {
|
|
|
|
id: 50,
|
|
|
|
starred: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
ui_updated = false;
|
|
|
|
|
2018-08-01 20:09:12 +02:00
|
|
|
message_flags.toggle_starred_and_update_server(message);
|
2018-07-03 01:30:43 +02:00
|
|
|
|
|
|
|
assert(ui_updated);
|
|
|
|
|
|
|
|
assert.deepEqual(posted_data, {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[50]",
|
|
|
|
flag: "starred",
|
|
|
|
op: "remove",
|
2018-07-03 01:30:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.deepEqual(message, {
|
|
|
|
id: 50,
|
|
|
|
starred: false,
|
|
|
|
});
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("read", () => {
|
2019-05-04 04:10:05 +02:00
|
|
|
// Way to capture posted info in every request
|
2019-11-02 00:06:25 +01:00
|
|
|
let channel_post_opts;
|
2019-05-04 04:10:05 +02:00
|
|
|
channel.post = (opts) => {
|
|
|
|
channel_post_opts = opts;
|
|
|
|
};
|
|
|
|
|
|
|
|
// For testing purpose limit the batch size value to 5 instead of 1000
|
|
|
|
message_flags._unread_batch_size = 5;
|
2019-11-02 00:06:25 +01:00
|
|
|
let msgs_to_flag_read = [
|
2020-07-16 22:40:18 +02:00
|
|
|
{locally_echoed: false, id: 1},
|
|
|
|
{locally_echoed: false, id: 2},
|
|
|
|
{locally_echoed: false, id: 3},
|
|
|
|
{locally_echoed: false, id: 4},
|
|
|
|
{locally_echoed: false, id: 5},
|
|
|
|
{locally_echoed: false, id: 6},
|
|
|
|
{locally_echoed: false, id: 7},
|
2019-05-04 04:10:05 +02:00
|
|
|
];
|
|
|
|
message_flags.send_read(msgs_to_flag_read);
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[1,2,3,4,5]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mock successful flagging of ids
|
2019-11-02 00:06:25 +01:00
|
|
|
let success_response_data = {
|
2019-05-04 04:10:05 +02:00
|
|
|
messages: [1, 2, 3, 4, 5],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[6,7]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
success_response_data = {
|
|
|
|
messages: [6, 7],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
|
|
|
|
// Don't flag locally echoed messages as read
|
2020-07-16 22:40:18 +02:00
|
|
|
const local_msg_1 = {locally_echoed: true, id: 1};
|
|
|
|
const local_msg_2 = {locally_echoed: true, id: 2};
|
2019-05-04 04:10:05 +02:00
|
|
|
msgs_to_flag_read = [
|
|
|
|
local_msg_1,
|
|
|
|
local_msg_2,
|
2020-07-16 22:40:18 +02:00
|
|
|
{locally_echoed: false, id: 3},
|
|
|
|
{locally_echoed: false, id: 4},
|
|
|
|
{locally_echoed: false, id: 5},
|
|
|
|
{locally_echoed: false, id: 6},
|
|
|
|
{locally_echoed: false, id: 7},
|
2019-05-04 04:10:05 +02:00
|
|
|
];
|
|
|
|
message_flags.send_read(msgs_to_flag_read);
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[3,4,5,6,7]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mark them non local
|
|
|
|
local_msg_1.locally_echoed = false;
|
|
|
|
local_msg_2.locally_echoed = false;
|
|
|
|
|
|
|
|
// Mock successful flagging of ids
|
|
|
|
success_response_data = {
|
|
|
|
messages: [3, 4, 5, 6, 7],
|
|
|
|
};
|
|
|
|
channel_post_opts.success(success_response_data);
|
|
|
|
|
|
|
|
// Former locally echoed messages flagging retried
|
|
|
|
assert.deepEqual(channel_post_opts, {
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-05-04 04:10:05 +02:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
2020-07-15 01:29:15 +02:00
|
|
|
messages: "[1,2]",
|
|
|
|
op: "add",
|
|
|
|
flag: "read",
|
2019-05-04 04:10:05 +02:00
|
|
|
},
|
|
|
|
success: channel_post_opts.success,
|
|
|
|
});
|
|
|
|
});
|
2021-02-28 00:39:51 +01:00
|
|
|
rewiremock.disable();
|