2017-03-18 01:41:56 +01:00
|
|
|
var unread_ops = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_all_as_read = function (cont) {
|
2017-03-18 01:41:56 +01:00
|
|
|
unread.declare_bankruptcy();
|
|
|
|
unread_ui.update_unread_counts();
|
|
|
|
|
|
|
|
channel.post({
|
2017-08-04 20:26:38 +02:00
|
|
|
url: '/json/mark_all_as_read',
|
2017-03-18 01:41:56 +01:00
|
|
|
idempotent: true,
|
|
|
|
success: cont});
|
|
|
|
};
|
|
|
|
|
2017-08-03 00:14:36 +02:00
|
|
|
function process_newly_read_message(message, options) {
|
|
|
|
home_msg_list.show_message_as_read(message, options);
|
|
|
|
message_list.all.show_message_as_read(message, options);
|
|
|
|
if (message_list.narrowed) {
|
|
|
|
message_list.narrowed.show_message_as_read(message, options);
|
|
|
|
}
|
|
|
|
notifications.close_notification(message);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:28:16 +02:00
|
|
|
exports.process_read_messages_event = function (message_ids) {
|
|
|
|
/*
|
|
|
|
This code has a lot in common with mark_messages_as_read,
|
|
|
|
but there are subtle differences due to the fact that the
|
|
|
|
server can tell us about unread messages that we didn't
|
|
|
|
actually read locally (and which we may not have even
|
|
|
|
loaded locally).
|
|
|
|
*/
|
|
|
|
var options = {from: 'server'};
|
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
message_ids = unread.get_unread_message_ids(message_ids);
|
|
|
|
if (message_ids.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-03 00:28:16 +02:00
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
_.each(message_ids, function (message_id) {
|
2017-08-03 00:28:16 +02:00
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
// I'm not sure this entirely makes sense for all server
|
|
|
|
// notifications.
|
|
|
|
unread.messages_read_in_narrow = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unread.mark_as_read(message_id);
|
|
|
|
|
|
|
|
var message = message_store.get(message_id);
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
process_newly_read_message(message, options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2017-08-03 00:28:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-18 01:41:56 +01:00
|
|
|
// Takes a list of messages and marks them as read
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_messages_as_read = function (messages, options) {
|
2017-03-18 01:41:56 +01:00
|
|
|
options = options || {};
|
2017-12-16 16:53:27 +01:00
|
|
|
|
|
|
|
messages = unread.get_unread_messages(messages);
|
|
|
|
if (messages.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
|
2017-12-21 17:51:52 +01:00
|
|
|
message_flags.send_read(messages);
|
|
|
|
|
2017-03-18 01:41:56 +01:00
|
|
|
_.each(messages, function (message) {
|
|
|
|
if (current_msg_list === message_list.narrowed) {
|
|
|
|
unread.messages_read_in_narrow = true;
|
|
|
|
}
|
|
|
|
|
2017-08-02 21:40:01 +02:00
|
|
|
unread.mark_as_read(message.id);
|
2017-08-03 00:14:36 +02:00
|
|
|
process_newly_read_message(message, options);
|
2017-03-18 01:41:56 +01:00
|
|
|
});
|
|
|
|
|
2017-12-16 16:53:27 +01:00
|
|
|
unread_ui.update_unread_counts();
|
2017-03-18 01:41:56 +01:00
|
|
|
};
|
|
|
|
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_message_as_read = function (message, options) {
|
2017-03-18 01:41:56 +01:00
|
|
|
exports.mark_messages_as_read([message], options);
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we ever materially change the algorithm for this function, we
|
|
|
|
// may need to update notifications.received_messages as well.
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.process_visible = function () {
|
2017-03-18 01:41:56 +01:00
|
|
|
if (! notifications.window_has_focus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (feature_flags.mark_read_at_bottom) {
|
|
|
|
if (message_viewport.bottom_message_visible()) {
|
|
|
|
exports.mark_current_list_as_read();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exports.mark_messages_as_read(message_viewport.visible_messages(true));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_current_list_as_read = function (options) {
|
2017-03-18 01:41:56 +01:00
|
|
|
exports.mark_messages_as_read(current_msg_list.all_messages(), options);
|
|
|
|
};
|
|
|
|
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_stream_as_read = function (stream_id, cont) {
|
2017-03-18 01:41:56 +01:00
|
|
|
channel.post({
|
2017-08-08 16:11:45 +02:00
|
|
|
url: '/json/mark_stream_as_read',
|
2017-03-18 01:41:56 +01:00
|
|
|
idempotent: true,
|
2017-08-15 19:34:15 +02:00
|
|
|
data: {stream_id: stream_id},
|
2017-03-18 01:41:56 +01:00
|
|
|
success: cont});
|
|
|
|
};
|
|
|
|
|
2017-12-16 22:02:58 +01:00
|
|
|
exports.mark_topic_as_read = function (stream_id, topic, cont) {
|
2017-03-18 01:41:56 +01:00
|
|
|
channel.post({
|
2017-08-08 16:11:45 +02:00
|
|
|
url: '/json/mark_topic_as_read',
|
2017-03-18 01:41:56 +01:00
|
|
|
idempotent: true,
|
2017-08-15 19:28:32 +02:00
|
|
|
data: {stream_id: stream_id, topic_name: topic},
|
2017-03-18 01:41:56 +01:00
|
|
|
success: cont});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = unread_ops;
|
|
|
|
}
|