js: Convert static/js/settings_bots.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 16:19:16 -08:00 committed by Tim Abbott
parent 7a1ce36d4f
commit c66d616752
13 changed files with 76 additions and 72 deletions

View File

@ -156,7 +156,6 @@
"recent_topics": false, "recent_topics": false,
"search_pill_widget": false, "search_pill_widget": false,
"settings": false, "settings": false,
"settings_bots": false,
"settings_emoji": false, "settings_emoji": false,
"settings_exports": false, "settings_exports": false,
"settings_linkifiers": false, "settings_linkifiers": false,

View File

@ -2,14 +2,19 @@
const {strict: assert} = require("assert"); const {strict: assert} = require("assert");
const {set_global, zrequire} = require("../zjsunit/namespace"); const rewiremock = require("rewiremock/node");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test"); const {run_test} = require("../zjsunit/test");
const _settings_bots = { const _settings_bots = {
__esModule: true,
render_bots: () => {}, render_bots: () => {},
}; };
set_global("settings_bots", _settings_bots); rewiremock("../../static/js/settings_bots").with(_settings_bots);
rewiremock.enable();
const bot_data = zrequire("bot_data"); const bot_data = zrequire("bot_data");
const people = zrequire("people"); const people = zrequire("people");
@ -171,3 +176,4 @@ run_test("test_basics", () => {
assert.equal(bots[1].email, "bot2@zulip.com"); assert.equal(bots[1].email, "bot2@zulip.com");
})(); })();
}); });
rewiremock.disable();

View File

@ -57,7 +57,8 @@ const scroll_bar = {__esModule: true};
rewiremock("../../static/js/scroll_bar").with(scroll_bar); rewiremock("../../static/js/scroll_bar").with(scroll_bar);
const settings_account = {__esModule: true}; const settings_account = {__esModule: true};
rewiremock("../../static/js/settings_account").with(settings_account); rewiremock("../../static/js/settings_account").with(settings_account);
const settings_bots = set_global("settings_bots", {}); const settings_bots = {__esModule: true};
rewiremock("../../static/js/settings_bots").with(settings_bots);
const settings_display = {__esModule: true}; const settings_display = {__esModule: true};
rewiremock("../../static/js/settings_display").with(settings_display); rewiremock("../../static/js/settings_display").with(settings_display);
const settings_emoji = set_global("settings_emoji", {}); const settings_emoji = set_global("settings_emoji", {});

View File

@ -4,6 +4,7 @@ const render_admin_tab = require("../templates/admin_tab.hbs");
const render_settings_organization_settings_tip = require("../templates/settings/organization_settings_tip.hbs"); const render_settings_organization_settings_tip = require("../templates/settings/organization_settings_tip.hbs");
const overlays = require("./overlays"); const overlays = require("./overlays");
const settings_bots = require("./settings_bots");
const settings_config = require("./settings_config"); const settings_config = require("./settings_config");
const settings_data = require("./settings_data"); const settings_data = require("./settings_data");
const settings_panel_menu = require("./settings_panel_menu"); const settings_panel_menu = require("./settings_panel_menu");

View File

@ -3,6 +3,7 @@
const _ = require("lodash"); const _ = require("lodash");
const people = require("./people"); const people = require("./people");
const settings_bots = require("./settings_bots");
const bots = new Map(); const bots = new Map();

View File

@ -35,7 +35,6 @@ import "../server_events";
import "../zulip"; import "../zulip";
import "../templates"; import "../templates";
import "../dropdown_list_widget"; import "../dropdown_list_widget";
import "../settings_bots";
import "../settings_sections"; import "../settings_sections";
import "../settings_emoji"; import "../settings_emoji";
import "../settings_exports"; import "../settings_exports";

View File

@ -28,7 +28,6 @@ declare let reactions: any;
declare let recent_topics: any; declare let recent_topics: any;
declare let search_pill_widget: any; declare let search_pill_widget: any;
declare let settings: any; declare let settings: any;
declare let settings_bots: any;
declare let settings_emoji: any; declare let settings_emoji: any;
declare let settings_exports: any; declare let settings_exports: any;
declare let settings_invites: any; declare let settings_invites: any;

View File

