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.

settings_bots: Move show_edit_bot_info_modal to user_profile.js.

Sahil Batraf0a265c363e5
diff --git a/web/src/settings_bots.js b/web/src/settings_bots.js
index 56ac2d51fb6..8f757551084 100644
--- a/web/src/settings_bots.js
+++ b/web/src/settings_bots.js
@@ -5,9 +5,6 @@ import render_settings_deactivation_bot_modal from "../templates/confirm_dialog/
import render_add_new_bot_form from "../templates/settings/add_new_bot_form.hbs";
import render_bot_avatar_row from "../templates/settings/bot_avatar_row.hbs";
import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs";
-import render_edit_bot_form from "../templates/settings/edit_bot_form.hbs";
-import render_settings_edit_embedded_bot_service from "../templates/settings/edit_embedded_bot_service.hbs";
-import render_settings_edit_outgoing_webhook_service from "../templates/settings/edit_outgoing_webhook_service.hbs";
import render_generate_integration_url_modal from "../templates/settings/generate_integration_url_modal.hbs";
import * as avatar from "./avatar";
@@ -20,7 +17,6 @@ import * as dropdown_widget from "./dropdown_widget";
import {$t, $t_html} from "./i18n";
import {page_params} from "./page_params";
import * as people from "./people";
-import * as settings_config from "./settings_config";
import * as settings_data from "./settings_data";
import * as settings_users from "./settings_users";
import * as stream_data from "./stream_data";
@@ -30,7 +26,6 @@ import * as user_profile from "./user_profile";
const INCOMING_WEBHOOK_BOT_TYPE = 2;
const OUTGOING_WEBHOOK_BOT_TYPE = "3";
const EMBEDDED_BOT_TYPE = "4";
-export let bot_owner_dropdown_widget;
const focus_tab = {
active_bots_tab() {
@@ -339,183 +334,6 @@ export function confirm_bot_deactivation(bot_id, handle_confirm, loading_spinner
});
}
-export function show_edit_bot_info_modal(user_id, $container) {
- const bot = people.maybe_get_user_by_id(user_id);
- const owner_id = bot_data.get(user_id).owner_id;
- const owner_full_name = people.get_full_name(owner_id);
-
- if (!bot || !bot_data.get(user_id)) {
- return;
- }
-
- const html_body = render_edit_bot_form({
- user_id,
- email: bot.email,
- full_name: bot.full_name,
- user_role_values: settings_config.user_role_values,
- disable_role_dropdown: !page_params.is_admin || (bot.is_owner && !page_params.is_owner),
- bot_avatar_url: bot.avatar_url,
- owner_full_name,
- current_bot_owner: bot.bot_owner_id,
- is_incoming_webhook_bot: bot.bot_type === INCOMING_WEBHOOK_BOT_TYPE,
- });
- $container.append(html_body);
- let avatar_widget;
-
- const bot_type = bot.bot_type.toString();
- const service = bot_data.get_services(bot.user_id)[0];
- edit_bot_post_render();
- $("#user-profile-modal").on("click", ".dialog_submit_button", () => {
- const role = Number.parseInt($("#bot-role-select").val().trim(), 10);
- const $full_name = $("#bot-edit-form").find("input[name='full_name']");
- const url = "/json/bots/" + encodeURIComponent(bot.user_id);
-
- const formData = new FormData();
- formData.append("csrfmiddlewaretoken", csrf_token);
- formData.append("full_name", $full_name.val());
- formData.append("role", JSON.stringify(role));
- const new_bot_owner_id = bot_owner_dropdown_widget.value();
- if (new_bot_owner_id) {
- formData.append("bot_owner_id", new_bot_owner_id);
- }
-
- if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
- const service_payload_url = $("#edit_service_base_url").val();
- const service_interface = $("#edit_service_interface").val();
- formData.append("service_payload_url", JSON.stringify(service_payload_url));
- formData.append("service_interface", service_interface);
- } else if (bot_type === EMBEDDED_BOT_TYPE && service !== undefined) {
- const config_data = {};
- $("#config_edit_inputbox input").each(function () {
- config_data[$(this).attr("name")] = $(this).val();
- });
- formData.append("config_data", JSON.stringify(config_data));
- }
-
- const $file_input = $("#bot-edit-form").find(".edit_bot_avatar_file_input");
- for (const [i, file] of Array.prototype.entries.call($file_input[0].files)) {
- formData.append("file-" + i, file);
- }
-
- const $submit_btn = $("#user-profile-modal .dialog_submit_button");
- const $cancel_btn = $("#user-profile-modal .dialog_exit_button");
- settings_users.show_button_spinner($submit_btn);
- $cancel_btn.prop("disabled", true);
-
- channel.patch({
- url,
- data: formData,
- processData: false,
- contentType: false,
- success() {
- avatar_widget.clear();
- user_profile.hide_user_profile();
- },
- error(xhr) {
- ui_report.error(
- $t_html({defaultMessage: "Failed"}),
- xhr,
- $("#bot-edit-form-error"),
- );
- // Scrolling modal to top, to make error visible to user.
- $("#bot-edit-form")
- .closest(".simplebar-content-wrapper")
- .animate({scrollTop: 0}, "fast");
- settings_users.hide_button_spinner($submit_btn);
- $cancel_btn.prop("disabled", false);
- },
- });
- });
-
- function edit_bot_post_render() {
- $("#edit_bot_modal .dialog_submit_button").prop("disabled", true);
-
- function get_options() {
- const user_ids = people.get_realm_active_human_user_ids();
- return user_ids.map((user_id) => ({
- name: people.get_full_name(user_id),
- unique_id: user_id,
- }));
- }
-
- function item_click_callback(event, dropdown) {
- bot_owner_dropdown_widget.render();
- // Let dialog_widget know that there was a change in value.
- $(bot_owner_dropdown_widget.widget_id).trigger("input");
- dropdown.hide();
- event.stopPropagation();
- event.preventDefault();
- }
-
- bot_owner_dropdown_widget = new dropdown_widget.DropdownWidget({
- widget_name: "edit_bot_owner",
- get_options,
- item_click_callback,
- $events_container: $("#bot-edit-form"),
- tippy_props: {
- placement: "bottom-start",
- },
- default_id: owner_id,
- unique_id_type: dropdown_widget.DATA_TYPES.NUMBER,
- });
- bot_owner_dropdown_widget.setup();
-
- $("#bot-role-select").val(bot.role);
- if (!page_params.is_owner) {
- $("#bot-role-select")
- .find(`option[value="${CSS.escape(settings_config.user_role_values.owner.code)}"]`)
- .hide();
- }
-
- avatar_widget = avatar.build_bot_edit_widget($("#bot-edit-form"));
-
- if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
- $("#service_data").append(
- render_settings_edit_outgoing_webhook_service({
- service,
- }),
- );
- $("#edit_service_interface").val(service.interface);
- }
- if (bot_type === EMBEDDED_BOT_TYPE) {
- $("#service_data").append(
- render_settings_edit_embedded_bot_service({
- service,
- }),
- );
- }
-
- // Hide the avatar if the user has uploaded an image
- $("#bot-edit-form").on("input", ".edit_bot_avatar_file_input", () => {
- $("#current_bot_avatar_image").hide();
- });
-
- // Show the avatar if the user has cleared the image
- $("#bot-edit-form").on("click", ".edit_bot_avatar_clear_button", () => {
- $("#current_bot_avatar_image").show();
- $(".edit_bot_avatar_file_input").trigger("input");
- });
-
- $("#bot-edit-form").on("click", ".deactivate_bot_button", (e) => {
- e.preventDefault();
- e.stopPropagation();
- const bot_id = $("#bot-edit-form").data("user-id");
- function handle_confirm() {
- const url = "/json/bots/" + encodeURIComponent(bot_id);
- dialog_widget.submit_api_request(channel.del, url);
- }
- confirm_bot_deactivation(bot_id, handle_confirm, true);
- });
-
- $("#bot-edit-form").on("click", ".generate_url_for_integration", (e) => {
- e.preventDefault();
- e.stopPropagation();
- const current_bot_data = bot_data.get(bot.user_id);
- show_generate_integration_url_modal(current_bot_data.api_key);
- });
- }
-}
-
export function show_generate_integration_url_modal(api_key) {
const default_url_message = $t_html({defaultMessage: "Integration URL will appear here."});
const streams = stream_data.subscribed_subs();
diff --git a/web/src/user_profile.js b/web/src/user_profile.js
index af6c6f76407..a86ce991393 100644
--- a/web/src/user_profile.js
+++ b/web/src/user_profile.js
@@ -1,14 +1,21 @@
import {parseISO} from "date-fns";
import $ from "jquery";
+import render_edit_bot_form from "../templates/settings/edit_bot_form.hbs";
+import render_settings_edit_embedded_bot_service from "../templates/settings/edit_embedded_bot_service.hbs";
+import render_settings_edit_outgoing_webhook_service from "../templates/settings/edit_outgoing_webhook_service.hbs";
import render_user_group_list_item from "../templates/user_group_list_item.hbs";
import render_user_profile_modal from "../templates/user_profile_modal.hbs";
import render_user_stream_list_item from "../templates/user_stream_list_item.hbs";
+import * as avatar from "./avatar";
+import * as bot_data from "./bot_data";
import * as browser_history from "./browser_history";
import * as buddy_data from "./buddy_data";
import * as channel from "./channel";
import * as components from "./components";
+import {csrf_token} from "./csrf";
+import * as dialog_widget from "./dialog_widget";
import * as dropdown_widget from "./dropdown_widget";
import * as hash_util from "./hash_util";
import {$t, $t_html} from "./i18n";
@@ -19,6 +26,7 @@ import * as people from "./people";
import * as popovers from "./popovers";
import * as settings_account from "./settings_account";
import * as settings_bots from "./settings_bots";
+import * as settings_config from "./settings_config";
import * as settings_data from "./settings_data";
import * as settings_profile_fields from "./settings_profile_fields";
import * as settings_users from "./settings_users";
@@ -35,6 +43,11 @@ import * as util from "./util";
let user_streams_list_widget;
let user_profile_subscribe_widget;
let toggler;
+let bot_owner_dropdown_widget;
+
+const INCOMING_WEBHOOK_BOT_TYPE = 2;
+const OUTGOING_WEBHOOK_BOT_TYPE = "3";
+const EMBEDDED_BOT_TYPE = "4";
function compare_by_name(a, b) {
return util.strcmp(a.name, b.name);
@@ -201,7 +214,7 @@ function render_manage_profile_content(user) {
const $container = $("#manage-profile-tab");
$container.empty();
if (user.is_bot) {
- settings_bots.show_edit_bot_info_modal(user.user_id, $container);
+ show_edit_bot_info_modal(user.user_id, $container);
} else {
settings_users.show_edit_user_info_modal(user.user_id, $container);
}
@@ -422,6 +435,183 @@ function handle_remove_stream_subscription(target_user_id, sub, success, failure
}
}
+export function show_edit_bot_info_modal(user_id, $container) {
+ const bot = people.maybe_get_user_by_id(user_id);
+ const owner_id = bot_data.get(user_id).owner_id;
+ const owner_full_name = people.get_full_name(owner_id);
+
+ if (!bot || !bot_data.get(user_id)) {
+ return;
+ }
+
+ const html_body = render_edit_bot_form({
+ user_id,
+ email: bot.email,
+ full_name: bot.full_name,
+ user_role_values: settings_config.user_role_values,
+ disable_role_dropdown: !page_params.is_admin || (bot.is_owner && !page_params.is_owner),
+ bot_avatar_url: bot.avatar_url,
+ owner_full_name,
+ current_bot_owner: bot.bot_owner_id,
+ is_incoming_webhook_bot: bot.bot_type === INCOMING_WEBHOOK_BOT_TYPE,
+ });
+ $container.append(html_body);
+ let avatar_widget;
+
+ const bot_type = bot.bot_type.toString();
+ const service = bot_data.get_services(bot.user_id)[0];
+ edit_bot_post_render();
+ $("#user-profile-modal").on("click", ".dialog_submit_button", () => {
+ const role = Number.parseInt($("#bot-role-select").val().trim(), 10);
+ const $full_name = $("#bot-edit-form").find("input[name='full_name']");
+ const url = "/json/bots/" + encodeURIComponent(bot.user_id);
+
+ const formData = new FormData();
+ formData.append("csrfmiddlewaretoken", csrf_token);
+ formData.append("full_name", $full_name.val());
+ formData.append("role", JSON.stringify(role));
+ const new_bot_owner_id = bot_owner_dropdown_widget.value();
+ if (new_bot_owner_id) {
+ formData.append("bot_owner_id", new_bot_owner_id);
+ }
+
+ if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
+ const service_payload_url = $("#edit_service_base_url").val();
+ const service_interface = $("#edit_service_interface").val();
+ formData.append("service_payload_url", JSON.stringify(service_payload_url));
+ formData.append("service_interface", service_interface);
+ } else if (bot_type === EMBEDDED_BOT_TYPE && service !== undefined) {
+ const config_data = {};
+ $("#config_edit_inputbox input").each(function () {
+ config_data[$(this).attr("name")] = $(this).val();
+ });
+ formData.append("config_data", JSON.stringify(config_data));
+ }
+
+ const $file_input = $("#bot-edit-form").find(".edit_bot_avatar_file_input");
+ for (const [i, file] of Array.prototype.entries.call($file_input[0].files)) {
+ formData.append("file-" + i, file);
+ }
+
+ const $submit_btn = $("#user-profile-modal .dialog_submit_button");
+ const $cancel_btn = $("#user-profile-modal .dialog_exit_button");
+ settings_users.show_button_spinner($submit_btn);
+ $cancel_btn.prop("disabled", true);
+
+ channel.patch({
+ url,
+ data: formData,
+ processData: false,
+ contentType: false,
+ success() {
+ avatar_widget.clear();
+ hide_user_profile();
+ },
+ error(xhr) {
+ ui_report.error(
+ $t_html({defaultMessage: "Failed"}),
+ xhr,
+ $("#bot-edit-form-error"),
+ );
+ // Scrolling modal to top, to make error visible to user.
+ $("#bot-edit-form")
+ .closest(".simplebar-content-wrapper")
+ .animate({scrollTop: 0}, "fast");
+ settings_users.hide_button_spinner($submit_btn);
+ $cancel_btn.prop("disabled", false);
+ },
+ });
+ });
+
+ function edit_bot_post_render() {
+ $("#edit_bot_modal .dialog_submit_button").prop("disabled", true);
+
+ function get_options() {
+ const user_ids = people.get_realm_active_human_user_ids();
+ return user_ids.map((user_id) => ({
+ name: people.get_full_name(user_id),
+ unique_id: user_id,
+ }));
+ }
+
+ function item_click_callback(event, dropdown) {
+ bot_owner_dropdown_widget.render();
+ // Let dialog_widget know that there was a change in value.
+ $(bot_owner_dropdown_widget.widget_id).trigger("input");
+ dropdown.hide();
+ event.stopPropagation();
+ event.preventDefault();
+ }
+
+ bot_owner_dropdown_widget = new dropdown_widget.DropdownWidget({
+ widget_name: "edit_bot_owner",
+ get_options,
+ item_click_callback,
+ $events_container: $("#bot-edit-form"),
+ tippy_props: {
+ placement: "bottom-start",
+ },
+ default_id: owner_id,
+ unique_id_type: dropdown_widget.DATA_TYPES.NUMBER,
+ });
+ bot_owner_dropdown_widget.setup();
+
+ $("#bot-role-select").val(bot.role);
+ if (!page_params.is_owner) {
+ $("#bot-role-select")
+ .find(`option[value="${CSS.escape(settings_config.user_role_values.owner.code)}"]`)
+ .hide();
+ }
+
+ avatar_widget = avatar.build_bot_edit_widget($("#bot-edit-form"));
+
+ if (bot_type === OUTGOING_WEBHOOK_BOT_TYPE) {
+ $("#service_data").append(
+ render_settings_edit_outgoing_webhook_service({
+ service,
+ }),
+ );
+ $("#edit_service_interface").val(service.interface);
+ }
+ if (bot_type === EMBEDDED_BOT_TYPE) {
+ $("#service_data").append(
+ render_settings_edit_embedded_bot_service({
+ service,
+ }),
+ );
+ }
+
+ // Hide the avatar if the user has uploaded an image
+ $("#bot-edit-form").on("input", ".edit_bot_avatar_file_input", () => {
+ $("#current_bot_avatar_image").hide();
+ });
+
+ // Show the avatar if the user has cleared the image
+ $("#bot-edit-form").on("click", ".edit_bot_avatar_clear_button", () => {
+ $("#current_bot_avatar_image").show();
+ $(".edit_bot_avatar_file_input").trigger("input");
+ });
+
+ $("#bot-edit-form").on("click", ".deactivate_bot_button", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ const bot_id = $("#bot-edit-form").data("user-id");
+ function handle_confirm() {
+ const url = "/json/bots/" + encodeURIComponent(bot_id);
+ dialog_widget.submit_api_request(channel.del, url);
+ }
+ settings_bots.confirm_bot_deactivation(bot_id, handle_confirm, true);
+ });
+
+ $("#bot-edit-form").on("click", ".generate_url_for_integration", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ const current_bot_data = bot_data.get(bot.user_id);
+ settings_bots.show_generate_integration_url_modal(current_bot_data.api_key);
+ });
+ }
+}
+
export function register_click_handlers() {
$("body").on("click", "#user-profile-modal .add-subscription-button", (e) => {
e.preventDefault();