2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
const channel = require("./channel");
|
|
|
|
|
2017-12-16 13:33:54 +01:00
|
|
|
function send_flag_update(message, flag, op) {
|
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2017-12-16 13:33:54 +01:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
|
|
|
messages: JSON.stringify([message.id]),
|
2020-07-20 22:18:43 +02:00
|
|
|
flag,
|
|
|
|
op,
|
2017-12-16 13:33:54 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2019-05-04 04:10:05 +02:00
|
|
|
exports._unread_batch_size = 1000;
|
2017-12-16 00:04:37 +01:00
|
|
|
exports.send_read = (function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
let queue = [];
|
|
|
|
let on_success;
|
|
|
|
let start;
|
2014-01-31 17:03:52 +01:00
|
|
|
function server_request() {
|
|
|
|
// Wait for server IDs before sending flags
|
2020-07-02 01:39:34 +02:00
|
|
|
const real_msgs = queue.filter((msg) => !msg.locally_echoed);
|
|
|
|
const real_msg_ids = real_msgs.map((msg) => msg.id);
|
2014-01-31 17:03:52 +01:00
|
|
|
|
|
|
|
if (real_msg_ids.length === 0) {
|
2014-03-07 23:03:46 +01:00
|
|
|
setTimeout(start, 100);
|
2014-01-31 17:03:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const real_msg_ids_batch = real_msg_ids.slice(0, exports._unread_batch_size);
|
2019-05-04 04:10:05 +02:00
|
|
|
|
2014-03-07 23:04:01 +01:00
|
|
|
// We have some real IDs. If there are any left in the queue when this
|
|
|
|
// call finishes, they will be handled in the success callback.
|
|
|
|
|
2014-01-31 17:03:52 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2014-01-31 17:03:52 +01:00
|
|
|
idempotent: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
data: {messages: JSON.stringify(real_msg_ids_batch), op: "add", flag: "read"},
|
2018-12-18 19:34:45 +01:00
|
|
|
success: on_success,
|
2014-01-31 17:03:52 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:51:52 +01:00
|
|
|
start = _.throttle(server_request, 1000);
|
2014-01-31 17:03:52 +01:00
|
|
|
|
2016-12-02 14:06:06 +01:00
|
|
|
on_success = function on_success(data) {
|
2020-07-16 23:29:01 +02:00
|
|
|
if (data === undefined || data.messages === undefined) {
|
2014-01-31 17:03:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
queue = queue.filter((message) => !data.messages.includes(message.id));
|
2014-01-31 17:03:52 +01:00
|
|
|
|
|
|
|
if (queue.length > 0) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-21 17:51:52 +01:00
|
|
|
function add(messages) {
|
|
|
|
queue = queue.concat(messages);
|
2014-01-31 17:03:52 +01:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return add;
|
2020-07-16 22:35:58 +02:00
|
|
|
})();
|
2014-01-31 17:03:52 +01:00
|
|
|
|
2017-12-15 23:32:36 +01:00
|
|
|
exports.save_collapsed = function (message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
send_flag_update(message, "collapsed", "add");
|
2017-12-15 23:32:36 +01:00
|
|
|
};
|
2014-01-31 17:03:52 +01:00
|
|
|
|
2017-12-15 23:32:36 +01:00
|
|
|
exports.save_uncollapsed = function (message) {
|
2020-07-15 01:29:15 +02:00
|
|
|
send_flag_update(message, "collapsed", "remove");
|
2014-01-31 17:03:52 +01:00
|
|
|
};
|
|
|
|
|
2018-08-01 20:11:32 +02:00
|
|
|
// This updates the state of the starred flag in local data
|
|
|
|
// structures, and triggers a UI rerender.
|
2018-08-01 02:29:10 +02:00
|
|
|
exports.update_starred_flag = function (message_id, new_value) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = message_store.get(message_id);
|
2018-10-12 22:51:54 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
// If we don't have the message locally, do nothing; if later
|
|
|
|
// we fetch it, it'll come with the correct `starred` state.
|
|
|
|
return;
|
|
|
|
}
|
2018-08-01 02:29:10 +02:00
|
|
|
message.starred = new_value;
|
2018-08-01 19:02:38 +02:00
|
|
|
ui.update_starred_view(message_id, new_value);
|
2018-08-01 02:29:10 +02:00
|
|
|
};
|
|
|
|
|
2018-08-01 20:09:12 +02:00
|
|
|
exports.toggle_starred_and_update_server = function (message) {
|
2017-12-16 13:33:54 +01:00
|
|
|
if (message.locally_echoed) {
|
|
|
|
// This is defensive code for when you hit the "*" key
|
|
|
|
// before we get a server ack. It's rare that somebody
|
|
|
|
// can star this quickly, and we don't have a good way
|
|
|
|
// to tell the server which message was starred.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.starred = !message.starred;
|
|
|
|
|
2019-07-10 02:03:41 +02:00
|
|
|
// Unlike most calls to mark messages as read, we don't check
|
|
|
|
// msg_list.can_mark_messages_read, because starring a message is an
|
|
|
|
// explicit interaction and we'd like to preserve the user
|
|
|
|
// expectation invariant that all starred messages are read.
|
2018-04-04 21:32:45 +02:00
|
|
|
unread_ops.notify_server_message_read(message);
|
2018-08-01 19:02:38 +02:00
|
|
|
ui.update_starred_view(message.id, message.starred);
|
2017-12-16 13:33:54 +01:00
|
|
|
|
|
|
|
if (message.starred) {
|
2020-07-15 01:29:15 +02:00
|
|
|
send_flag_update(message, "starred", "add");
|
2018-08-17 03:46:32 +02:00
|
|
|
starred_messages.add([message.id]);
|
2017-03-19 00:30:32 +01:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
send_flag_update(message, "starred", "remove");
|
2018-08-17 03:46:32 +02:00
|
|
|
starred_messages.remove([message.id]);
|
2017-03-19 00:30:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-26 21:23:32 +01:00
|
|
|
exports.unstar_all_messages = function () {
|
|
|
|
const starred_msg_ids = starred_messages.get_starred_msg_ids();
|
2019-02-22 12:28:29 +01:00
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/flags",
|
2019-02-22 12:28:29 +01:00
|
|
|
idempotent: true,
|
|
|
|
data: {
|
|
|
|
messages: JSON.stringify(starred_msg_ids),
|
2020-07-15 01:29:15 +02:00
|
|
|
flag: "starred",
|
|
|
|
op: "remove",
|
2019-02-22 12:28:29 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.message_flags = exports;
|