2017-11-16 19:51:44 +01:00
|
|
|
// Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html
|
2012-10-29 21:02:46 +01:00
|
|
|
var reload = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2015-11-29 03:00:00 +01:00
|
|
|
function preserve_state(send_after_reload, save_pointer, save_narrow, save_compose) {
|
2017-05-05 01:21:58 +02:00
|
|
|
if (!localstorage.supported()) {
|
|
|
|
// If local storage is not supported by the browser, we can't
|
|
|
|
// save the browser's position across reloads (since there's
|
|
|
|
// no secure way to pass that state in a signed fashion to the
|
|
|
|
// next instance of the browser client).
|
|
|
|
//
|
|
|
|
// So we jure return here and let the reload proceed without
|
|
|
|
// having preserved state. We keep the hash the same so we'll
|
|
|
|
// at least save their narrow state.
|
|
|
|
blueslip.log("Can't preserve state; no local storage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
if (send_after_reload === undefined) {
|
|
|
|
send_after_reload = 0;
|
|
|
|
}
|
|
|
|
var url = "#reload:send_after_reload=" + Number(send_after_reload);
|
2012-11-14 19:24:01 +01:00
|
|
|
url += "+csrf_token=" + encodeURIComponent(csrf_token);
|
2013-07-17 18:07:06 +02:00
|
|
|
|
2015-11-29 03:00:00 +01:00
|
|
|
if (save_compose) {
|
2017-04-24 20:35:26 +02:00
|
|
|
var msg_type = compose_state.get_message_type();
|
|
|
|
if (msg_type === 'stream') {
|
2015-11-29 03:00:00 +01:00
|
|
|
url += "+msg_type=stream";
|
2017-04-15 01:15:59 +02:00
|
|
|
url += "+stream=" + encodeURIComponent(compose_state.stream_name());
|
2018-12-16 18:02:16 +01:00
|
|
|
url += "+topic=" + encodeURIComponent(compose_state.topic());
|
2017-04-24 20:35:26 +02:00
|
|
|
} else if (msg_type === 'private') {
|
2015-11-29 03:00:00 +01:00
|
|
|
url += "+msg_type=private";
|
2017-03-18 18:48:43 +01:00
|
|
|
url += "+recipient=" + encodeURIComponent(compose_state.recipient());
|
2015-11-29 03:00:00 +01:00
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
|
2017-04-24 20:35:26 +02:00
|
|
|
if (msg_type) {
|
2017-04-15 01:15:59 +02:00
|
|
|
url += "+msg=" + encodeURIComponent(compose_state.message_content());
|
2015-11-29 03:00:00 +01:00
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 03:00:00 +01:00
|
|
|
if (save_pointer) {
|
|
|
|
var pointer = home_msg_list.selected_id();
|
|
|
|
if (pointer !== -1) {
|
|
|
|
url += "+pointer=" + pointer;
|
2014-02-12 20:03:05 +01:00
|
|
|
}
|
2015-11-29 03:00:00 +01:00
|
|
|
}
|
2014-02-12 20:03:05 +01:00
|
|
|
|
2015-11-29 03:00:00 +01:00
|
|
|
if (save_narrow) {
|
|
|
|
var row = home_msg_list.selected_row();
|
2017-04-25 15:25:31 +02:00
|
|
|
if (!narrow_state.active()) {
|
2015-11-29 03:00:00 +01:00
|
|
|
if (row.length > 0) {
|
|
|
|
url += "+offset=" + row.offset().top;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
url += "+offset=" + home_msg_list.pre_narrow_offset;
|
|
|
|
|
2016-04-25 23:45:25 +02:00
|
|
|
var narrow_pointer = message_list.narrowed.selected_id();
|
2015-11-29 03:00:00 +01:00
|
|
|
if (narrow_pointer !== -1) {
|
|
|
|
url += "+narrow_pointer=" + narrow_pointer;
|
|
|
|
}
|
2016-04-25 23:45:25 +02:00
|
|
|
var narrow_row = message_list.narrowed.selected_row();
|
2015-11-29 03:00:00 +01:00
|
|
|
if (narrow_row.length > 0) {
|
|
|
|
url += "+narrow_offset=" + narrow_row.offset().top;
|
|
|
|
}
|
2014-02-12 20:03:05 +01:00
|
|
|
}
|
2014-02-04 22:02:26 +01:00
|
|
|
}
|
2013-06-18 23:04:39 +02:00
|
|
|
|
|
|
|
var oldhash = window.location.hash;
|
|
|
|
if (oldhash.length !== 0 && oldhash[0] === '#') {
|
|
|
|
oldhash = oldhash.slice(1);
|
|
|
|
}
|
|
|
|
url += "+oldhash=" + encodeURIComponent(oldhash);
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2017-04-26 12:37:28 +02:00
|
|
|
var ls = localstorage();
|
|
|
|
// Delete all the previous preserved states.
|
|
|
|
ls.removeRegex('reload:\\d+');
|
|
|
|
|
2017-03-23 06:21:13 +01:00
|
|
|
// To protect the browser against CSRF type attacks, the reload
|
|
|
|
// logic uses a random token (to distinct this browser from
|
|
|
|
// others) which is passed via the URL to the browser (post
|
|
|
|
// reloading). The token is a key into local storage, where we
|
|
|
|
// marshall and store the URL.
|
|
|
|
//
|
|
|
|
// TODO: Remove the now-unnecessary URL-encoding logic above and
|
|
|
|
// just pass the actual data structures through local storage.
|
2018-06-04 21:13:07 +02:00
|
|
|
var token = util.random_int(0, 1024 * 1024 * 1024 * 1024);
|
2017-03-23 06:21:13 +01:00
|
|
|
|
|
|
|
ls.set("reload:" + token, url);
|
|
|
|
window.location.replace("#reload:" + token);
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
|
|
|
|
2014-03-03 23:32:17 +01:00
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
// Check if we're doing a compose-preserving reload. This must be
|
2014-01-30 20:29:00 +01:00
|
|
|
// done before the first call to get_events
|
2018-03-07 14:01:37 +01:00
|
|
|
exports.initialize = function () {
|
2012-10-29 21:02:46 +01:00
|
|
|
var location = window.location.toString();
|
2017-03-23 06:21:13 +01:00
|
|
|
var hash_fragment = location.substring(location.indexOf('#') + 1);
|
|
|
|
|
|
|
|
// hash_fragment should be e.g. `reload:12345123412312`
|
|
|
|
if (hash_fragment.search("reload:") !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using the token, recover the saved pre-reload data from local
|
|
|
|
// storage. Afterwards, we clear the reload entry from local
|
|
|
|
// storage to avoid a local storage space leak.
|
|
|
|
var ls = localstorage();
|
|
|
|
var fragment = ls.get(hash_fragment);
|
|
|
|
if (fragment === undefined) {
|
2017-05-05 01:16:02 +02:00
|
|
|
// Since this can happen sometimes with hand-reloading, it's
|
|
|
|
// not really worth throwing an exception if these don't
|
|
|
|
// exist, but be log it so that it's available for future
|
|
|
|
// debugging if an exception happens later.
|
|
|
|
blueslip.info("Invalid hash change reload token");
|
|
|
|
hashchange.changehash("");
|
2012-10-29 21:02:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-03-23 06:21:13 +01:00
|
|
|
ls.remove(hash_fragment);
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
fragment = fragment.replace(/^reload:/, "");
|
|
|
|
var keyvals = fragment.split("+");
|
|
|
|
var vars = {};
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(keyvals, function (str) {
|
2012-10-29 21:02:46 +01:00
|
|
|
var pair = str.split("=");
|
|
|
|
vars[pair[0]] = decodeURIComponent(pair[1]);
|
|
|
|
});
|
|
|
|
|
2015-11-10 19:01:34 +01:00
|
|
|
if (vars.msg !== undefined) {
|
2013-07-17 18:07:06 +02:00
|
|
|
var send_now = parseInt(vars.send_after_reload, 10);
|
|
|
|
|
2018-03-07 14:16:34 +01:00
|
|
|
try {
|
|
|
|
// TODO: preserve focus
|
2018-12-16 18:02:16 +01:00
|
|
|
var topic = util.get_reload_topic(vars);
|
|
|
|
|
2018-03-07 14:16:34 +01:00
|
|
|
compose_actions.start(vars.msg_type, {stream: vars.stream || '',
|
2018-11-15 19:14:16 +01:00
|
|
|
topic: topic || '',
|
2018-05-06 21:43:17 +02:00
|
|
|
private_message_recipient: vars.recipient || '',
|
|
|
|
content: vars.msg || ''});
|
2018-03-07 14:16:34 +01:00
|
|
|
if (send_now) {
|
|
|
|
compose.finish();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// We log an error if we can't open the compose box, but otherwise
|
|
|
|
// we continue, since this is not critical.
|
|
|
|
blueslip.warn(err.toString());
|
2013-07-17 18:07:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
var pointer = parseInt(vars.pointer, 10);
|
2013-06-18 23:04:39 +02:00
|
|
|
|
2013-07-17 18:07:06 +02:00
|
|
|
if (pointer) {
|
2017-04-24 21:33:48 +02:00
|
|
|
page_params.orig_initial_pointer = page_params.pointer;
|
|
|
|
page_params.pointer = pointer;
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
2014-02-04 22:02:26 +01:00
|
|
|
var offset = parseInt(vars.offset, 10);
|
|
|
|
if (offset) {
|
|
|
|
page_params.initial_offset = offset;
|
|
|
|
}
|
2013-07-17 18:07:06 +02:00
|
|
|
|
2014-02-12 20:03:05 +01:00
|
|
|
var narrow_pointer = parseInt(vars.narrow_pointer, 10);
|
|
|
|
if (narrow_pointer) {
|
|
|
|
page_params.initial_narrow_pointer = narrow_pointer;
|
|
|
|
}
|
|
|
|
var narrow_offset = parseInt(vars.narrow_offset, 10);
|
|
|
|
if (narrow_offset) {
|
|
|
|
page_params.initial_narrow_offset = narrow_offset;
|
|
|
|
}
|
|
|
|
|
2013-09-06 21:52:12 +02:00
|
|
|
activity.new_user_input = false;
|
2013-07-17 18:07:06 +02:00
|
|
|
hashchange.changehash(vars.oldhash);
|
2014-03-03 23:32:17 +01:00
|
|
|
};
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2015-11-29 03:00:00 +01:00
|
|
|
function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compose, message) {
|
2018-08-04 15:40:25 +02:00
|
|
|
if (reload_state.is_in_progress()) {
|
2017-10-12 05:31:32 +02:00
|
|
|
blueslip.log("do_reload_app: Doing nothing since reload_in_progress");
|
|
|
|
return;
|
|
|
|
}
|
2014-02-19 18:41:01 +01:00
|
|
|
|
2014-02-10 21:26:25 +01:00
|
|
|
// TODO: we should completely disable the UI here
|
2015-11-29 03:00:00 +01:00
|
|
|
if (save_pointer || save_narrow || save_compose) {
|
2017-03-27 22:32:10 +02:00
|
|
|
try {
|
|
|
|
preserve_state(send_after_reload, save_pointer, save_narrow, save_compose);
|
|
|
|
} catch (ex) {
|
|
|
|
blueslip.error('Failed to preserve state',
|
|
|
|
undefined, ex.stack);
|
|
|
|
}
|
2014-02-10 21:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message === undefined) {
|
2017-05-07 00:04:48 +02:00
|
|
|
message = "Reloading ...";
|
2014-02-10 21:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We need a better API for showing messages.
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.message(message, $("#reloading-application"));
|
2014-02-10 21:26:25 +01:00
|
|
|
blueslip.log('Starting server requested page reload');
|
2018-08-04 15:40:25 +02:00
|
|
|
reload_state.set_state_to_in_progress();
|
2014-02-10 21:26:25 +01:00
|
|
|
|
2017-10-04 19:32:43 +02:00
|
|
|
// Sometimes the window.location.reload that we attempt has no
|
|
|
|
// immediate effect (likely by browsers trying to save power by
|
|
|
|
// skipping requested reloads), which can leave the Zulip app in a
|
|
|
|
// broken state and cause lots of confusing tracebacks. So, we
|
|
|
|
// set ourselves to try reloading a bit later, both periodically
|
|
|
|
// and when the user focuses the window.
|
|
|
|
$(window).on('focus', function () {
|
|
|
|
blueslip.log("Retrying on-focus page reload");
|
|
|
|
window.location.reload(true);
|
|
|
|
});
|
|
|
|
setInterval(function () {
|
|
|
|
blueslip.log("Retrying page reload due to 30s timer");
|
|
|
|
window.location.reload(true);
|
|
|
|
}, 30000);
|
|
|
|
|
2017-03-27 22:18:55 +02:00
|
|
|
try {
|
|
|
|
server_events.cleanup_event_queue();
|
|
|
|
} catch (ex) {
|
|
|
|
blueslip.error('Failed to cleanup before reloading',
|
|
|
|
undefined, ex.stack);
|
2014-02-10 21:26:25 +01:00
|
|
|
}
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
window.location.reload(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.initiate = function (options) {
|
2013-07-30 05:11:50 +02:00
|
|
|
options = _.defaults({}, options, {
|
|
|
|
immediate: false,
|
2015-11-29 03:00:00 +01:00
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
send_after_reload: false,
|
2013-07-30 05:11:50 +02:00
|
|
|
});
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2015-11-30 17:47:19 +01:00
|
|
|
if (options.save_pointer === undefined ||
|
|
|
|
options.save_narrow === undefined ||
|
|
|
|
options.save_compose === undefined) {
|
|
|
|
blueslip.error("reload.initiate() called without explicit save options.");
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
if (options.immediate) {
|
2015-11-29 03:00:00 +01:00
|
|
|
do_reload_app(options.send_after_reload,
|
|
|
|
options.save_pointer,
|
|
|
|
options.save_narrow,
|
|
|
|
options.save_compose,
|
|
|
|
options.message);
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
|
|
|
|
2018-08-04 15:40:25 +02:00
|
|
|
if (reload_state.is_pending()) {
|
2012-10-29 21:02:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-08-04 15:40:25 +02:00
|
|
|
reload_state.set_state_to_pending();
|
2012-10-29 21:02:46 +01:00
|
|
|
|
|
|
|
// If the user is composing a message, reload if they become idle
|
|
|
|
// while composing. If they finish or cancel the compose, wait
|
|
|
|
// until they're idle again
|
|
|
|
var idle_control;
|
2018-06-04 21:13:07 +02:00
|
|
|
var unconditional_timeout = 1000 * 60 * 30 + util.random_int(0, 1000 * 60 * 5);
|
|
|
|
var composing_timeout = 1000 * 60 * 5 + util.random_int(0, 1000 * 60);
|
|
|
|
var home_timeout = 1000 * 60 + util.random_int(0, 1000 * 60);
|
2016-12-02 17:09:31 +01:00
|
|
|
var compose_done_handler;
|
|
|
|
var compose_started_handler;
|
2012-10-29 21:02:46 +01:00
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function reload_from_idle() {
|
2015-11-29 03:00:00 +01:00
|
|
|
do_reload_app(false,
|
|
|
|
options.save_pointer,
|
|
|
|
options.save_narrow,
|
|
|
|
options.save_compose,
|
|
|
|
options.message);
|
2013-06-18 21:03:20 +02:00
|
|
|
}
|
2012-11-30 17:35:19 +01:00
|
|
|
|
2013-07-22 23:27:32 +02:00
|
|
|
// Make sure we always do a reload eventually
|
|
|
|
setTimeout(reload_from_idle, unconditional_timeout);
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler = function () {
|
|
|
|
idle_control.cancel();
|
2016-12-03 03:08:47 +01:00
|
|
|
idle_control = $(document).idle({idle: home_timeout,
|
|
|
|
onIdle: reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).off('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_started.zulip', compose_started_handler);
|
2012-10-29 21:02:46 +01:00
|
|
|
};
|
|
|
|
compose_started_handler = function () {
|
|
|
|
idle_control.cancel();
|
2016-12-03 03:08:47 +01:00
|
|
|
idle_control = $(document).idle({idle: composing_timeout,
|
|
|
|
onIdle: reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).off('compose_started.zulip', compose_started_handler);
|
|
|
|
$(document).on('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
|
|
|
};
|
|
|
|
|
2017-03-18 18:48:43 +01:00
|
|
|
if (compose_state.composing()) {
|
2016-12-03 03:08:47 +01:00
|
|
|
idle_control = $(document).idle({idle: composing_timeout,
|
|
|
|
onIdle: reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_canceled.zulip compose_finished.zulip',
|
2012-10-29 21:02:46 +01:00
|
|
|
compose_done_handler);
|
|
|
|
} else {
|
2016-12-03 03:08:47 +01:00
|
|
|
idle_control = $(document).idle({idle: home_timeout,
|
|
|
|
onIdle: reload_from_idle});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('compose_started.zulip', compose_started_handler);
|
2012-10-29 21:02:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-19 18:41:01 +01:00
|
|
|
window.addEventListener('beforeunload', function () {
|
|
|
|
// When navigating away from the page do not try to reload.
|
|
|
|
// The polling get_events call will fail after we delete the event queue.
|
|
|
|
// When that happens we reload the page to correct the problem. If this
|
|
|
|
// happens before the navigation is complete the user is kept captive at
|
|
|
|
// zulip.
|
2017-10-12 05:31:32 +02:00
|
|
|
blueslip.log("Setting reload_in_progress in beforeunload handler");
|
2018-08-04 15:40:25 +02:00
|
|
|
reload_state.set_state_to_in_progress();
|
2014-02-19 18:41:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-10-29 21:02:46 +01:00
|
|
|
return exports;
|
2012-11-01 19:38:01 +01:00
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = reload;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.reload = reload;
|