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: Allow downloading bot config from admin bot panel.

Maneesh Shukla825bf2cffa92
diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js
index b5ec21f43b2..31b7d5bd9f5 100644
--- a/web/src/server_events_dispatch.js
+++ b/web/src/server_events_dispatch.js
@@ -497,18 +497,24 @@ export function dispatch_normal_event(event) {
settings_bots.render_bots();
if (event.bot.owner_id === current_user.user_id) {
settings_users.redraw_your_bots_list();
+ settings_users.toggle_bot_config_download_container();
}
break;
case "delete":
bot_data.del(event.bot.user_id);
settings_bots.render_bots();
settings_users.redraw_your_bots_list();
+ settings_users.toggle_bot_config_download_container();
break;
case "update":
bot_data.update(event.bot.user_id, event.bot);
settings_bots.render_bots();
if ("owner_id" in event.bot) {
settings_users.redraw_your_bots_list();
+ settings_users.toggle_bot_config_download_container();
+ }
+ if ("is_active" in event.bot) {
+ settings_users.toggle_bot_config_download_container();
}
break;
default:
diff --git a/web/src/settings_users.ts b/web/src/settings_users.ts
index f1bb9c5ccc8..a473d5d32b9 100644
--- a/web/src/settings_users.ts
+++ b/web/src/settings_users.ts
@@ -6,6 +6,7 @@ import render_settings_user_list_row from "../templates/settings/settings_user_l
import {compute_active_status, post_presence_response_schema} from "./activity.ts";
import * as bot_data from "./bot_data.ts";
+import type {Bot} from "./bot_data.ts";
import * as browser_history from "./browser_history.ts";
import * as channel from "./channel.ts";
import * as dialog_widget from "./dialog_widget.ts";
@@ -30,6 +31,8 @@ import * as user_profile from "./user_profile.ts";
import * as user_sort from "./user_sort.ts";
import * as util from "./util.ts";
+const OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;
+
export const active_user_list_dropdown_widget_name = "active_user_list_select_user_role";
export const deactivated_user_list_dropdown_widget_name = "deactivated_user_list_select_user_role";
export const all_bots_list_dropdown_widget_name = "all_bots_list_select_bot_status";
@@ -742,6 +745,14 @@ function deactivated_create_table(deactivated_users: number[]): void {
$("#admin_deactivated_users_table").show();
}
+export function toggle_bot_config_download_container(): void {
+ const bots = bot_data.get_all_bots_for_current_user().filter((elem: Bot) => {
+ const is_active = people.is_person_active(elem.user_id);
+ return elem.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT && is_active;
+ });
+ $("#botserverrc-text-container").toggle(bots.length > 0);
+}
+
export function update_bot_data(bot_user_id: number): void {
if (all_bots_section.list_widget) {
all_bots_section.list_widget.render_item(bot_info(bot_user_id));
@@ -1000,6 +1011,32 @@ 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 += settings_bots.generate_botserverrc_content(
+ bot.email,
+ bot.api_key,
+ bot_token,
+ );
+ }
+ }
+
+ $("#hidden-botserverrc-download").attr(
+ "href",
+ "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content),
+ );
+ $("#hidden-botserverrc-download")[0]?.click();
+ });
+ toggle_bot_config_download_container();
}
type FetchPresenceUserSettingParams = {
diff --git a/web/styles/settings.css b/web/styles/settings.css
index 6635fe712df..18b426fd43b 100644
--- a/web/styles/settings.css
+++ b/web/styles/settings.css
@@ -362,6 +362,10 @@ select.settings_select {
}
}
+.config-download-text {
+ margin-top: 10px;
+}
+
.user_role,
.profile_field_type,
.bot_type,
diff --git a/web/templates/settings/bot_list_admin.hbs b/web/templates/settings/bot_list_admin.hbs
index 6006398543b..77180c5920d 100644
--- a/web/templates/settings/bot_list_admin.hbs
+++ b/web/templates/settings/bot_list_admin.hbs
@@ -20,6 +20,18 @@
}}
</div>
<div id="admin-your-bots-list" class="bot-settings-section user-or-bot-settings-section" data-bot-settings-section="your-bots">
+ <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>
+ {{> ../components/icon_button
+ id= "download-botserverrc-file"
+ icon="download"
+ intent="brand"
+ custom_classes="tippy-zulip-delayed-tooltip inline"
+ data-tippy-content=(t "Download botserverrc")
+ }}
+ </div>
+
{{> bot_list
section_name="your_bots"
section_title=(t "Your bots")
diff --git a/web/tests/dispatch.test.cjs b/web/tests/dispatch.test.cjs
index ba3633b2dcd..3f608b7c53f 100644
--- a/web/tests/dispatch.test.cjs
+++ b/web/tests/dispatch.test.cjs
@@ -840,6 +840,7 @@ run_test("realm_bot add", ({override}) => {
override(bot_data, "add", bot_stub.f);
override(settings_bots, "render_bots", noop);
override(settings_users, "redraw_your_bots_list", noop);
+ override(settings_users, "toggle_bot_config_download_container", noop);
dispatch(event);
@@ -854,6 +855,7 @@ run_test("realm_bot delete", ({override}) => {
override(bot_data, "del", bot_stub.f);
override(settings_bots, "render_bots", noop);
override(settings_users, "redraw_your_bots_list", noop);
+ override(settings_users, "toggle_bot_config_download_container", noop);
dispatch(event);
assert.equal(bot_stub.num_calls, 1);
@@ -876,14 +878,32 @@ run_test("realm_bot update", ({override}) => {
bot_stub = make_stub();
override(bot_data, "update", bot_stub.f);
+ let toggle_download_container_stub = make_stub();
+ override(
+ settings_users,
+ "toggle_bot_config_download_container",
+ toggle_download_container_stub.f,
+ );
event = event_fixtures.realm_bot__update_owner;
override(settings_users, "redraw_your_bots_list", noop);
dispatch(event);
+ assert.equal(toggle_download_container_stub.num_calls, 1);
assert.equal(bot_stub.num_calls, 1);
args = bot_stub.get_args("user_id", "bot");
assert_same(args.user_id, event.bot.user_id);
assert_same(args.bot, event.bot);
+
+ toggle_download_container_stub = make_stub();
+ override(
+ settings_users,
+ "toggle_bot_config_download_container",
+ toggle_download_container_stub.f,
+ );
+
+ event = event_fixtures.realm_bot__update_is_active;
+ dispatch(event);
+ assert.equal(toggle_download_container_stub.num_calls, 1);
});
run_test("realm_emoji", ({override}) => {
diff --git a/web/tests/lib/events.cjs b/web/tests/lib/events.cjs
index 5ef8d79f665..a4b90336c5b 100644
--- a/web/tests/lib/events.cjs
+++ b/web/tests/lib/events.cjs
@@ -550,6 +550,15 @@ exports.fixtures = {
},
},
+ realm_bot__update_is_active: {
+ type: "realm_bot",
+ op: "update",
+ bot: {
+ user_id: 4321,
+ is_active: false,
+ },
+ },
+
realm_bot__update_owner: {
type: "realm_bot",
op: "update",