2014-01-31 17:03:52 +01:00
|
|
|
var message_flags = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
2017-12-16 13:33:54 +01:00
|
|
|
function send_flag_update(message, flag, op) {
|
|
|
|
channel.post({
|
|
|
|
url: '/json/messages/flags',
|
|
|
|
idempotent: true,
|
|
|
|
data: {
|
|
|
|
messages: JSON.stringify([message.id]),
|
|
|
|
flag: flag,
|
|
|
|
op: op,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-16 00:04:37 +01:00
|
|
|
exports.send_read = (function () {
|
2014-01-31 17:03:52 +01:00
|
|
|
var queue = [];
|
|
|
|
var on_success;
|
|
|
|
var start;
|
|
|
|
|
|
|
|
function server_request() {
|
|
|
|
// Wait for server IDs before sending flags
|
|
|
|
var real_msgs = _.filter(queue, function (msg) {
|
2017-07-17 16:52:57 +02:00
|
|
|
return !msg.locally_echoed;
|
2014-01-31 17:03:52 +01:00
|
|
|
});
|
|
|
|
var real_msg_ids = _.map(real_msgs, function (msg) {
|
|
|
|
return msg.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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({
|
2016-04-01 12:16:09 +02:00
|
|
|
url: '/json/messages/flags',
|
2014-01-31 17:03:52 +01:00
|
|
|
idempotent: true,
|
|
|
|
data: {messages: JSON.stringify(real_msg_ids),
|
2017-12-16 00:04:37 +01:00
|
|
|
op: 'add',
|
|
|
|
flag: 'read'},
|
2017-01-12 00:17:43 +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) {
|
2014-01-31 17:03:52 +01:00
|
|
|
if (data === undefined || data.messages === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue = _.filter(queue, function (message) {
|
|
|
|
return data.messages.indexOf(message.id) === -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
2017-12-16 00:04:37 +01:00
|
|
|
}());
|
2014-01-31 17:03:52 +01:00
|
|
|
|
2017-12-15 23:32:36 +01:00
|
|
|
exports.save_collapsed = function (message) {
|
|
|
|
send_flag_update(message, 'collapsed', true);
|
|
|
|
};
|
2014-01-31 17:03:52 +01:00
|
|
|
|
2017-12-15 23:32:36 +01:00
|
|
|
exports.save_uncollapsed = function (message) {
|
|
|
|
send_flag_update(message, 'collapsed', true);
|
2014-01-31 17:03:52 +01:00
|
|
|
};
|
|
|
|
|
2017-03-19 00:30:32 +01:00
|
|
|
exports.toggle_starred = 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;
|
|
|
|
|
2018-04-04 21:32:45 +02:00
|
|
|
unread_ops.notify_server_message_read(message);
|
2017-12-16 13:33:54 +01:00
|
|
|
ui.update_starred(message);
|
|
|
|
|
|
|
|
if (message.starred) {
|
|
|
|
send_flag_update(message, 'starred', 'add');
|
2017-03-19 00:30:32 +01:00
|
|
|
} else {
|
2017-12-16 13:33:54 +01:00
|
|
|
send_flag_update(message, 'starred', 'remove');
|
2017-03-19 00:30:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-31 17:03:52 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = message_flags;
|
|
|
|
}
|