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({
|
2018-12-18 19:34:45 +01:00
|
|
|
url: '/json/messages/flags',
|
2014-01-31 17:03:52 +01:00
|
|
|
idempotent: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
data: {messages: JSON.stringify(real_msg_ids),
|
|
|
|
op: 'add',
|
|
|
|
flag: 'read'},
|
|
|
|
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) {
|
2018-07-03 01:41:31 +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) {
|
2018-07-03 01:41:31 +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) {
|
|
|
|
var 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;
|
|
|
|
|
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) {
|
|
|
|
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 {
|
2017-12-16 13:33:54 +01: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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.message_flags = message_flags;
|