zulip/web/src/reload_setup.js

92 lines
3.2 KiB
JavaScript
Raw Normal View History

import * as activity from "./activity";
import * as blueslip from "./blueslip";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import {localstorage} from "./localstorage";
import * as narrow from "./narrow";
import {page_params} from "./page_params";
// Check if we're doing a compose-preserving reload. This must be
// done before the first call to get_events
export function initialize() {
// location.hash should be e.g. `#reload:12345123412312`
if (!location.hash.startsWith("#reload:")) {
return;
}
const hash_fragment = location.hash.slice("#".length);
// 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.
const ls = localstorage();
let fragment = ls.get(hash_fragment);
if (fragment === undefined) {
// 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");
narrow.changehash("");
return;
}
ls.remove(hash_fragment);
// TODO/compatibility: `fragment` was changed from a string
// to a map containing the string and a timestamp. For now we'll
// delete all tokens that only contain the url. Remove the
// `|| fragment` once you can no longer directly upgrade
// from Zulip 5.x to the current version.
[, fragment] = /^#reload:(.*)/.exec(fragment.url || fragment);
const keyvals = fragment.split("+");
const vars = {};
for (const str of keyvals) {
const pair = str.split("=");
vars[pair[0]] = decodeURIComponent(pair[1]);
}
if (vars.msg !== undefined) {
const send_now = Number.parseInt(vars.send_after_reload, 10);
try {
compose_actions.start(vars.msg_type, {
stream: vars.stream || "",
topic: vars.topic || "",
private_message_recipient: vars.recipient || "",
content: vars.msg || "",
draft_id: vars.draft_id || "",
});
if (send_now) {
compose.finish();
}
} catch (error) {
// We log an error if we can't open the compose box, but otherwise
// we continue, since this is not critical.
blueslip.warn(error.toString());
}
}
const pointer = Number.parseInt(vars.pointer, 10);
if (pointer) {
page_params.initial_pointer = pointer;
}
const offset = Number.parseInt(vars.offset, 10);
if (offset) {
page_params.initial_offset = offset;
}
const narrow_pointer = Number.parseInt(vars.narrow_pointer, 10);
if (narrow_pointer) {
page_params.initial_narrow_pointer = narrow_pointer;
}
const narrow_offset = Number.parseInt(vars.narrow_offset, 10);
if (narrow_offset) {
page_params.initial_narrow_offset = narrow_offset;
}
activity.set_new_user_input(false);
narrow.changehash(vars.oldhash);
}