2017-11-16 19:54:24 +01:00
|
|
|
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html for notes on
|
2016-08-17 01:19:14 +02:00
|
|
|
// how this system is designed.
|
|
|
|
|
2016-04-12 17:38:47 +02:00
|
|
|
var pointer = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.recenter_pointer_on_display = false;
|
|
|
|
|
|
|
|
// Toggles re-centering the pointer in the window
|
|
|
|
// when Home is next clicked by the user
|
|
|
|
exports.suppress_scroll_pointer_update = false;
|
|
|
|
exports.furthest_read = -1;
|
|
|
|
exports.server_furthest_read = -1;
|
|
|
|
|
|
|
|
var pointer_update_in_flight = false;
|
|
|
|
|
|
|
|
function update_pointer() {
|
|
|
|
if (!pointer_update_in_flight) {
|
|
|
|
pointer_update_in_flight = true;
|
2016-12-31 08:54:00 +01:00
|
|
|
return channel.post({
|
2016-04-12 17:38:47 +02:00
|
|
|
url: '/json/users/me/pointer',
|
|
|
|
idempotent: true,
|
|
|
|
data: {pointer: pointer.furthest_read},
|
|
|
|
success: function () {
|
|
|
|
pointer.server_furthest_read = pointer.furthest_read;
|
|
|
|
pointer_update_in_flight = false;
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
pointer_update_in_flight = false;
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-04-12 17:38:47 +02:00
|
|
|
});
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
// Return an empty, resolved Deferred.
|
|
|
|
return $.when();
|
2016-04-12 17:38:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exports.send_pointer_update = function () {
|
|
|
|
// Only bother if you've read new messages.
|
|
|
|
if (pointer.furthest_read > pointer.server_furthest_read) {
|
|
|
|
update_pointer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function unconditionally_send_pointer_update() {
|
|
|
|
if (pointer_update_in_flight) {
|
|
|
|
// Keep trying.
|
|
|
|
var deferred = $.Deferred();
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
deferred.resolve(unconditionally_send_pointer_update());
|
|
|
|
}, 100);
|
|
|
|
return deferred;
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return update_pointer();
|
2016-04-12 17:38:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.fast_forward_pointer = function () {
|
|
|
|
channel.get({
|
2016-05-11 01:30:31 +02:00
|
|
|
url: '/json/users/me',
|
2016-04-12 17:38:47 +02:00
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
2017-03-18 01:41:56 +01:00
|
|
|
unread_ops.mark_all_as_read(function () {
|
2016-04-12 17:38:47 +02:00
|
|
|
pointer.furthest_read = data.max_message_id;
|
|
|
|
unconditionally_send_pointer_update().then(function () {
|
|
|
|
reload.initiate({immediate: true,
|
|
|
|
save_pointer: false,
|
2017-03-23 05:19:35 +01:00
|
|
|
save_narrow: true,
|
2016-04-12 17:38:47 +02:00
|
|
|
save_compose: true});
|
|
|
|
});
|
|
|
|
});
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-04-12 17:38:47 +02:00
|
|
|
});
|
|
|
|
};
|
2016-04-16 14:22:03 +02:00
|
|
|
|
2016-11-05 18:52:07 +01:00
|
|
|
exports.initialize = function initialize() {
|
2017-04-24 21:33:48 +02:00
|
|
|
pointer.server_furthest_read = page_params.pointer;
|
2016-11-05 18:52:07 +01:00
|
|
|
if (page_params.orig_initial_pointer !== undefined &&
|
|
|
|
page_params.orig_initial_pointer > pointer.server_furthest_read) {
|
|
|
|
pointer.server_furthest_read = page_params.orig_initial_pointer;
|
|
|
|
}
|
|
|
|
pointer.furthest_read = pointer.server_furthest_read;
|
|
|
|
|
|
|
|
// We only send pointer updates when the user has been idle for a
|
|
|
|
// short while to avoid hammering the server
|
|
|
|
$(document).idle({idle: 1000,
|
|
|
|
onIdle: pointer.send_pointer_update,
|
|
|
|
keepTracking: true});
|
|
|
|
|
|
|
|
$(document).on('message_selected.zulip', function (event) {
|
|
|
|
// Only advance the pointer when not narrowed
|
|
|
|
if (event.id === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Additionally, don't advance the pointer server-side
|
|
|
|
// if the selected message is local-only
|
|
|
|
if (event.msg_list === home_msg_list && page_params.narrow_stream === undefined) {
|
2017-07-17 16:52:57 +02:00
|
|
|
if (event.id > pointer.furthest_read) {
|
|
|
|
var msg = home_msg_list.get(event.id);
|
|
|
|
if (!msg.locally_echoed) {
|
|
|
|
pointer.furthest_read = event.id;
|
|
|
|
}
|
2016-11-05 18:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.mark_read && event.previously_selected !== -1) {
|
|
|
|
// Mark messages between old pointer and new pointer as read
|
|
|
|
var messages;
|
|
|
|
if (event.id < event.previously_selected) {
|
|
|
|
messages = event.msg_list.message_range(event.id, event.previously_selected);
|
|
|
|
} else {
|
|
|
|
messages = event.msg_list.message_range(event.previously_selected, event.id);
|
|
|
|
}
|
2017-03-18 01:41:56 +01:00
|
|
|
unread_ops.mark_messages_as_read(messages, {from: 'pointer'});
|
2016-11-05 18:52:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-12 17:38:47 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = pointer;
|
|
|
|
}
|