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.

bot-settings: Create bot_helper for bot settings.

Maneesh Shukla54596e76a43c
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index ae352c060bf..13fc4a9c312 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -61,6 +61,7 @@ EXEMPT_FILES = make_set(
"web/src/blueslip.ts",
"web/src/blueslip_stacktrace.ts",
"web/src/bootstrap_typeahead.ts",
+ "web/src/bot_helper.ts",
"web/src/browser_history.ts",
"web/src/buddy_list.ts",
"web/src/buttons.ts",
diff --git a/web/src/bot_helper.ts b/web/src/bot_helper.ts
new file mode 100644
index 00000000000..99ed0378643
--- /dev/null
+++ b/web/src/bot_helper.ts
@@ -0,0 +1,45 @@
+import assert from "minimalistic-assert";
+
+import * as bot_data from "./bot_data.ts";
+import {realm} from "./state_data.ts";
+
+export function generate_zuliprc_url(bot_id: number): string {
+ const bot = bot_data.get(bot_id);
+ assert(bot !== undefined);
+ const data = generate_zuliprc_content(bot);
+ return encode_zuliprc_as_url(data);
+}
+
+export function encode_zuliprc_as_url(zuliprc: string): string {
+ return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);
+}
+
+export function generate_zuliprc_content(bot: {
+ bot_type?: number;
+ user_id: number;
+ email: string;
+ api_key: string;
+}): string {
+ let token;
+ // 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;
+ }
+ 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"
+ );
+}
diff --git a/web/src/settings_account.ts b/web/src/settings_account.ts
index 6b4c65b2f0b..2ef70bf29a7 100644
--- a/web/src/settings_account.ts
+++ b/web/src/settings_account.ts
@@ -9,6 +9,7 @@ import render_settings_api_key_modal from "../templates/settings/api_key_modal.h
import render_settings_dev_env_email_access from "../templates/settings/dev_env_email_access.hbs";
import * as avatar from "./avatar.ts";
+import * as bot_helper from "./bot_helper.ts";
import * as channel from "./channel.ts";
import * as common from "./common.ts";
import {csrf_token} from "./csrf.ts";
@@ -21,7 +22,6 @@ import * as modals from "./modals.ts";
import * as overlays from "./overlays.ts";
import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
-import * as settings_bots from "./settings_bots.ts";
import * as settings_components from "./settings_components.ts";
import * as settings_config from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
@@ -356,8 +356,8 @@ export function set_up(): void {
email: current_user.delivery_email,
api_key: $("#api_key_value").text(),
};
- const data = settings_bots.generate_zuliprc_content(bot_object);
- $(this).attr("href", settings_bots.encode_zuliprc_as_url(data));
+ const data = bot_helper.generate_zuliprc_content(bot_object);
+ $(this).attr("href", bot_helper.encode_zuliprc_as_url(data));
});
$("#api_key_modal [data-micromodal-close]").on("click", () => {
diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.ts
index e321d3852ca..bcd6d065465 100644
--- a/web/src/settings_bots.ts
+++ b/web/src/settings_bots.ts
@@ -9,6 +9,7 @@ import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs"
import * as avatar from "./avatar.ts";
import * as bot_data from "./bot_data.ts";
+import * as bot_helper from "./bot_helper.ts";
import * as channel from "./channel.ts";
import * as components from "./components.ts";
import {show_copied_confirmation} from "./copied_tooltip.ts";
@@ -100,47 +101,6 @@ export function render_bots(): void {
list_widget.render_empty_list_message_if_needed($("#inactive_bots_list"));
}
-export function generate_zuliprc_url(bot_id: number): string {
- const bot = bot_data.get(bot_id);
- assert(bot !== undefined);
- const data = generate_zuliprc_content(bot);
- return encode_zuliprc_as_url(data);
-}
-
-export function encode_zuliprc_as_url(zuliprc: string): string {
- return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);
-}
-
-export function generate_zuliprc_content(bot: {
- bot_type?: number;
- user_id: number;
- email: string;
- api_key: string;
-}): string {
- let token;
- // 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;
- }
- 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"
- );
-}
-
export function generate_botserverrc_content(
email: string,
api_key: string,
@@ -483,7 +443,7 @@ export function set_up(): void {
$("#active_bots_list").on("click", "a.download_bot_zuliprc", function () {
const $bot_info = $(this).closest(".bot-information-box").find(".bot-card-info");
const bot_id = Number.parseInt($bot_info.attr("data-user-id")!, 10);
- $(this).attr("href", generate_zuliprc_url(bot_id));
+ $(this).attr("href", bot_helper.generate_zuliprc_url(bot_id));
});
$("#active_bots_list").on("click", "button.open_bots_subscribed_streams", function (e) {
@@ -507,7 +467,7 @@ export function set_up(): void {
const bot_id = Number.parseInt($bot_info.attr("data-user-id")!, 10);
const bot = bot_data.get(bot_id);
assert(bot !== undefined);
- const data = generate_zuliprc_content(bot);
+ const data = bot_helper.generate_zuliprc_content(bot);
return data;
},
});
diff --git a/web/tests/settings_bots.test.cjs b/web/tests/settings_bots.test.cjs
index 1c29e670928..8aeea58c956 100644
--- a/web/tests/settings_bots.test.cjs
+++ b/web/tests/settings_bots.test.cjs
@@ -28,6 +28,7 @@ const bot_data_params = {
};
const bot_data = zrequire("bot_data");
+const bot_helper = zrequire("bot_helper");
const settings_bots = zrequire("settings_bots");
const {set_current_user, set_realm} = zrequire("state_data");
@@ -52,7 +53,7 @@ function test(label, f) {
}
test("generate_zuliprc_url", () => {
- const url = settings_bots.generate_zuliprc_url(1);
+ const url = bot_helper.generate_zuliprc_url(1);
const expected =
"data:application/octet-stream;charset=utf-8," +
encodeURIComponent(
@@ -66,7 +67,7 @@ test("generate_zuliprc_url", () => {
test("generate_zuliprc_content", () => {
const bot_user = bot_data.get(1);
- const content = settings_bots.generate_zuliprc_content(bot_user);
+ const content = bot_helper.generate_zuliprc_content(bot_user);
const expected =
"[api]\[email protected]\n" +
"key=QadL788EkiottHmukyhHgePUFHREiu8b\n" +