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 API key before downloading zuliprc files.

Sahil Batra783f97ae723e
diff --git a/web/e2e-tests/settings.test.ts b/web/e2e-tests/settings.test.ts
index 8900c72cda7..24d9bada656 100644
--- a/web/e2e-tests/settings.test.ts
+++ b/web/e2e-tests/settings.test.ts
@@ -148,6 +148,12 @@ async function test_webhook_bot_creation(page: Page): Promise<void> {
await page.waitForSelector(download_zuliprc_selector, {visible: true});
await page.click(download_zuliprc_selector);
+ // Wait for API request to be complete by checking when the loading indicator
+ // is shown and hidden.
+ const download_zuliprc_loader_selector = ".download-bot-zuliprc .button-loading-indicator";
+ await page.waitForSelector(download_zuliprc_loader_selector, {visible: true});
+ await page.waitForSelector(download_zuliprc_loader_selector, {hidden: true});
+
const zuliprc_decoded_url = await get_decoded_url_in_selector(page, zuliprc_url_selector);
assert.match(
zuliprc_decoded_url,
@@ -187,6 +193,13 @@ async function test_normal_bot_creation(page: Page): Promise<void> {
await page.waitForSelector(download_zuliprc_selector, {visible: true});
await page.click(download_zuliprc_selector);
+
+ // Wait for API request to be complete by checking when the loading indicator
+ // is shown and hidden.
+ const download_zuliprc_loader_selector = ".download-bot-zuliprc .button-loading-indicator";
+ await page.waitForSelector(download_zuliprc_loader_selector, {visible: true});
+ await page.waitForSelector(download_zuliprc_loader_selector, {hidden: true});
+
const zuliprc_decoded_url = await get_decoded_url_in_selector(page, zuliprc_url_selector);
assert.match(zuliprc_decoded_url, zuliprc_regex, "Incorrect zuliprc format for bot.");
await page.click(".micromodal .modal__close");
diff --git a/web/src/bot_helper.ts b/web/src/bot_helper.ts
index ccad2fb41f5..12d50afb75d 100644
--- a/web/src/bot_helper.ts
+++ b/web/src/bot_helper.ts
@@ -20,10 +20,10 @@ export function validate_bot_short_name(value: string): boolean {
return /^[\w!#$%&'*+/=?^`{|}~-]+(\.[\w!#$%&'*+/=?^`{|}~-]+)*$/i.test(value);
}
-export function generate_zuliprc_url(bot_id: number): string {
+export function generate_zuliprc_url(bot_id: number, api_key: string): string {
const bot = bot_data.get(bot_id);
assert(bot !== undefined);
- const data = generate_zuliprc_content(bot);
+ const data = generate_zuliprc_content({...bot, api_key});
return encode_zuliprc_as_url(data);
}
@@ -197,13 +197,19 @@ export function initialize_bot_click_handlers(): void {
});
$("body").on("click", "button.download-bot-zuliprc", function () {
- const $bot_info = $(this).closest("#bot-edit-form");
- const bot_id = Number.parseInt($bot_info.attr("data-user-id")!, 10);
- const bot_email = $bot_info.attr("data-email");
-
- // Select the <a> element by matching data-email.
- const $zuliprc_link = $(`.hidden-zuliprc-download[data-email="${bot_email}"]`);
- $zuliprc_link.attr("href", generate_zuliprc_url(bot_id));
- $zuliprc_link[0]?.click();
+ void (async () => {
+ const $bot_info = $(this).closest("#bot-edit-form");
+ const bot_id = Number.parseInt($bot_info.attr("data-user-id")!, 10);
+ const bot_email = $bot_info.attr("data-email");
+ const api_key = await fetch_bot_api_key(bot_id, $("#bot-edit-form-error"), $(this));
+ if (!api_key) {
+ return;
+ }
+
+ // Select the <a> element by matching data-email.
+ const $zuliprc_link = $(`.hidden-zuliprc-download[data-email="${bot_email}"]`);
+ $zuliprc_link.attr("href", generate_zuliprc_url(bot_id, api_key));
+ $zuliprc_link[0]?.click();
+ })();
});
}
diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.ts
index 8eda8fbd305..b16e44c3aec 100644
--- a/web/src/settings_bots.ts
+++ b/web/src/settings_bots.ts
@@ -10,6 +10,7 @@ import * as avatar from "./avatar.ts";
import * as bot_data from "./bot_data.ts";
import type {Bot} from "./bot_data.ts";
import * as bot_helper from "./bot_helper.ts";
+import * as buttons from "./buttons.ts";
import * as channel from "./channel.ts";
import {csrf_token} from "./csrf.ts";
import * as dialog_widget from "./dialog_widget.ts";
@@ -696,34 +697,58 @@ export function set_up_bots(): void {
create_status_filter_dropdown($("#admin-all-bots-list"), all_bots_section);
create_status_filter_dropdown($("#admin-your-bots-list"), your_bots_section);
- $("#download-botserverrc-file").on("click", () => {
- let content = "";
-
- for (const bot of bot_data.get_all_bots_for_current_user()) {
- if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
- const services = bot_data.get_services(bot.user_id);
- assert(services !== undefined);
- const service = services[0];
- assert(service && "token" in service);
- const bot_token = service.token;
- content += generate_botserverrc_content(bot.email, bot.api_key, bot_token);
+ $("#download-botserverrc-file").on("click", (e) => {
+ void (async () => {
+ let content = "";
+ buttons.show_button_loading_indicator($(e.currentTarget));
+ $(e.currentTarget).prop("disabled", true);
+ for (const bot of bot_data.get_all_bots_for_current_user()) {
+ if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
+ const services = bot_data.get_services(bot.user_id);
+ assert(services !== undefined);
+ const service = services[0];
+ assert(service && "token" in service);
+ const bot_token = service.token;
+ const api_key = await bot_helper.fetch_bot_api_key(
+ bot.user_id,
+ $("#admin-your-bots-list .bot-list-error"),
+ );
+ if (!api_key) {
+ buttons.hide_button_loading_indicator($(e.currentTarget));
+ $(e.currentTarget).prop("disabled", false);
+ return;
+ }
+ content += generate_botserverrc_content(bot.email, api_key, bot_token);
+ }
}
- }
-
- $("#hidden-botserverrc-download").attr(
- "href",
- "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content),
- );
- $("#hidden-botserverrc-download")[0]?.click();
+ buttons.hide_button_loading_indicator($(e.currentTarget));
+ $(e.currentTarget).prop("disabled", false);
+
+ $("#hidden-botserverrc-download").attr(
+ "href",
+ "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content),
+ );
+ $("#hidden-botserverrc-download")[0]?.click();
+ })();
});
toggle_bot_config_download_container();
- $("#admin-bot-list").on("click", ".download-bot-zuliprc-button", (e) => {
- const $row = $(e.target).closest(".user_row");
- const $zuliprc_link = $row.find(".hidden-zuliprc-download");
- const bot_id = Number.parseInt($zuliprc_link.attr("data-user-id")!, 10);
- $zuliprc_link.attr("href", bot_helper.generate_zuliprc_url(bot_id));
- $zuliprc_link[0]?.click();
+ $("#admin-bot-list").on("click", ".download-bot-zuliprc-button", function (this: HTMLElement) {
+ void (async () => {
+ const $row = $(this).closest(".user_row");
+ const $zuliprc_link = $row.find(".hidden-zuliprc-download");
+ const bot_id = Number.parseInt($zuliprc_link.attr("data-user-id")!, 10);
+ const api_key = await bot_helper.fetch_bot_api_key(
+ bot_id,
+ $row.closest(".bot-settings-section").find(".bot-list-error"),
+ $(this),
+ );
+ if (!api_key) {
+ return;
+ }
+ $zuliprc_link.attr("href", bot_helper.generate_zuliprc_url(bot_id, api_key));
+ $zuliprc_link[0]?.click();
+ })();
});
$("#admin-bot-list").on("click", ".generate-integration-url-button", (e) => {
diff --git a/web/styles/settings.css b/web/styles/settings.css
index 11340725e26..828ff824379 100644
--- a/web/styles/settings.css
+++ b/web/styles/settings.css
@@ -403,6 +403,13 @@ select.settings_select {
.config-download-text {
margin-top: 10px;
+ display: flex;
+ align-items: center;
+}
+
+.bot-list-error {
+ margin-top: 10px;
+ margin-bottom: 10px;
}
.user_role,
diff --git a/web/templates/settings/bot_list_admin.hbs b/web/templates/settings/bot_list_admin.hbs
index a4083dae827..b2973fd225d 100644
--- a/web/templates/settings/bot_list_admin.hbs
+++ b/web/templates/settings/bot_list_admin.hbs
@@ -13,6 +13,7 @@
</div>
<div class="tab-container"></div>
<div id="admin-all-bots-list" class="bot-settings-section user-or-bot-settings-section" data-bot-settings-section="all-bots">
+ <div class="bot-list-error alert"></div>
{{> bot_list
section_name="all_bots"
section_title=(t "All bots")
@@ -20,6 +21,7 @@
}}
</div>
<div id="admin-your-bots-list" class="bot-settings-section user-or-bot-settings-section" data-bot-settings-section="your-bots">
+ <div class="bot-list-error alert"></div>
<div id="botserverrc-text-container" class="config-download-text">
<span>{{t 'Download config of all active outgoing webhook bots in Zulip Botserver format.' }}</span>
<a type="submit" download="botserverrc" id= "hidden-botserverrc-download" hidden></a>
@@ -27,7 +29,7 @@
id= "download-botserverrc-file"
icon="download"
intent="brand"
- custom_classes="tippy-zulip-delayed-tooltip inline"
+ custom_classes="tippy-zulip-delayed-tooltip"
data-tippy-content=(t "Download botserverrc")
}}
</div>
diff --git a/web/tests/settings_bots.test.cjs b/web/tests/settings_bots.test.cjs
index 8aeea58c956..2a0a4b95bfc 100644
--- a/web/tests/settings_bots.test.cjs
+++ b/web/tests/settings_bots.test.cjs
@@ -6,10 +6,11 @@ const {make_realm} = require("./lib/example_realm.cjs");
const {zrequire} = require("./lib/namespace.cjs");
const {run_test} = require("./lib/test.cjs");
+const bot_api_key = "QadL788EkiottHmukyhHgePUFHREiu8b";
const bot_data_params = {
realm_bots: [
{
- api_key: "QadL788EkiottHmukyhHgePUFHREiu8b",
+ api_key: bot_api_key,
avatar_url: "",
bot_type: 1, // DEFAULT_BOT
default_all_public_streams: true,
@@ -53,7 +54,7 @@ function test(label, f) {
}
test("generate_zuliprc_url", () => {
- const url = bot_helper.generate_zuliprc_url(1);
+ const url = bot_helper.generate_zuliprc_url(1, bot_api_key);
const expected =
"data:application/octet-stream;charset=utf-8," +
encodeURIComponent(