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.

channel_folders: Let a new channel in a folder be created through a hash.

Evy Kassirer31fb0bd6d73f
diff --git a/web/src/channel_folders.ts b/web/src/channel_folders.ts
index 2627b8e8d45..d3af26e2e30 100644
--- a/web/src/channel_folders.ts
+++ b/web/src/channel_folders.ts
@@ -35,3 +35,7 @@ export function get_channel_folders(include_archived = false): ChannelFolder[] {
return true;
});
}
+
+export function is_valid_folder_id(folder_id: number): boolean {
+ return channel_folder_by_id_dict.has(folder_id);
+}
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 8d83983ca23..6eeb2982c10 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -1,6 +1,9 @@
+import assert from "minimalistic-assert";
+
import * as internal_url from "../shared/src/internal_url.ts";
import * as blueslip from "./blueslip.ts";
+import * as channel_folders from "./channel_folders.ts";
import type {Message} from "./message_store.ts";
import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
@@ -257,8 +260,30 @@ export function channels_settings_section_url(section = "subscribed"): string {
return `#channels/${section}`;
}
+// For folders we only support #channels/folders/{folder_id}/new
+// In the future we'd like to support #channels/folder/{folder_id}
+// for displaying the channels in a folder.
+function channels_settings_folder_url(hash: string): string {
+ const hash_components = hash.slice(1).split(/\//);
+ assert(hash_components[1] === "folders");
+ if (hash_components.length !== 4 || hash_components[3] !== "new") {
+ blueslip.warn("invalid hash for channels settings with folders: " + hash);
+ return "#channels/subscribed";
+ }
+ const folder_id = Number.parseInt(hash_components[2]!, 10);
+ if (!channel_folders.is_valid_folder_id(folder_id)) {
+ blueslip.warn("invalid folder id: " + folder_id);
+ return "#channels/new";
+ }
+ return hash;
+}
+
export function validate_channels_settings_hash(hash: string): string {
const hash_components = hash.slice(1).split(/\//);
+ if (hash_components[1] === "folders") {
+ return channels_settings_folder_url(hash);
+ }
+
const section = hash_components[1];
const can_create_streams =
diff --git a/web/src/hashchange.ts b/web/src/hashchange.ts
index 06d7b94c29b..ee44cb7aea6 100644
--- a/web/src/hashchange.ts
+++ b/web/src/hashchange.ts
@@ -339,6 +339,17 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
// the new overlay.
if (coming_from_overlay && base === old_base) {
if (base === "channels") {
+ if (hash_parser.get_current_nth_hash_section(1) === "folders") {
+ const folder_id = hash_parser.get_current_nth_hash_section(2);
+ stream_settings_ui.change_state(
+ "new",
+ undefined,
+ "",
+ Number.parseInt(folder_id, 10),
+ );
+ return;
+ }
+
// e.g. #channels/29/social/subscribers
const right_side_tab = hash_parser.get_current_nth_hash_section(3);
stream_settings_ui.change_state(section, undefined, right_side_tab);
@@ -411,6 +422,12 @@ function do_hashchange_overlay(old_hash: string | undefined): void {
}
if (base === "channels") {
+ if (hash_parser.get_current_nth_hash_section(1) === "folders") {
+ const folder_id = hash_parser.get_current_nth_hash_section(2);
+ stream_settings_ui.launch("new", undefined, "", Number.parseInt(folder_id, 10));
+ return;
+ }
+
// e.g. #channels/29/social/subscribers
const right_side_tab = hash_parser.get_current_nth_hash_section(3);
diff --git a/web/src/stream_create.ts b/web/src/stream_create.ts
index 5a1afc89a4f..ec2642b755c 100644
--- a/web/src/stream_create.ts
+++ b/web/src/stream_create.ts
@@ -456,7 +456,7 @@ function create_stream(): void {
});
}
-export function new_stream_clicked(stream_name: string): void {
+export function new_stream_clicked(stream_name: string, folder_id: number | undefined): void {
// this changes the tab switcher (settings/preview) which isn't necessary
// to a add new stream title.
stream_settings_components.show_subs_pane.create_stream();
@@ -466,6 +466,11 @@ export function new_stream_clicked(stream_name: string): void {
$("#create_stream_name").val(stream_name);
}
show_new_stream_modal();
+ if (folder_id) {
+ folder_widget!.render(folder_id);
+ } else {
+ folder_widget!.render(-1);
+ }
$("#create_stream_name").trigger("focus");
}
diff --git a/web/src/stream_settings_ui.ts b/web/src/stream_settings_ui.ts
index c6427e2d4e0..d367a2a8a70 100644
--- a/web/src/stream_settings_ui.ts
+++ b/web/src/stream_settings_ui.ts
@@ -971,11 +971,12 @@ export function change_state(
section: string,
left_side_tab: string | undefined,
right_side_tab: string,
+ folder_id?: number,
): void {
assert(toggler !== undefined);
// if in #channels/new form.
if (section === "new") {
- do_open_create_stream();
+ do_open_create_stream(folder_id);
show_right_section();
resize.resize_settings_creation_overlay($("#channels_overlay_container"));
return;
@@ -1036,6 +1037,7 @@ export function launch(
section: string,
left_side_tab: string | undefined,
right_side_tab: string,
+ folder_id?: number,
): void {
setup_page(() => {
overlays.open_overlay({
@@ -1045,7 +1047,7 @@ export function launch(
browser_history.exit_overlay();
},
});
- change_state(section, left_side_tab, right_side_tab);
+ change_state(section, left_side_tab, right_side_tab, folder_id);
setTimeout(() => {
if (!stream_settings_components.get_active_data().id) {
if (section === "new") {
@@ -1148,7 +1150,7 @@ export function view_stream(): void {
}
}
-export function do_open_create_stream(): void {
+export function do_open_create_stream(folder_id?: number): void {
// Only call this directly for hash changes.
// Prefer open_create_stream().
@@ -1161,7 +1163,7 @@ export function do_open_create_stream(): void {
return;
}
- stream_create.new_stream_clicked(stream.trim());
+ stream_create.new_stream_clicked(stream.trim(), folder_id);
}
export function open_create_stream(): void {
diff --git a/web/tests/channel_folders.test.cjs b/web/tests/channel_folders.test.cjs
index 41c6e38a8e7..aaa1c783845 100644
--- a/web/tests/channel_folders.test.cjs
+++ b/web/tests/channel_folders.test.cjs
@@ -56,4 +56,7 @@ run_test("basics", () => {
backend_folder,
devops_folder,
]);
+
+ assert.ok(channel_folders.is_valid_folder_id(frontend_folder.id));
+ assert.ok(!channel_folders.is_valid_folder_id(999));
});