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.

bots: Fetch bot API key before opening zuliprc and API key modal.

Sahil Batra8da98d6e5d8d
diff --git a/web/src/bot_helper.ts b/web/src/bot_helper.ts
index 6cf81274a7b..ccad2fb41f5 100644
--- a/web/src/bot_helper.ts
+++ b/web/src/bot_helper.ts
@@ -6,11 +6,14 @@ import * as z from "zod/mini";
import render_bot_api_key_details from "../templates/settings/bot_api_key_details.hbs";
import * as bot_data from "./bot_data.ts";
+import * as buttons from "./buttons.ts";
import * as channel from "./channel.ts";
import {show_copied_confirmation} from "./copied_tooltip.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t_html} from "./i18n.ts";
+import * as scroll_util from "./scroll_util.ts";
import {realm} from "./state_data.ts";
+import * as ui_report from "./ui_report.ts";
export function validate_bot_short_name(value: string): boolean {
// Adapted from Django's EmailValidator
@@ -58,6 +61,42 @@ export function generate_zuliprc_content(bot: {
);
}
+export async function fetch_bot_api_key(
+ bot_id: number,
+ $error_element: JQuery,
+ $trigger?: JQuery,
+): Promise<string | null> {
+ try {
+ if ($trigger !== undefined) {
+ buttons.show_button_loading_indicator($trigger);
+ $trigger.prop("disabled", true);
+ }
+ const raw_data = await channel.get({
+ url: `/json/bots/${bot_id}/api_key`,
+ error(xhr) {
+ ui_report.error($t_html({defaultMessage: "Failed"}), xhr, $error_element);
+ },
+ });
+
+ const data = z
+ .object({
+ api_key: z.string(),
+ msg: z.string(),
+ result: z.string(),
+ })
+ .parse(raw_data);
+
+ return data.api_key;
+ } catch {
+ return null;
+ } finally {
+ if ($trigger !== undefined) {
+ buttons.hide_button_loading_indicator($trigger);
+ $trigger.prop("disabled", false);
+ }
+ }
+}
+
function initialize_api_key_clipboard_handlers(): void {
new ClipboardJS("#copy-api-key-button", {
text(trigger) {
@@ -90,11 +129,25 @@ export function initialize_zuliprc_clipboard_handlers(): void {
});
}
-export function show_api_key_modal(bot_id: number): void {
+export async function show_api_key_modal(bot_id: number): Promise<void> {
+ const api_key = await fetch_bot_api_key(
+ bot_id,
+ $("#bot-edit-form-error"),
+ $("#bot-edit-form .show-api-key"),
+ );
+
+ if (!api_key) {
+ scroll_util.scroll_element_into_container(
+ $("#bot-edit-form-error"),
+ $("#user-profile-modal .modal__body"),
+ );
+ return;
+ }
+
const bot = bot_data.get(bot_id)!;
const modal_content_html = render_bot_api_key_details({
bot_id,
- api_key: bot.api_key,
+ api_key,
});
dialog_widget.launch({
modal_title_html: $t_html(
diff --git a/web/src/user_profile.ts b/web/src/user_profile.ts
index 186dbc8a981..8a7dd4cff2d 100644
--- a/web/src/user_profile.ts
+++ b/web/src/user_profile.ts
@@ -1124,7 +1124,9 @@ export function show_edit_bot_info_modal(user_id: number, $container: JQuery): v
e.preventDefault();
e.stopPropagation();
assert(bot !== undefined);
- bot_helper.show_api_key_modal(bot.user_id);
+ void (async () => {
+ await bot_helper.show_api_key_modal(bot.user_id);
+ })();
});
}
}