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.

hash: Fix logic for handling web public narrows.

opmkumar86eb69a68b64
diff --git a/web/src/hash_parser.ts b/web/src/hash_parser.ts
index 084804b1101..8d3b5d6a4cc 100644
--- a/web/src/hash_parser.ts
+++ b/web/src/hash_parser.ts
@@ -163,13 +163,16 @@ export function is_spectator_compatible(hash: string): boolean {
const main_hash = get_hash_category(hash);
if (main_hash === "narrow") {
- let hash_section = get_hash_section(hash);
- const second_hash_section = get_nth_hash_section(hash, 2);
- if (hash_section.startsWith("-")) {
- hash_section = hash_section.slice(1);
- }
- if (!is_an_allowed_web_public_narrow(hash_section, second_hash_section)) {
- return false;
+ const hash_components = hash
+ .split(/\//)
+ .filter((hash_component) => hash_component !== "#narrow");
+
+ for (let i = 0; i < hash_components.length; i += 2) {
+ const hash_section = hash_components[i]!.replace(/^-/, "");
+ const second_hash_section = hash_components[i + 1]!;
+ if (!is_an_allowed_web_public_narrow(hash_section, second_hash_section)) {
+ return false;
+ }
}
return true;
}
diff --git a/web/tests/hashchange.test.cjs b/web/tests/hashchange.test.cjs
index d698ee2f7b0..bd076b560fb 100644
--- a/web/tests/hashchange.test.cjs
+++ b/web/tests/hashchange.test.cjs
@@ -6,6 +6,7 @@ const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
const blueslip = require("./lib/zblueslip.cjs");
const $ = require("./lib/zjquery.cjs");
+const {page_params} = require("./lib/zpage_params.cjs");
let $window_stub;
set_global("to_$", () => $window_stub);
@@ -24,6 +25,9 @@ const settings = mock_esm("../src/settings");
mock_esm("../src/settings_data", {
user_can_create_public_streams: () => true,
});
+const spectators = mock_esm("../src/spectators", {
+ login_to_access() {},
+});
const stream_settings_ui = mock_esm("../src/stream_settings_ui");
const ui_util = mock_esm("../src/ui_util");
const ui_report = mock_esm("../src/ui_report");
@@ -210,6 +214,7 @@ function test_helper({override, override_rewire, change_tab}) {
stub(stream_settings_ui, "launch");
stub(ui_util, "blur_active_element");
stub(ui_report, "error");
+ stub(spectators, "login_to_access");
if (change_tab) {
override_rewire(message_view, "show", (terms) => {
@@ -322,6 +327,28 @@ run_test("hash_interactions", ({override, override_rewire}) => {
terms = helper.get_narrow_terms();
assert.equal(terms.length, 0);
+ page_params.is_spectator = true;
+
+ window.location.hash = "#narrow/is/resolved/has/reaction";
+ helper.clear_events();
+ $window_stub.trigger("hashchange");
+ helper.assert_events([
+ [overlays, "close_for_hash_change"],
+ [message_viewport, "stop_auto_scrolling"],
+ "message_view.show",
+ ]);
+ terms = helper.get_narrow_terms();
+ assert.equal(terms.length, 2);
+
+ // Test a narrow that spectators are not permitted to access.
+ window.location.hash = "#narrow/is/resolved/is/unread";
+
+ helper.clear_events();
+ $window_stub.trigger("hashchange");
+ helper.assert_events([[spectators, "login_to_access"]]);
+
+ page_params.is_spectator = false;
+
// Test an invalid narrow hash
window.location.hash = "#narrow/foo.foo";