zulip/static/js/reload.js

333 lines
13 KiB
JavaScript
Raw Normal View History

import $ from "jquery";
import * as activity from "./activity";
import * as blueslip from "./blueslip";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import {csrf_token} from "./csrf";
reload: Manually save draft and preserve id when triggering reload. We received a complaint about the generation of multiple duplicate drafts for a single message. It was discovered that the likely cause of this was how we were handling clients that were frequently suspending/unsuspending, we would initiate a reload when we discovered this, and expect the `beforeunload` handler to save the draft. This behaved correctly, however, we would also save the compose state and fill it in via `preserve_state` in reload.js. The important detail here is that `preserve_state` would not encode and preserve the `draft_id` for the current message, partly because it had no way of knowing the `draft_id` of the draft... since we have not saved it yet, the `beforeunload` event happens after `preserve_state`. As such, performing any action that would trigger a draft to be saved, eg pressing Esc to close the compose box, would save a duplicate draft of the same message. To resolve the above bug, we (1) ensure that we call `drafts.update_draft()` in `preserve_state`, this returns a draft_id to us, which we (2) ensure that we encode as part of the url and (3) set on the `#composebox-textarea` as a `draft-id` data attribute, which we check the next time we try to save the draft, post reload. Note that this causes us to save the draft twice, once from preserve_state and then again from the `beforeunload` handler, but we do not add two drafts since the second update_draft call just edits the timestamp because it finds the `draft-id` data attribute on the `#composebox-textarea` set by the first call.
2021-11-30 17:57:42 +01:00
import * as drafts from "./drafts";
import * as hash_util from "./hash_util";
import * as hashchange from "./hashchange";
import {localstorage} from "./localstorage";
import * as message_list from "./message_list";
import * as message_lists from "./message_lists";
import * as narrow_state from "./narrow_state";
import {page_params} from "./page_params";
import * as reload_state from "./reload_state";
import * as server_events from "./server_events";
import * as ui_report from "./ui_report";
import * as util from "./util";
// Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html
function preserve_state(send_after_reload, save_pointer, save_narrow, save_compose) {
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;
}
let url = "#reload:send_after_reload=" + Number(send_after_reload);
url += "+csrf_token=" + encodeURIComponent(csrf_token);
if (save_compose) {
const msg_type = compose_state.get_message_type();
if (msg_type === "stream") {
url += "+msg_type=stream";
url += "+stream=" + encodeURIComponent(compose_state.stream_name());
url += "+topic=" + encodeURIComponent(compose_state.topic());
} else if (msg_type === "private") {
url += "+msg_type=private";
url += "+recipient=" + encodeURIComponent(compose_state.private_message_recipient());
}
if (msg_type) {
url += "+msg=" + encodeURIComponent(compose_state.message_content());
reload: Manually save draft and preserve id when triggering reload. We received a complaint about the generation of multiple duplicate drafts for a single message. It was discovered that the likely cause of this was how we were handling clients that were frequently suspending/unsuspending, we would initiate a reload when we discovered this, and expect the `beforeunload` handler to save the draft. This behaved correctly, however, we would also save the compose state and fill it in via `preserve_state` in reload.js. The important detail here is that `preserve_state` would not encode and preserve the `draft_id` for the current message, partly because it had no way of knowing the `draft_id` of the draft... since we have not saved it yet, the `beforeunload` event happens after `preserve_state`. As such, performing any action that would trigger a draft to be saved, eg pressing Esc to close the compose box, would save a duplicate draft of the same message. To resolve the above bug, we (1) ensure that we call `drafts.update_draft()` in `preserve_state`, this returns a draft_id to us, which we (2) ensure that we encode as part of the url and (3) set on the `#composebox-textarea` as a `draft-id` data attribute, which we check the next time we try to save the draft, post reload. Note that this causes us to save the draft twice, once from preserve_state and then again from the `beforeunload` handler, but we do not add two drafts since the second update_draft call just edits the timestamp because it finds the `draft-id` data attribute on the `#composebox-textarea` set by the first call.
2021-11-30 17:57:42 +01:00
const draft_id = drafts.update_draft();
if (draft_id) {
url += "+draft_id=" + encodeURIComponent(draft_id);
}
}
}
if (save_pointer) {
const pointer = message_lists.home.selected_id();
if (pointer !== -1) {
url += "+pointer=" + pointer;
}
}
if (save_narrow) {
const row = message_lists.home.selected_row();
if (!narrow_state.active()) {
if (row.length > 0) {
url += "+offset=" + row.offset().top;
}
} else {
url += "+offset=" + message_lists.home.pre_narrow_offset;
const narrow_pointer = message_list.narrowed.selected_id();
if (narrow_pointer !== -1) {
url += "+narrow_pointer=" + narrow_pointer;
}
const narrow_row = message_list.narrowed.selected_row();
if (narrow_row.length > 0) {
url += "+narrow_offset=" + narrow_row.offset().top;
}
}
}
url += hash_util.build_reload_url();
const ls = localstorage();
// Delete all the previous preserved states.
ls.removeRegex("reload:\\d+");
// 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.
const token = util.random_int(0, 1024 * 1024 * 1024 * 1024);
ls.set("reload:" + token, url);
window.location.replace("#reload:" + token);
}
// Check if we're doing a compose-preserving reload. This must be
// done before the first call to get_events
export function initialize() {
const location = window.location.toString();
const hash_fragment = location.slice(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.
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");
hashchange.changehash("");
return;
}
ls.remove(hash_fragment);
fragment = fragment.replace(/^reload:/, "");
const keyvals = fragment.split("+");
const vars = {};
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
for (const str of keyvals) {
const pair = str.split("=");
vars[pair[0]] = decodeURIComponent(pair[1]);
js: Automatically convert _.each to for…of. This commit was automatically generated by the following script, followed by lint --fix and a few small manual lint-related cleanups. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import { Context } from "ast-types/lib/path-visitor"; import K from "ast-types/gen/kinds"; import { NodePath } from "ast-types/lib/node-path"; import assert from "assert"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); const checkStatement = (node: n.Node): node is K.StatementKind => n.Statement.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; let inLoop = false; let replaceReturn = false; const visitLoop = (...args: string[]) => function(this: Context, path: NodePath) { for (const arg of args) { this.visit(path.get(arg)); } const old = { inLoop }; inLoop = true; this.visit(path.get("body")); inLoop = old.inLoop; return false; }; recast.visit(ast, { visitDoWhileStatement: visitLoop("test"), visitExpressionStatement(path) { const { expression, comments } = path.node; let valueOnly; if ( n.CallExpression.check(expression) && n.MemberExpression.check(expression.callee) && !expression.callee.computed && n.Identifier.check(expression.callee.object) && expression.callee.object.name === "_" && n.Identifier.check(expression.callee.property) && ["each", "forEach"].includes(expression.callee.property.name) && [2, 3].includes(expression.arguments.length) && checkExpression(expression.arguments[0]) && (n.FunctionExpression.check(expression.arguments[1]) || n.ArrowFunctionExpression.check(expression.arguments[1])) && [1, 2].includes(expression.arguments[1].params.length) && n.Identifier.check(expression.arguments[1].params[0]) && ((valueOnly = expression.arguments[1].params[1] === undefined) || n.Identifier.check(expression.arguments[1].params[1])) && (expression.arguments[2] === undefined || n.ThisExpression.check(expression.arguments[2])) ) { const old = { inLoop, replaceReturn }; inLoop = false; replaceReturn = true; this.visit( path .get("expression") .get("arguments") .get(1) .get("body") ); inLoop = old.inLoop; replaceReturn = old.replaceReturn; const [right, { body, params }] = expression.arguments; const loop = b.forOfStatement( b.variableDeclaration("let", [ b.variableDeclarator( valueOnly ? params[0] : b.arrayPattern([params[1], params[0]]) ), ]), valueOnly ? right : b.callExpression( b.memberExpression(right, b.identifier("entries")), [] ), checkStatement(body) ? body : b.expressionStatement(body) ); loop.comments = comments; path.replace(loop); changed = true; } this.traverse(path); }, visitForStatement: visitLoop("init", "test", "update"), visitForInStatement: visitLoop("left", "right"), visitForOfStatement: visitLoop("left", "right"), visitFunction(path) { this.visit(path.get("params")); const old = { replaceReturn }; replaceReturn = false; this.visit(path.get("body")); replaceReturn = old.replaceReturn; return false; }, visitReturnStatement(path) { if (replaceReturn) { assert(!inLoop); // could use labeled continue if this ever fires const { argument, comments } = path.node; if (argument === null) { const s = b.continueStatement(); s.comments = comments; path.replace(s); } else { const s = b.expressionStatement(argument); s.comments = comments; path.replace(s, b.continueStatement()); } return false; } this.traverse(path); }, visitWhileStatement: visitLoop("test"), }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
}
if (vars.msg !== undefined) {
const send_now = Number.parseInt(vars.send_after_reload, 10);
try {
// TODO: preserve focus
const topic = util.get_reload_topic(vars);
compose_actions.start(vars.msg_type, {
stream: vars.stream || "",
topic: topic || "",
private_message_recipient: vars.recipient || "",
content: vars.msg || "",
reload: Manually save draft and preserve id when triggering reload. We received a complaint about the generation of multiple duplicate drafts for a single message. It was discovered that the likely cause of this was how we were handling clients that were frequently suspending/unsuspending, we would initiate a reload when we discovered this, and expect the `beforeunload` handler to save the draft. This behaved correctly, however, we would also save the compose state and fill it in via `preserve_state` in reload.js. The important detail here is that `preserve_state` would not encode and preserve the `draft_id` for the current message, partly because it had no way of knowing the `draft_id` of the draft... since we have not saved it yet, the `beforeunload` event happens after `preserve_state`. As such, performing any action that would trigger a draft to be saved, eg pressing Esc to close the compose box, would save a duplicate draft of the same message. To resolve the above bug, we (1) ensure that we call `drafts.update_draft()` in `preserve_state`, this returns a draft_id to us, which we (2) ensure that we encode as part of the url and (3) set on the `#composebox-textarea` as a `draft-id` data attribute, which we check the next time we try to save the draft, post reload. Note that this causes us to save the draft twice, once from preserve_state and then again from the `beforeunload` handler, but we do not add two drafts since the second update_draft call just edits the timestamp because it finds the `draft-id` data attribute on the `#composebox-textarea` set by the first call.
2021-11-30 17:57:42 +01:00
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);
hashchange.changehash(vars.oldhash);
}
function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compose, message_html) {
if (reload_state.is_in_progress()) {
blueslip.log("do_reload_app: Doing nothing since reload_in_progress");
return;
}
// TODO: we should completely disable the UI here
if (save_pointer || save_narrow || save_compose) {
try {
preserve_state(send_after_reload, save_pointer, save_narrow, save_compose);
} catch (error) {
blueslip.error("Failed to preserve state", undefined, error.stack);
}
}
// TODO: We need a better API for showing messages.
ui_report.message(message_html, $("#reloading-application"));
blueslip.log("Starting server requested page reload");
reload_state.set_state_to_in_progress();
// 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", () => {
blueslip.log("Retrying on-focus page reload");
window.location.reload(true);
});
setInterval(() => {
blueslip.log("Retrying page reload due to 30s timer");
window.location.reload(true);
}, 30000);
try {
server_events.cleanup_event_queue();
} catch (error) {
blueslip.error("Failed to clean up before reloading", undefined, error.stack);
}
window.location.reload(true);
}
export function initiate({
immediate = false,
save_pointer = true,
save_narrow = true,
save_compose = true,
send_after_reload = false,
message_html = "Reloading ...",
}) {
if (immediate) {
do_reload_app(send_after_reload, save_pointer, save_narrow, save_compose, message_html);
}
if (reload_state.is_pending()) {
return;
}
reload_state.set_state_to_pending();
// We're now planning to execute a reload of the browser, usually
// to get an updated version of the Zulip web app code. Because in
// most cases all browsers will be receiving this notice at the
// same or similar times, we need to randomize the time that we do
// this in order to avoid a thundering herd overloading the server.
//
// Additionally, we try to do this reload at a time the user will
// not notice. So completely idle clients will reload first;
// those will an open compose box will wait until the message has
// been sent (or until it's clear the user isn't likely to send it).
//
// And then we unconditionally reload sometime after 30 minutes
// even if there is continued activity, because we don't support
// old JavaScript versions against newer servers and eventually
// letting that situation continue will lead to users seeing bugs.
//
// It's a little odd that how this timeout logic works with
// compose box resets including the random variance, but that
// makes it simple to reason about: We know that reloads will be
// spread over at least 5 minutes in all cases.
let idle_control;
const random_variance = util.random_int(0, 1000 * 60 * 5);
const unconditional_timeout = 1000 * 60 * 30 + random_variance;
const composing_idle_timeout = 1000 * 60 * 7 + random_variance;
const basic_idle_timeout = 1000 * 60 * 1 + random_variance;
let compose_started_handler;
function reload_from_idle() {
do_reload_app(false, save_pointer, save_narrow, save_compose, message_html);
}
// Make sure we always do a reload eventually after
// unconditional_timeout. Because we save cursor location and
// compose state when reloading, we expect this to not be
// particularly disruptive.
setTimeout(reload_from_idle, unconditional_timeout);
const compose_done_handler = function () {
// If the user sends their message or otherwise closes
// compose, we return them to the not-composing timeouts.
idle_control.cancel();
idle_control = $(document).idle({idle: basic_idle_timeout, onIdle: reload_from_idle});
$(document).off("compose_canceled.zulip compose_finished.zulip", compose_done_handler);
$(document).on("compose_started.zulip", compose_started_handler);
};
compose_started_handler = function () {
// If the user stops being idle and starts composing a
// message, switch to the compose-open timeouts.
idle_control.cancel();
idle_control = $(document).idle({idle: composing_idle_timeout, onIdle: reload_from_idle});
$(document).off("compose_started.zulip", compose_started_handler);
$(document).on("compose_canceled.zulip compose_finished.zulip", compose_done_handler);
};
2017-03-18 18:48:43 +01:00
if (compose_state.composing()) {
idle_control = $(document).idle({idle: composing_idle_timeout, onIdle: reload_from_idle});
$(document).on("compose_canceled.zulip compose_finished.zulip", compose_done_handler);
} else {
idle_control = $(document).idle({idle: basic_idle_timeout, onIdle: reload_from_idle});
$(document).on("compose_started.zulip", compose_started_handler);
}
}
window.addEventListener("beforeunload", () => {
// 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.
blueslip.log("Setting reload_in_progress in beforeunload handler");
reload_state.set_state_to_in_progress();
});
reload_state.set_csrf_failed_handler(() => {
initiate({
immediate: true,
save_pointer: true,
save_narrow: true,
save_compose: true,
});
});