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.

hashchange: For hash that needs auth, prompt spectator to login.

Aman Agrawal3ed833a7e44b
diff --git a/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js
index 8586b74c496..b648e0fe232 100644
--- a/frontend_tests/node_tests/hashchange.js
+++ b/frontend_tests/node_tests/hashchange.js
@@ -26,7 +26,7 @@ const drafts = mock_esm("../../static/js/drafts");
const floating_recipient_bar = mock_esm("../../static/js/floating_recipient_bar");
const info_overlay = mock_esm("../../static/js/info_overlay");
const message_viewport = mock_esm("../../static/js/message_viewport");
-const narrow = mock_esm("../../static/js/narrow");
+const narrow = zrequire("../../static/js/narrow");
const overlays = mock_esm("../../static/js/overlays");
const settings = mock_esm("../../static/js/settings");
const stream_settings_ui = mock_esm("../../static/js/stream_settings_ui");
diff --git a/static/js/hash_util.js b/static/js/hash_util.js
index 19fc6c731b3..c788f6769cd 100644
--- a/static/js/hash_util.js
+++ b/static/js/hash_util.js
@@ -270,3 +270,39 @@ export function active_stream() {
export function is_create_new_stream_narrow() {
return window.location.hash === "#streams/new";
}
+
+export const allowed_web_public_narrows = [
+ "streams",
+ "stream",
+ "topic",
+ "sender",
+ "has",
+ "search",
+ "near",
+ "id",
+];
+
+export function is_web_public_compatible(hash) {
+ const web_public_allowed_hashes = [
+ "",
+ "narrow", // full #narrow hash handled in narrow.is_web_public_compatible
+ "recent_topics",
+ "keyboard-shortcuts",
+ "message-formatting",
+ "search-operators",
+ "all_messages",
+ "about-zulip",
+ ];
+
+ const main_hash = get_hash_category(hash);
+
+ if (main_hash === "narrow") {
+ const hash_section = get_hash_section(hash);
+ if (!allowed_web_public_narrows.includes(hash_section)) {
+ return false;
+ }
+ return true;
+ }
+
+ return web_public_allowed_hashes.includes(main_hash);
+}
diff --git a/static/js/hashchange.js b/static/js/hashchange.js
index e58f7992ad2..a035f497d58 100644
--- a/static/js/hashchange.js
+++ b/static/js/hashchange.js
@@ -9,6 +9,7 @@ import * as floating_recipient_bar from "./floating_recipient_bar";
import * as hash_util from "./hash_util";
import * as info_overlay from "./info_overlay";
import * as invite from "./invite";
+import * as login_to_access from "./login_to_access";
import * as message_lists from "./message_lists";
import * as message_viewport from "./message_viewport";
import * as narrow from "./narrow";
@@ -312,6 +313,7 @@ function do_hashchange_overlay(old_hash) {
}
function hashchanged(from_reload, e) {
+ const current_hash = window.location.hash;
const old_hash = e && (e.oldURL ? new URL(e.oldURL).hash : browser_history.old_hash());
const was_internal_change = browser_history.save_old_hash();
@@ -327,7 +329,13 @@ function hashchanged(from_reload, e) {
return undefined;
}
- if (hash_util.is_overlay_hash(window.location.hash)) {
+ const is_hash_web_public_compatible = hash_util.is_web_public_compatible(current_hash);
+ if (page_params.is_spectator && !is_hash_web_public_compatible) {
+ login_to_access.show();
+ return undefined;
+ }
+
+ if (hash_util.is_overlay_hash(current_hash)) {
browser_history.state.changing_hash = true;
do_hashchange_overlay(old_hash);
browser_history.state.changing_hash = false;