Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings_bots: Refactor to extract a generate_bot_config_file_content.
Niloth Pfad617813a20
diff --git a/web/src/bot_helper.ts b/web/src/bot_helper.tsindex 0290fd36ab0..b71c7b68b9c 100644--- a/web/src/bot_helper.ts+++ b/web/src/bot_helper.ts@@ -32,34 +32,45 @@ export function encode_zuliprc_as_url(zuliprc: string): string {return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);}+export function get_outgoing_webhook_token(bot_user_id: number): string {+ const services = bot_data.get_services(bot_user_id);+ assert(services !== undefined);+ const service = services[0];+ assert(service && "token" in service);+ return service.token;+}++export function generate_bot_config_file_content(+ heading: string,+ email: string,+ api_key: string,+ token?: string,+): string {+ const body = `+${heading}+email=${email}+key=${api_key}+site=${realm.realm_url}+${token !== undefined ? `token=${token}` : ""}+`.trim();++ // Some tools would not work in files without a trailing new line.+ return `${body}\n`;+}+export function generate_zuliprc_content(bot: {bot_type?: number;user_id: number;email: string;api_key: string;}): string {- let token;+ let token: string | undefined;// For outgoing webhooks, include the token in the zuliprc.// It's needed for authenticating to the Botserver.if (bot.bot_type === 3) {- const services = bot_data.get_services(bot.user_id);- assert(services !== undefined);- const service = services[0];- assert(service && "token" in service);- token = service.token;+ token = get_outgoing_webhook_token(bot.user_id);}- return (- "[api]" +- "\nemail=" +- bot.email +- "\nkey=" +- bot.api_key +- "\nsite=" +- realm.realm_url +- (token === undefined ? "" : "\ntoken=" + token) +- // Some tools would not work in files without a trailing new line.- "\n"- );+ return generate_bot_config_file_content("[api]", bot.email, bot.api_key, token);}export async function fetch_bot_api_key(diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.tsindex 74d2bc4419e..41b88b58e2c 100644--- a/web/src/settings_bots.ts+++ b/web/src/settings_bots.ts@@ -160,18 +160,7 @@ export function generate_botserverrc_content(api_key: string,token: string,): string {- return (- "[]" +- "\nemail=" +- email +- "\nkey=" +- api_key +- "\nsite=" +- realm.realm_url +- "\ntoken=" +- token +- "\n"- );+ return bot_helper.generate_bot_config_file_content("[]", email, api_key, token);}export function can_create_new_bots(): boolean {@@ -750,11 +739,7 @@ function set_up_bot_handlers($container: JQuery): void {$(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 bot_token = bot_helper.get_outgoing_webhook_token(bot.user_id);const api_key = await bot_helper.fetch_bot_api_key(bot.user_id,$("#admin-your-bots-list .bot-list-error"),