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;
|
2018-08-04 08:38:54 +02:00
|
|
|
exports.set_recenter_pointer_on_display = function (value) {
|
|
|
|
exports.recenter_pointer_on_display = value;
|
|
|
|
};
|
2016-04-12 17:38:47 +02:00
|
|
|
|
|
|
|
// Toggles re-centering the pointer in the window
|
2017-08-08 07:37:39 +02:00
|
|
|
// when All Messages is next clicked by the user
|
2016-04-12 17:38:47 +02:00
|
|
|
exports.suppress_scroll_pointer_update = false;
|
2018-08-04 08:39:30 +02:00
|
|
|
exports.set_suppress_scroll_pointer_update = function (value) {
|
|
|
|
exports.suppress_scroll_pointer_update = value;
|
|
|
|
};
|
2016-04-12 17:38:47 +02:00
|
|
|
exports.furthest_read = -1;
|
2018-08-04 08:37:23 +02:00
|
|
|
exports.set_furthest_read = function (value) {
|
|
|
|
exports.furthest_read = value;
|
|
|
|
};
|
2016-04-12 17:38:47 +02:00
|
|
|
exports.server_furthest_read = -1;
|
2018-08-04 08:41:12 +02:00
|
|
|
exports.set_server_furthest_read = function (value) {
|
|
|
|
exports.server_furthest_read = value;
|
|
|
|
};
|
2016-04-12 17:38:47 +02:00
|
|
|
|
|
|
|
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({
|
2018-12-18 19:34:45 +01:00
|
|
|
url: '/json/users/me/pointer',
|
2016-04-12 17:38:47 +02:00
|
|
|
idempotent: true,
|
2018-12-18 19:34:45 +01:00
|
|
|
data: {pointer: pointer.furthest_read},
|
2016-04-12 17:38:47 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-07-10 02:03:41 +02:00
|
|
|
if (event.msg_list.can_mark_messages_read()) {
|
|
|
|
unread_ops.notify_server_messages_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;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.pointer = pointer;
|