Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_create: Wait for subscriber fetches before creating.
Shubham Padiacfd9f5ebe784
diff --git a/web/src/stream_create.ts b/web/src/stream_create.tsindex c9363e1f3d0..444edc1fe77 100644--- a/web/src/stream_create.ts+++ b/web/src/stream_create.ts@@ -20,6 +20,7 @@ import {current_user, realm} from "./state_data.ts";import * as stream_create_subscribers from "./stream_create_subscribers.ts";import * as stream_data from "./stream_data.ts";import * as stream_edit from "./stream_edit.ts";+import * as stream_pill from "./stream_pill.ts";import * as stream_settings_components from "./stream_settings_components.ts";import * as stream_settings_data from "./stream_settings_data.ts";import {stream_permission_group_settings_schema} from "./stream_types.ts";@@ -81,6 +82,13 @@ class StreamSubscriptionError {$("#stream_subscription_error").show();}+ report_subscriber_fetch_failed(failed_stream_id: number): void {+ $("#stream_subscription_error").text(+ stream_pill.get_subscriber_fetch_failure_message(failed_stream_id),+ );+ $("#stream_subscription_error").show();+ }+clear_errors(): void {$("#stream_subscription_error").hide();}@@ -296,6 +304,7 @@ function create_stream(): void {$t_html({defaultMessage: "The channel description cannot contain newline characters."}),$(".stream_create_info"),);+ set_create_form_disabled(false);return;}const subscriptions = JSON.stringify([{name: stream_name, description}]);@@ -412,6 +421,9 @@ function create_stream(): void {$("#create_stream_name").val("");$("#create_stream_description").val("");loading.destroy_indicator($("#stream_creating_indicator"));+ // The form is cleared, so this cannot create the channel twice.+ // Needed in case the event that closes the form never arrives.+ set_create_form_disabled(false);// The rest of the work is done via the subscribe event we will get},error(xhr): void {@@ -445,6 +457,7 @@ function create_stream(): void {);}loading.destroy_indicator($("#stream_creating_indicator"));+ set_create_form_disabled(false);},});}@@ -474,12 +487,29 @@ function clear_error_display(): void {stream_subscription_error.clear_errors();}+function set_create_form_disabled(disabled: boolean): void {+ $(+ "#stream-creation .finalize_create_stream, #stream-creation #stream_creation_go_to_configure_channel_settings",+ ).prop("disabled", disabled);+ // Pills added or removed after the subscribers are read would+ // show in the preview but not end up in the channel.+ const $add_subscribers_container = $("#people_to_add .add_subscribers_container");+ $add_subscribers_container.toggleClass("add_subscribers_disabled", disabled);+ $add_subscribers_container.find(".input").prop("contenteditable", !disabled);+}+export function show_new_stream_modal(): void {$("#stream-creation").removeClass("hide");$(".right .settings").hide();stream_create_subscribers.build_widgets();+ // A previous create click may have left these buttons disabled,+ // either because the channel was created or because the form was+ // closed while waiting for subscriber data. The sticky footer is+ // not re-rendered when the form reopens, so reset the buttons here.+ set_create_form_disabled(false);+// Make the options default to the same each time// The message retention setting is visible to owners only. The below block@@ -608,12 +638,6 @@ export function set_up_handlers(): void {return;}- const principals = stream_create_subscribers.get_principals();- if (principals.length === 0) {- stream_subscription_error.report_no_subs_to_stream();- return;- }-assert(stream_create_subscribers.pill_widget !== undefined);assert(stream_create_subscribers.pill_widget !== null);if (stream_create_subscribers.pill_widget.is_pending()) {@@ -626,23 +650,62 @@ export function set_up_handlers(): void {return;}- if (principals.length >= 50) {- const modal_content_html = render_subscription_invites_warning_modal({- channel_name: stream_name,- count: principals.length,- });+ void (async () => {+ // Disable the create button until the channel is created,+ // so that clicking it again (or pressing Enter) while we+ // wait for subscriber data or for the server cannot create+ // the channel twice, the back button so that the settings+ // we validated cannot be changed underneath us, and the+ // pill input so that the preview matches what is created.+ // They are re-enabled if creating fails, or when the form+ // is reopened.+ set_create_form_disabled(true);+ const result = await stream_create_subscribers.add_subscribers_from_pills();+ // The creation form was closed while we were fetching+ // subscriber data, so don't create the channel. Leave the+ // buttons alone, since a newly opened form may have+ // disabled them for its own fetch.+ if (result.status === "form_closed") {+ return;+ }+ if (result.status === "failed") {+ set_create_form_disabled(false);+ stream_subscription_error.report_subscriber_fetch_failed(result.failed_stream_id);+ return;+ }- confirm_dialog.launch({- modal_title_html: $t_html({defaultMessage: "Large number of subscribers"}),- modal_content_html,- is_compact: true,- on_click() {- create_stream();- },- });- } else {- create_stream();- }+ const principals = stream_create_subscribers.get_principals();+ if (principals.length === 0) {+ set_create_form_disabled(false);+ stream_subscription_error.report_no_subs_to_stream();+ return;+ }++ if (principals.length >= 50) {+ const modal_content_html = render_subscription_invites_warning_modal({+ channel_name: stream_name,+ count: principals.length,+ });++ let confirmed = false;+ confirm_dialog.launch({+ modal_title_html: $t_html({defaultMessage: "Large number of subscribers"}),+ modal_content_html,+ is_compact: true,+ on_click() {+ confirmed = true;+ create_stream();+ },+ on_hidden() {+ if (!confirmed) {+ set_create_form_disabled(false);+ }+ },+ });+ } else {+ create_stream();+ }+ })();});function handle_channel_name_length_limit(): void {diff --git a/web/src/stream_create_subscribers.ts b/web/src/stream_create_subscribers.tsindex cd427fd2c78..f0a31ea3998 100644--- a/web/src/stream_create_subscribers.ts+++ b/web/src/stream_create_subscribers.ts@@ -6,6 +6,8 @@ import render_new_stream_users from "../templates/stream_settings/new_stream_useimport * as add_subscribers_pill from "./add_subscribers_pill.ts";import * as ListWidget from "./list_widget.ts";import type {ListWidget as ListWidgetType} from "./list_widget.ts";+import * as loading from "./loading.ts";+import * as overlays from "./overlays.ts";import * as people from "./people.ts";import {current_user} from "./state_data.ts";import * as stream_create_subscribers_data from "./stream_create_subscribers_data.ts";@@ -30,6 +32,40 @@ function add_user_ids(user_ids: number[]): void {redraw_subscriber_list();}+export async function add_subscribers_from_pills(): Promise<+ {status: "synced" | "form_closed"} | {status: "failed"; failed_stream_id: number}+> {+ // Subscriber data for a channel pill may still be fetching from+ // the server, in which case the subscribers of that channel are+ // not yet in the data set. Wait for the fetch and sync the pills'+ // user ids, so that the channel is not created without them.+ const $spinner = $("#people_to_add .add-subscriber-loading-spinner");+ loading.make_indicator($spinner, {+ height: 28, // 2em at 14px / 1em+ });+ const current_pill_widget = pill_widget;+ const result = await add_subscribers_pill.get_pill_user_ids(pill_widget);+ // Make sure the creation form was not closed, or closed and+ // rebuilt, while we were waiting for subscriber data. Closing+ // the channels overlay hides it without resetting the panes,+ // so also check that the overlay itself is still open. We don't+ // need to destroy the spinner in that case, because build_widgets+ // re-renders its container every time the form opens.+ if (+ pill_widget !== current_pill_widget ||+ !overlays.streams_open() ||+ $("#stream-creation").css("display") === "none"+ ) {+ return {status: "form_closed"};+ }+ loading.destroy_indicator($spinner);+ if (result.status === "failed") {+ return result;+ }+ add_user_ids(result.user_ids);+ return {status: "synced"};+}+function soft_remove_user_id(user_id: number): void {stream_create_subscribers_data.soft_remove_user_id(user_id);redraw_subscriber_list();@@ -53,20 +89,34 @@ function build_pill_widget({const $pill_container = $parent_container.find(".pill-container");const get_potential_subscribers = stream_create_subscribers_data.get_potential_subscribers;const get_user_groups = user_groups.get_all_realm_user_groups;- return add_subscribers_pill.create({+ const new_pill_widget = add_subscribers_pill.create({$parent_container,$pill_container,get_potential_subscribers,get_user_groups,with_add_button: false,- onPillCreateAction: add_user_ids,+ onPillCreateAction(user_ids) {+ // This runs after subscriber data is fetched, by which+ // time the form may have been closed and rebuilt with a+ // new widget. Ignore the stale widget's actions.+ if (pill_widget !== new_pill_widget) {+ return;+ }+ add_user_ids(user_ids);+ },// It is better to sync the current set of user ids in the input// instead of removing user_ids from the user_ids_set, otherwise// we'll have to have more complex logic of when to remove// a user and when not to depending upon their group, channel// and individual pills.- onPillRemoveAction: sync_user_ids,+ onPillRemoveAction(user_ids) {+ if (pill_widget !== new_pill_widget) {+ return;+ }+ sync_user_ids(user_ids);+ },});+ return new_pill_widget;}export function create_handlers($container: JQuery): void {