@ -22,6 +22,7 @@ import * as realm_logo from "./realm_logo";
import * as reload from "./reload"; import * as reload from "./reload";
import * as scroll_bar from "./scroll_bar"; import * as scroll_bar from "./scroll_bar";
import * as settings_account from "./settings_account"; import * as settings_account from "./settings_account";
import * as settings_bots from "./settings_bots";
import * as settings_config from "./settings_config"; import * as settings_config from "./settings_config";
import * as settings_display from "./settings_display"; import * as settings_display from "./settings_display";
import * as settings_notifications from "./settings_notifications"; import * as settings_notifications from "./settings_notifications";

View File

@ -6,6 +6,7 @@ const render_settings_tab = require("../templates/settings_tab.hbs");
const overlays = require("./overlays"); const overlays = require("./overlays");
const people = require("./people"); const people = require("./people");
const settings_account = require("./settings_account"); const settings_account = require("./settings_account");
const settings_bots = require("./settings_bots");
const settings_config = require("./settings_config"); const settings_config = require("./settings_config");
const settings_notifications = require("./settings_notifications"); const settings_notifications = require("./settings_notifications");
const settings_panel_menu = require("./settings_panel_menu"); const settings_panel_menu = require("./settings_panel_menu");

View File

@ -11,6 +11,7 @@ import * as overlays from "./overlays";
import * as people from "./people"; import * as people from "./people";
import * as pill_typeahead from "./pill_typeahead"; import * as pill_typeahead from "./pill_typeahead";
import * as popovers from "./popovers"; import * as popovers from "./popovers";
import * as settings_bots from "./settings_bots";
import * as settings_ui from "./settings_ui"; import * as settings_ui from "./settings_ui";
import * as setup from "./setup"; import * as setup from "./setup";
import * as ui_report from "./ui_report"; import * as ui_report from "./ui_report";

View File

