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.

group-settings: Handle invalid urls correctly.

Sahil Batra2fe36cc0d731
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index db8b8de0de7..31f746e9d4b 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -8,6 +8,7 @@ import * as settings_data from "./settings_data";
import * as stream_data from "./stream_data";
import * as sub_store from "./sub_store";
import type {StreamSubscription} from "./sub_store";
+import * as user_groups from "./user_groups";
import type {UserGroup} from "./user_groups";
// TODO(typescript): Move to filter.js when it's converted to typescript.
@@ -238,3 +239,36 @@ export function validate_stream_settings_hash(hash: string): string {
}
return hash;
}
+
+export function validate_group_settings_hash(hash: string): string {
+ const hash_components = hash.slice(1).split(/\//);
+ const section = hash_components[1];
+
+ if (/\d+/.test(section)) {
+ const group_id = Number.parseInt(section, 10);
+ const group = user_groups.maybe_get_user_group_from_id(group_id);
+ if (!group) {
+ // Some users can type random url of the form
+ // /#groups/<random-group-id> we need to handle that.
+ return "#groups/your";
+ }
+
+ const group_name = hash_components[2];
+ let right_side_tab = hash_components[3];
+ const valid_right_side_tab_values = new Set(["general", "members"]);
+ if (group.name === group_name && valid_right_side_tab_values.has(right_side_tab)) {
+ return hash;
+ }
+ if (!valid_right_side_tab_values.has(right_side_tab)) {
+ right_side_tab = "general";
+ }
+ return group_edit_url(group, right_side_tab);
+ }
+
+ const valid_section_values = ["new", "your", "all"];
+ if (!valid_section_values.includes(section)) {
+ blueslip.info("invalid section for groups: " + section);
+ return "#groups/your";
+ }
+ return hash;
+}
diff --git a/web/src/hashchange.js b/web/src/hashchange.js
index d53f6df335b..0fd667b5a02 100644
--- a/web/src/hashchange.js
+++ b/web/src/hashchange.js
@@ -248,8 +248,12 @@ function do_hashchange_overlay(old_hash) {
}
}
- if (base === "groups" && !section) {
- history.replaceState(null, "", browser_history.get_full_url("groups/your"));
+ if (base === "groups") {
+ const valid_hash = hash_util.validate_group_settings_hash(window.location.hash);
+ if (valid_hash !== window.location.hash) {
+ history.replaceState(null, "", browser_history.get_full_url(valid_hash));
+ section = hash_parser.get_current_hash_section();
+ }
}
// Start by handling the specific case of going
diff --git a/web/src/user_group_edit.js b/web/src/user_group_edit.js
index 343abaf5470..fc8292a322b 100644
--- a/web/src/user_group_edit.js
+++ b/web/src/user_group_edit.js
@@ -568,33 +568,20 @@ export function change_state(section, right_side_tab) {
return;
}
- if (section === "your") {
- group_list_toggler.goto("your-groups");
- empty_right_panel();
- return;
- }
-
// if the section is a valid number.
if (/\d+/.test(section)) {
const group_id = Number.parseInt(section, 10);
const group = user_groups.get_user_group_from_id(group_id);
- if (!group) {
- // Some users can type random url of the form
- // /#groups/<random-group-id> we need to handle that.
- group_list_toggler.goto("your-groups");
- } else {
- show_right_section();
- // We show the list of user groups in the left panel
- // based on the tab that is active. It is `your-groups`
- // tab by default.
- redraw_user_group_list();
- select_tab = right_side_tab;
- switch_to_group_row(group);
- }
+ show_right_section();
+ // We show the list of user groups in the left panel
+ // based on the tab that is active. It is `your-groups`
+ // tab by default.
+ redraw_user_group_list();
+ select_tab = right_side_tab;
+ switch_to_group_row(group);
return;
}
- blueslip.info("invalid section for groups: " + section);
group_list_toggler.goto("your-groups");
empty_right_panel();
}
diff --git a/web/src/user_groups.ts b/web/src/user_groups.ts
index 5b78e24598a..e36dcea0e0d 100644
--- a/web/src/user_groups.ts
+++ b/web/src/user_groups.ts
@@ -67,6 +67,10 @@ export function get_user_group_from_id(group_id: number): UserGroup {
return user_group;
}
+export function maybe_get_user_group_from_id(group_id: number): UserGroup | undefined {
+ return user_group_by_id_dict.get(group_id);
+}
+
export function update(event: UserGroupUpdateEvent): void {
const group = get_user_group_from_id(event.group_id);
if (event.data.name !== undefined) {
diff --git a/web/tests/user_groups.test.js b/web/tests/user_groups.test.js
index 137ae83ebc0..abee0dd8394 100644
--- a/web/tests/user_groups.test.js
+++ b/web/tests/user_groups.test.js
@@ -49,6 +49,9 @@ run_test("user_groups", () => {
user_groups.add(admins);
assert.deepEqual(user_groups.get_user_group_from_id(admins.id), admins);
+ assert.equal(user_groups.maybe_get_user_group_from_id(99), undefined);
+ assert.deepEqual(user_groups.get_user_group_from_id(admins.id), admins);
+
const update_name_event = {
group_id: admins.id,
data: {