Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

reload_setup: Convert module to typescript.

Evy Kassirer12a6e22283ec
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index 46754728104..b473bda5eab 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -198,7 +198,7 @@ EXEMPT_FILES = make_set(
"web/src/realm_playground.ts",
"web/src/recent_view_ui.ts",
"web/src/reload.ts",
- "web/src/reload_setup.js",
+ "web/src/reload_setup.ts",
"web/src/reminders_overlay_ui.ts",
"web/src/resize.ts",
"web/src/resize_handler.ts",
diff --git a/web/src/reload.ts b/web/src/reload.ts
index 69866785947..b555ec12a4f 100644
--- a/web/src/reload.ts
+++ b/web/src/reload.ts
@@ -54,12 +54,12 @@ function preserve_state(send_after_reload: boolean, save_compose: boolean): void
return;
}
- let url = "#reload:send_after_reload=" + Number(send_after_reload);
+ let url = "#reload:send_after_reload=" + (send_after_reload ? "1" : "0");
assert(csrf_token !== undefined);
url += "+csrf_token=" + encodeURIComponent(csrf_token);
- if (save_compose) {
- const msg_type = compose_state.get_message_type();
+ const msg_type = compose_state.get_message_type();
+ if (save_compose && msg_type) {
if (msg_type === "stream") {
const stream_id = compose_state.stream_id();
url += "+msg_type=stream";
@@ -74,12 +74,10 @@ function preserve_state(send_after_reload: boolean, save_compose: boolean): void
encodeURIComponent(compose_state.private_message_recipient_emails());
}
- if (msg_type) {
- url += "+msg=" + encodeURIComponent(compose_state.message_content());
- const draft_id = drafts.update_draft();
- if (draft_id) {
- url += "+draft_id=" + encodeURIComponent(draft_id);
- }
+ url += "+msg=" + encodeURIComponent(compose_state.message_content());
+ const draft_id = drafts.update_draft();
+ if (draft_id) {
+ url += "+draft_id=" + encodeURIComponent(draft_id);
}
}
diff --git a/web/src/reload_setup.js b/web/src/reload_setup.ts
similarity index 54%
rename from web/src/reload_setup.js
rename to web/src/reload_setup.ts
index 352a5049a9e..1aafbb0e36b 100644
--- a/web/src/reload_setup.js
+++ b/web/src/reload_setup.ts
@@ -1,3 +1,6 @@
+import assert from "minimalistic-assert";
+import * as z from "zod/mini";
+
import * as activity from "./activity.ts";
import * as blueslip from "./blueslip.ts";
import * as compose from "./compose.ts";
@@ -10,7 +13,30 @@ import * as people from "./people.ts";
// Check if we're doing a compose-preserving reload. This must be
// done before the first call to get_events
-export function initialize() {
+// See `reload.preserve_state` for context on how this is built.
+const reload_vars_schema = z.intersection(
+ z.object({
+ oldhash: z.string(),
+ narrow_pointer: z.optional(z.string()),
+ narrow_offset: z.optional(z.string()),
+ send_after_reload: z.enum(["0", "1"]),
+ }),
+ z.union([
+ z.object({
+ msg: z.optional(z.undefined()),
+ }),
+ z.object({
+ msg: z.string(),
+ recipient: z.string(),
+ message_type: z.enum(["private", "stream"]),
+ stream_id: z.optional(z.string()),
+ topic: z.optional(z.string()),
+ draft_id: z.optional(z.string()),
+ }),
+ ]),
+);
+
+export function initialize(): void {
// window.location.hash should be e.g. `#reload:12345123412312`
if (!window.location.hash.startsWith("#reload:")) {
return;
@@ -22,7 +48,7 @@ export function initialize() {
// 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);
+ const 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
@@ -37,32 +63,50 @@ export function initialize() {
// 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 = {};
+ // `z.string().parse(fragment)` branch once you can no longer
+ // directly upgrade from Zulip 5.x to the current version.
+ let parsed_fragment: string;
+ const parse_with_url = z.object({url: z.string()}).safeParse(fragment);
+ if (parse_with_url.success) {
+ parsed_fragment = parse_with_url.data.url;
+ } else {
+ parsed_fragment = z.string().parse(fragment);
+ }
+ const match = /^#reload:(.*)/.exec(parsed_fragment);
+ assert(match !== null);
+ const matched_fragment = match[1];
+ assert(matched_fragment !== undefined);
+ const keyvals: string[] = matched_fragment.split("+");
+ const raw_vars: Record<string, string> = {};
for (const str of keyvals) {
const pair = str.split("=");
- vars[pair[0]] = decodeURIComponent(pair[1]);
+ assert(pair.length === 2);
+ raw_vars[pair[0]!] = decodeURIComponent(pair[1]!);
}
+ const vars = reload_vars_schema.parse(raw_vars);
if (vars.msg !== undefined) {
- const send_now = Number.parseInt(vars.send_after_reload, 10);
+ const send_now = vars.send_after_reload === "1";
try {
const private_message_recipient_ids = vars.recipient
? people.emails_string_to_user_ids(vars.recipient)
: [];
+ let message_type: "private" | "stream";
+ if (vars.message_type === "private") {
+ message_type = "private";
+ } else {
+ message_type = "stream";
+ }
compose_actions.start({
- message_type: vars.msg_type,
- stream_id: Number.parseInt(vars.stream_id, 10) || undefined,
- topic: vars.topic || "",
+ message_type,
+ stream_id: vars.stream_id ? Number.parseInt(vars.stream_id, 10) : undefined,
+ topic: vars.topic ?? "",
private_message_recipient_ids,
- content: vars.msg || "",
- draft_id: vars.draft_id || "",
+ content: vars.msg ?? "",
+ draft_id: vars.draft_id ?? "",
});
if (send_now) {
compose.finish();
@@ -70,7 +114,7 @@ export function initialize() {
} 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());
+ blueslip.warn(String(error));
}
}
@@ -80,11 +124,13 @@ export function initialize() {
// user gets back to these narrow much later (maybe days) and finds them at a random position in
// narrow which they didn't navigate to while they were trying to just get to the latest unread
// message in that narrow which will now take more effort to find.
- const narrow_pointer = Number.parseInt(vars.narrow_pointer, 10);
- const narrow_offset = Number.parseInt(vars.narrow_offset, 10);
+ const narrow_pointer = vars.narrow_pointer
+ ? Number.parseInt(vars.narrow_pointer, 10)
+ : undefined;
+ const narrow_offset = vars.narrow_offset ? Number.parseInt(vars.narrow_offset, 10) : undefined;
message_fetch.set_initial_pointer_and_offset({
- narrow_pointer: Number.isNaN(narrow_pointer) ? undefined : narrow_pointer,
- narrow_offset: Number.isNaN(narrow_offset) ? undefined : narrow_offset,
+ narrow_pointer,
+ narrow_offset,
});
activity.set_new_user_input(false);
diff --git a/web/src/ui_init.js b/web/src/ui_init.js
index bb32c4a31b6..6e12e4e8ef4 100644
--- a/web/src/ui_init.js
+++ b/web/src/ui_init.js
@@ -106,7 +106,7 @@ import * as realm_logo from "./realm_logo.ts";
import * as realm_playground from "./realm_playground.ts";
import * as realm_user_settings_defaults from "./realm_user_settings_defaults.ts";
import * as recent_view_ui from "./recent_view_ui.ts";
-import * as reload_setup from "./reload_setup.js";
+import * as reload_setup from "./reload_setup.ts";
import * as reminders_overlay_ui from "./reminders_overlay_ui.ts";
import * as resize_handler from "./resize_handler.ts";
import * as saved_snippets from "./saved_snippets.ts";