@ -1,23 +1,21 @@
"use strict"; import ClipboardJS from "clipboard";
const ClipboardJS = require("clipboard"); import render_bot_avatar_row from "../templates/bot_avatar_row.hbs";
import render_edit_bot from "../templates/edit_bot.hbs";
import render_settings_edit_embedded_bot_service from "../templates/settings/edit_embedded_bot_service.hbs";
import render_settings_edit_outgoing_webhook_service from "../templates/settings/edit_outgoing_webhook_service.hbs";
const render_bot_avatar_row = require("../templates/bot_avatar_row.hbs"); import * as avatar from "./avatar";
const render_edit_bot = require("../templates/edit_bot.hbs"); import * as channel from "./channel";
const render_settings_edit_embedded_bot_service = require("../templates/settings/edit_embedded_bot_service.hbs"); import * as loading from "./loading";
const render_settings_edit_outgoing_webhook_service = require("../templates/settings/edit_outgoing_webhook_service.hbs"); import * as overlays from "./overlays";
import * as people from "./people";
import * as typeahead_helper from "./typeahead_helper";
const avatar = require("./avatar"); export function hide_errors() {
const channel = require("./channel");
const loading = require("./loading");
const overlays = require("./overlays");
const people = require("./people");
const typeahead_helper = require("./typeahead_helper");
exports.hide_errors = function () {
$("#bot_table_error").hide(); $("#bot_table_error").hide();
$(".bot_error").hide(); $(".bot_error").hide();
}; }
const focus_tab = { const focus_tab = {
add_a_new_bot_tab() { add_a_new_bot_tab() {
@ -26,7 +24,7 @@ const focus_tab = {
$("#add-a-new-bot-form").show(); $("#add-a-new-bot-form").show();
$("#active_bots_list").hide(); $("#active_bots_list").hide();
$("#inactive_bots_list").hide(); $("#inactive_bots_list").hide();
exports.hide_errors(); hide_errors();
}, },
active_bots_tab() { active_bots_tab() {
$("#bots_lists_navbar .active").removeClass("active"); $("#bots_lists_navbar .active").removeClass("active");
@ -34,7 +32,7 @@ const focus_tab = {
$("#add-a-new-bot-form").hide(); $("#add-a-new-bot-form").hide();
$("#active_bots_list").show(); $("#active_bots_list").show();
$("#inactive_bots_list").hide(); $("#inactive_bots_list").hide();
exports.hide_errors(); hide_errors();
}, },
inactive_bots_tab() { inactive_bots_tab() {
$("#bots_lists_navbar .active").removeClass("active"); $("#bots_lists_navbar .active").removeClass("active");
@ -42,23 +40,23 @@ const focus_tab = {
$("#add-a-new-bot-form").hide(); $("#add-a-new-bot-form").hide();
$("#active_bots_list").hide(); $("#active_bots_list").hide();
$("#inactive_bots_list").show(); $("#inactive_bots_list").show();
exports.hide_errors(); hide_errors();
}, },
}; };
exports.get_bot_info_div = function (bot_id) { export function get_bot_info_div(bot_id) {
const sel = `.bot_info[data-user-id="${CSS.escape(bot_id)}"]`; const sel = `.bot_info[data-user-id="${CSS.escape(bot_id)}"]`;
return $(sel).expectOne(); return $(sel).expectOne();
}; }
exports.bot_error = function (bot_id, xhr) { export function bot_error(bot_id, xhr) {
const bot_info = exports.get_bot_info_div(bot_id); const bot_info = get_bot_info_div(bot_id);
const bot_error_div = bot_info.find(".bot_error"); const bot_error_div = bot_info.find(".bot_error");
bot_error_div.text(JSON.parse(xhr.responseText).msg); bot_error_div.text(JSON.parse(xhr.responseText).msg);
bot_error_div.show(); bot_error_div.show();
const bot_box = bot_info.closest(".bot-information-box"); const bot_box = bot_info.closest(".bot-information-box");
bot_box.scrollTop(bot_box[0].scrollHeight - bot_box[0].clientHeight); bot_box.scrollTop(bot_box[0].scrollHeight - bot_box[0].clientHeight);
}; }
function add_bot_row(info) { function add_bot_row(info) {
const row = $(render_bot_avatar_row(info)); const row = $(render_bot_avatar_row(info));
@ -77,11 +75,11 @@ function is_local_part(value, element) {
); );
} }
exports.type_id_to_string = function (type_id) { export function type_id_to_string(type_id) {
return page_params.bot_types.find((bot_type) => bot_type.type_id === type_id).name; return page_params.bot_types.find((bot_type) => bot_type.type_id === type_id).name;
}; }
exports.render_bots = function () { export function render_bots() {
$("#active_bots_list").empty(); $("#active_bots_list").empty();
$("#inactive_bots_list").empty(); $("#inactive_bots_list").empty();
@ -93,7 +91,7 @@ exports.render_bots = function () {
name: elem.full_name, name: elem.full_name,
email: elem.email, email: elem.email,
user_id: elem.user_id, user_id: elem.user_id,
type: exports.type_id_to_string(elem.bot_type), type: type_id_to_string(elem.bot_type),
avatar_url: elem.avatar_url, avatar_url: elem.avatar_url,
api_key: elem.api_key, api_key: elem.api_key,
is_active: elem.is_active, is_active: elem.is_active,
@ -102,7 +100,7 @@ exports.render_bots = function () {
user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active; user_owns_an_active_bot = user_owns_an_active_bot || elem.is_active;
} }
if (exports.can_create_new_bots() && !user_owns_an_active_bot) { if (can_create_new_bots() && !user_owns_an_active_bot) {
focus_tab.add_a_new_bot_tab(); focus_tab.add_a_new_bot_tab();
return; return;
} }
@ -120,19 +118,19 @@ exports.render_bots = function () {
$("#active_bots_list").hide(); $("#active_bots_list").hide();
$("#inactive_bots_list").show(); $("#inactive_bots_list").show();
} }
}; }
exports.generate_zuliprc_uri = function (bot_id) { export function generate_zuliprc_uri(bot_id) {
const bot = bot_data.get(bot_id); const bot = bot_data.get(bot_id);
const data = exports.generate_zuliprc_content(bot); const data = generate_zuliprc_content(bot);
return exports.encode_zuliprc_as_uri(data); return encode_zuliprc_as_uri(data);
}; }
exports.encode_zuliprc_as_uri = function (zuliprc) { export function encode_zuliprc_as_uri(zuliprc) {
return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc); return "data:application/octet-stream;charset=utf-8," + encodeURIComponent(zuliprc);
}; }
exports.generate_zuliprc_content = function (bot) { export function generate_zuliprc_content(bot) {
let token; let token;
// For outgoing webhooks, include the token in the zuliprc. // For outgoing webhooks, include the token in the zuliprc.
// It's needed for authenticating to the Botserver. // It's needed for authenticating to the Botserver.
@ -151,9 +149,9 @@ exports.generate_zuliprc_content = function (bot) {
// Some tools would not work in files without a trailing new line. // Some tools would not work in files without a trailing new line.
"\n" "\n"
); );
}; }
exports.generate_botserverrc_content = function (email, api_key, token) { export function generate_botserverrc_content(email, api_key, token) {
return ( return (
"[]" + "[]" +
"\nemail=" + "\nemail=" +
@ -166,9 +164,9 @@ exports.generate_botserverrc_content = function (email, api_key, token) {
token + token +
"\n" "\n"
); );
}; }
exports.bot_creation_policy_values = { export const bot_creation_policy_values = {
admins_only: { admins_only: {
code: 3, code: 3,
description: i18n.t("Admins"), description: i18n.t("Admins"),
@ -183,7 +181,7 @@ exports.bot_creation_policy_values = {
}, },
}; };
exports.can_create_new_bots = function () { export function can_create_new_bots() {
if (page_params.is_admin) { if (page_params.is_admin) {
return true; return true;
} }
@ -192,14 +190,11 @@ exports.can_create_new_bots = function () {
return false; return false;
} }
return ( return page_params.realm_bot_creation_policy !== bot_creation_policy_values.admins_only.code;
page_params.realm_bot_creation_policy !== }
exports.bot_creation_policy_values.admins_only.code
);
};
exports.update_bot_settings_tip = function () { export function update_bot_settings_tip() {
const permission_type = exports.bot_creation_policy_values; const permission_type = bot_creation_policy_values;
const current_permission = page_params.realm_bot_creation_policy; const current_permission = page_params.realm_bot_creation_policy;
let tip_text; let tip_text;
if (current_permission === permission_type.admins_only.code) { if (current_permission === permission_type.admins_only.code) {
@ -210,13 +205,13 @@ exports.update_bot_settings_tip = function () {
tip_text = i18n.t("Anyone in this organization can add bots"); tip_text = i18n.t("Anyone in this organization can add bots");
} }
$(".bot-settings-tip").text(tip_text); $(".bot-settings-tip").text(tip_text);
}; }
exports.update_bot_permissions_ui = function () { export function update_bot_permissions_ui() {
exports.update_bot_settings_tip(); update_bot_settings_tip();
exports.hide_errors(); hide_errors();
$("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy);
if (!exports.can_create_new_bots()) { if (!can_create_new_bots()) {
$("#create_bot_form").hide(); $("#create_bot_form").hide();
$(".add-a-new-bot-tab").hide(); $(".add-a-new-bot-tab").hide();
focus_tab.active_bots_tab(); focus_tab.active_bots_tab();
@ -224,9 +219,9 @@ exports.update_bot_permissions_ui = function () {
$("#create_bot_form").show(); $("#create_bot_form").show();
$(".add-a-new-bot-tab").show(); $(".add-a-new-bot-tab").show();
} }
}; }
exports.set_up = function () { export function set_up() {
$("#payload_url_inputbox").hide(); $("#payload_url_inputbox").hide();
$("#create_payload_url").val(""); $("#create_payload_url").val("");
$("#service_name_list").hide(); $("#service_name_list").hide();
@ -243,7 +238,7 @@ exports.set_up = function () {
for (const bot of bot_data.get_all_bots_for_current_user()) { for (const bot of bot_data.get_all_bots_for_current_user()) {
if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) { if (bot.is_active && bot.bot_type === OUTGOING_WEBHOOK_BOT_TYPE_INT) {
const bot_token = bot_data.get_services(bot.user_id)[0].token; const bot_token = bot_data.get_services(bot.user_id)[0].token;
content += exports.generate_botserverrc_content(bot.email, bot.api_key, bot_token); content += generate_botserverrc_content(bot.email, bot.api_key, bot_token);
} }
} }
@ -253,7 +248,7 @@ exports.set_up = function () {
); );
}); });
exports.render_bots(); render_bots();
$.validator.addMethod( $.validator.addMethod(
"bot_local_part", "bot_local_part",
@ -273,7 +268,7 @@ exports.set_up = function () {
$("#create_bot_form").validate({ $("#create_bot_form").validate({
errorClass: "text-error", errorClass: "text-error",
success() { success() {
exports.hide_errors(); hide_errors();
}, },
submitHandler() { submitHandler() {
const bot_type = $("#create_bot_type :selected").val(); const bot_type = $("#create_bot_type :selected").val();
@ -316,7 +311,7 @@ exports.set_up = function () {
processData: false, processData: false,
contentType: false, contentType: false,
success() { success() {
exports.hide_errors(); hide_errors();
$("#create_bot_name").val(""); $("#create_bot_name").val("");
$("#create_bot_short_name").val(""); $("#create_bot_short_name").val("");
$("#create_payload_url").val(""); $("#create_payload_url").val("");
@ -382,7 +377,7 @@ exports.set_up = function () {
}); });
}, },
error(xhr) { error(xhr) {
exports.bot_error(bot_id, xhr); bot_error(bot_id, xhr);
}, },
}); });
}); });
@ -393,7 +388,7 @@ exports.set_up = function () {
channel.post({ channel.post({
url: "/json/users/" + encodeURIComponent(user_id) + "/reactivate", url: "/json/users/" + encodeURIComponent(user_id) + "/reactivate",
error(xhr) { error(xhr) {
exports.bot_error(user_id, xhr); bot_error(user_id, xhr);
}, },
}); });
}); });
@ -543,7 +538,7 @@ exports.set_up = function () {
$("#active_bots_list").on("click", "a.download_bot_zuliprc", function () { $("#active_bots_list").on("click", "a.download_bot_zuliprc", function () {
const bot_info = $(this).closest(".bot-information-box").find(".bot_info"); const bot_info = $(this).closest(".bot-information-box").find(".bot_info");
const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10); const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10);
$(this).attr("href", exports.generate_zuliprc_uri(bot_id)); $(this).attr("href", generate_zuliprc_uri(bot_id));
}); });
new ClipboardJS("#copy_zuliprc", { new ClipboardJS("#copy_zuliprc", {
@ -551,7 +546,7 @@ exports.set_up = function () {
const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info"); const bot_info = $(trigger).closest(".bot-information-box").find(".bot_info");
const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10); const bot_id = Number.parseInt(bot_info.attr("data-user-id"), 10);
const bot = bot_data.get(bot_id); const bot = bot_data.get(bot_id);
const data = exports.generate_zuliprc_content(bot); const data = generate_zuliprc_content(bot);
return data; return data;
}, },
}); });
@ -573,6 +568,4 @@ exports.set_up = function () {
e.stopPropagation(); e.stopPropagation();
focus_tab.inactive_bots_tab(); focus_tab.inactive_bots_tab();
}); });
}; }
window.settings_bots = exports;

View File

@ -3,6 +3,7 @@
const alert_words_ui = require("./alert_words_ui"); const alert_words_ui = require("./alert_words_ui");
const attachments_ui = require("./attachments_ui"); const attachments_ui = require("./attachments_ui");
const settings_account = require("./settings_account"); const settings_account = require("./settings_account");
const settings_bots = require("./settings_bots");
const settings_display = require("./settings_display"); const settings_display = require("./settings_display");
const settings_muting = require("./settings_muting"); const settings_muting = require("./settings_muting");
const settings_notifications = require("./settings_notifications"); const settings_notifications = require("./settings_notifications");

View File

@ -11,6 +11,7 @@ const overlays = require("./overlays");
const people = require("./people"); const people = require("./people");
const presence = require("./presence"); const presence = require("./presence");
const settings_account = require("./settings_account"); const settings_account = require("./settings_account");
const settings_bots = require("./settings_bots");
const settings_config = require("./settings_config"); const settings_config = require("./settings_config");
const settings_data = require("./settings_data"); const settings_data = require("./settings_data");
const settings_panel_menu = require("./settings_panel_menu"); const settings_panel_menu = require("./settings_panel_menu");