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.

login_to_access: Take user to web_public_compatible hash on exit.

Aman Agrawalcfd81b1bf419
diff --git a/frontend_tests/node_tests/browser_history.js b/frontend_tests/node_tests/browser_history.js
index 8192dcf01f1..68af4cee431 100644
--- a/frontend_tests/node_tests/browser_history.js
+++ b/frontend_tests/node_tests/browser_history.js
@@ -67,3 +67,14 @@ test("update internal hash if required", ({override}) => {
// calls to stub.
assert.equal(stub.num_calls, 1);
});
+
+test("web public view hash restore", () => {
+ const allowed_hash = "#";
+ browser_history.update(allowed_hash);
+ assert.equal(location.hash, allowed_hash);
+ const new_hash = "#narrow/is/private";
+ browser_history.update(new_hash);
+ assert.equal(location.hash, new_hash);
+ browser_history.return_to_web_public_hash();
+ assert.equal(location.hash, allowed_hash);
+});
diff --git a/static/js/browser_history.js b/static/js/browser_history.js
index 38a732e4892..0e925e5def2 100644
--- a/static/js/browser_history.js
+++ b/static/js/browser_history.js
@@ -7,6 +7,13 @@ export const state = {
hash_before_overlay: null,
old_hash: window.location.hash,
changing_hash: false,
+ // If the spectator's hash changes to a restricted hash, then we store the old hash
+ // so that we can take user back to the allowed hash.
+ // TODO: Store #narrow old hashes. Currently they are not stored here since, the #narrow
+ // hashes are changed without calling `hashchanged` in many ways.
+ spectator_old_hash: hash_util.is_web_public_compatible(window.location.hash)
+ ? window.location.hash
+ : "#",
};
export function clear_for_testing() {
@@ -73,3 +80,7 @@ export function update_hash_internally_if_required(hash) {
update(hash);
}
}
+
+export function return_to_web_public_hash() {
+ window.location.hash = state.spectator_old_hash;
+}
diff --git a/static/js/click_handlers.js b/static/js/click_handlers.js
index 0768fc0f4b6..4198d337733 100644
--- a/static/js/click_handlers.js
+++ b/static/js/click_handlers.js
@@ -44,6 +44,15 @@ import * as user_profile from "./user_profile";
import * as util from "./util";
export function initialize() {
+ // SPECATORS LOGIN TO ACCESS MODAL
+
+ $("body").on("click hide", ".go_back_button", (e) => {
+ browser_history.return_to_web_public_hash();
+ $("#login_to_access_modal").modal("hide");
+ e.preventDefault();
+ e.stopPropagation();
+ });
+
// MESSAGE CLICKING
function initialize_long_tap() {
diff --git a/static/js/hashchange.js b/static/js/hashchange.js
index a035f497d58..239ff37dcf1 100644
--- a/static/js/hashchange.js
+++ b/static/js/hashchange.js
@@ -318,6 +318,11 @@ function hashchanged(from_reload, e) {
const was_internal_change = browser_history.save_old_hash();
+ const is_hash_web_public_compatible = hash_util.is_web_public_compatible(current_hash);
+ if (is_hash_web_public_compatible) {
+ browser_history.state.spectator_old_hash = current_hash;
+ }
+
if (was_internal_change) {
return undefined;
}
@@ -329,7 +334,6 @@ function hashchanged(from_reload, e) {
return undefined;
}
- 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;