Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
hashchange: Fix crash on "#topics" links that aren't for a channel.
Aman Agrawal647877b9be7b
diff --git a/web/src/hashchange.ts b/web/src/hashchange.tsindex f346ca5c369..b7ec2b6d721 100644--- a/web/src/hashchange.ts+++ b/web/src/hashchange.ts@@ -167,6 +167,10 @@ function show_home_view(narrow_opts?: message_view.ShowMessageViewOpts): void {}}+function show_invalid_url_error(): void {+ ui_report.error($t_html({defaultMessage: "Invalid URL"}), undefined, $("#home-error"), 2000);+}+// Returns true if this function performed a narrowfunction do_hashchange_normal(from_reload: boolean, restore_selected_id: boolean): boolean {message_viewport.stop_auto_scrolling();@@ -204,12 +208,7 @@ function do_hashchange_normal(from_reload: boolean, restore_selected_id: boolean// TODO: Show possible valid URLs to the user.terms = hash_util.parse_narrow(hash);} catch {- ui_report.error(- $t_html({defaultMessage: "Invalid URL"}),- undefined,- $("#home-error"),- 2000,- );+ show_invalid_url_error();}if (terms === undefined) {// If the narrow URL didn't parse,@@ -222,12 +221,15 @@ function do_hashchange_normal(from_reload: boolean, restore_selected_id: boolean}// Show inbox style topics list for #topics narrow.- if (hash[0] === "#topics" && terms.length === 1) {- const channel_id_string = hash[2];- if (channel_id_string) {- inbox_ui.show(new Filter(terms));- return true;+ if (hash[0] === "#topics") {+ const filter = new Filter(terms);+ if (!filter.is_channel_view()) {+ show_invalid_url_error();+ show_home_view();+ return false;}+ inbox_ui.show(filter);+ return true;}message_view.show(terms, narrow_opts);diff --git a/web/tests/hashchange.test.cjs b/web/tests/hashchange.test.cjsindex c3ae9db48be..91a97babeb8 100644--- a/web/tests/hashchange.test.cjs+++ b/web/tests/hashchange.test.cjs@@ -17,6 +17,7 @@ const history = set_global("history", {state: null});const admin = mock_esm("../src/admin");const drafts_overlay_ui = mock_esm("../src/drafts_overlay_ui");+const inbox_ui = mock_esm("../src/inbox_ui");const info_overlay = mock_esm("../src/info_overlay");const message_viewport = mock_esm("../src/message_viewport");const overlays = mock_esm("../src/overlays");@@ -273,6 +274,38 @@ run_test("hash_interactions", ({override, override_rewire}) => {let terms = helper.get_narrow_terms();assert.equal(terms[0].operand, denmark_id.toString());+ let inbox_filter;+ override(inbox_ui, "show", (filter) => {+ inbox_filter = filter;+ });+ window.location.hash = "#topics/channel/Denmark";++ helper.clear_events();+ $window_stub.trigger("hashchange");+ helper.assert_events([+ [overlays, "close_for_hash_change"],+ [message_viewport, "stop_auto_scrolling"],+ ]);+ assert.equal(inbox_filter.terms()[0].operand, denmark_id.toString());++ // Only a channel has a topics list.+ for (const hash of ["#topics", "#topics/is/starred", "#topics/channel/Denmark/topic/lunch"]) {+ window.location.hash = hash;+ recent_view_ui_shown = false;+ inbox_filter = undefined;++ helper.clear_events();+ $window_stub.trigger("hashchange");+ helper.assert_events([+ [overlays, "close_for_hash_change"],+ [message_viewport, "stop_auto_scrolling"],+ [ui_report, "error"],+ ]);+ // The home view is shown, and not a topics list.+ assert.equal(recent_view_ui_shown, true);+ assert.equal(inbox_filter, undefined);+ }+window.location.hash = "#narrow";helper.clear_events();