Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings_users: Move bot related code from to settings_bots.ts.
Sahil Batra710ddd7d4410
diff --git a/web/src/admin.ts b/web/src/admin.tsindex 6a8ff8b1429..b3523f9bcc2 100644--- a/web/src/admin.ts+++ b/web/src/admin.ts@@ -281,8 +281,8 @@ export function build_page(): void {settings_users.deactivated_user_list_dropdown_widget_name,giphy_help_link,...get_realm_level_notification_settings(),- all_bots_list_dropdown_widget_name: settings_users.all_bots_list_dropdown_widget_name,- your_bots_list_dropdown_widget_name: settings_users.your_bots_list_dropdown_widget_name,+ all_bots_list_dropdown_widget_name: settings_bots.all_bots_list_dropdown_widget_name,+ your_bots_list_dropdown_widget_name: settings_bots.your_bots_list_dropdown_widget_name,group_setting_labels: settings_config.all_group_setting_labels.realm,server_can_summarize_topics: realm.server_can_summarize_topics,is_plan_self_hosted:diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.jsindex c963af8ade7..f455680669c 100644--- a/web/src/server_events_dispatch.js+++ b/web/src/server_events_dispatch.js@@ -74,7 +74,6 @@ import * as settings_profile_fields from "./settings_profile_fields.ts";import * as settings_realm_domains from "./settings_realm_domains.ts";import * as settings_realm_user_settings_defaults from "./settings_realm_user_settings_defaults.ts";import * as settings_streams from "./settings_streams.ts";-import * as settings_users from "./settings_users.ts";import * as sidebar_ui from "./sidebar_ui.ts";import * as starred_messages from "./starred_messages.ts";import * as starred_messages_ui from "./starred_messages_ui.ts";@@ -495,23 +494,23 @@ export function dispatch_normal_event(event) {case "add":bot_data.add(event.bot);if (event.bot.owner_id === current_user.user_id) {- settings_users.redraw_your_bots_list();- settings_users.toggle_bot_config_download_container();+ settings_bots.redraw_your_bots_list();+ settings_bots.toggle_bot_config_download_container();}break;case "delete":bot_data.del(event.bot.user_id);- settings_users.redraw_your_bots_list();- settings_users.toggle_bot_config_download_container();+ settings_bots.redraw_your_bots_list();+ settings_bots.toggle_bot_config_download_container();break;case "update":bot_data.update(event.bot.user_id, event.bot);if ("owner_id" in event.bot) {- settings_users.redraw_your_bots_list();- settings_users.toggle_bot_config_download_container();+ settings_bots.redraw_your_bots_list();+ settings_bots.toggle_bot_config_download_container();}if ("is_active" in event.bot) {- settings_users.toggle_bot_config_download_container();+ settings_bots.toggle_bot_config_download_container();}break;default:@@ -614,7 +613,7 @@ export function dispatch_normal_event(event) {people.add_active_user(event.person, "server_events");settings_account.maybe_update_deactivate_account_button();if (event.person.is_bot) {- settings_users.redraw_all_bots_list();+ settings_bots.redraw_all_bots_list();}if (should_redraw) {@@ -633,7 +632,7 @@ export function dispatch_normal_event(event) {user_events.update_person(event.person);settings_account.maybe_update_deactivate_account_button();if (people.is_valid_bot_user(event.person.user_id)) {- settings_users.update_bot_data(event.person.user_id);+ settings_bots.update_bot_data(event.person.user_id);}break;case "remove": {diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.tsindex f7b0082dc87..7036ec19617 100644--- a/web/src/settings_bots.ts+++ b/web/src/settings_bots.ts@@ -1,36 +1,132 @@import $ from "jquery";import assert from "minimalistic-assert";+import type * as tippy from "tippy.js";import render_add_new_bot_form from "../templates/settings/add_new_bot_form.hbs";import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs";+import render_settings_user_list_row from "../templates/settings/settings_user_list_row.hbs";import * as avatar from "./avatar.ts";+import * as bot_data from "./bot_data.ts";+import type {Bot} from "./bot_data.ts";import * as channel from "./channel.ts";import {csrf_token} from "./csrf.ts";import * as dialog_widget from "./dialog_widget.ts";-import {$t_html} from "./i18n.ts";+import * as dropdown_widget from "./dropdown_widget.ts";+import {$t, $t_html} from "./i18n.ts";+import type {ListWidget as ListWidgetType} from "./list_widget.ts";+import * as ListWidget from "./list_widget.ts";+import * as loading from "./loading.ts";import {page_params} from "./page_params.ts";+import * as people from "./people.ts";+import * as scroll_util from "./scroll_util.ts";import * as settings_config from "./settings_config.ts";import * as settings_data from "./settings_data.ts";-import {realm} from "./state_data.ts";+import * as settings_users from "./settings_users.ts";+import {current_user, realm} from "./state_data.ts";import type {HTMLSelectOneElement} from "./types.ts";import * as ui_report from "./ui_report.ts";import type {UploadWidget} from "./upload_widget.ts";+import * as user_deactivation_ui from "./user_deactivation_ui.ts";+import * as user_sort from "./user_sort.ts";+import * as util from "./util.ts";const INCOMING_WEBHOOK_BOT_TYPE = 2;const OUTGOING_WEBHOOK_BOT_TYPE = "3";+const OUTGOING_WEBHOOK_BOT_TYPE_INT = 3;const EMBEDDED_BOT_TYPE = "4";+export const all_bots_list_dropdown_widget_name = "all_bots_list_select_bot_status";+export const your_bots_list_dropdown_widget_name = "your_bots_list_select_bot_status";+type BotType = {type_id: number;name: string;};+type BotInfo = {+ is_bot: boolean;+ role: number;+ status_code: number;+ is_active: boolean;+ user_id: number;+ full_name: string;+ user_role_text: string | undefined;+ img_src: string;+ bot_type: string | undefined;+ bot_owner_full_name: string;+ no_owner: boolean;+ is_current_user: boolean;+ can_modify: boolean;+ cannot_deactivate: boolean;+ cannot_edit: boolean;+ display_email: string;+} & (+ | {+ bot_owner_id: number;+ is_bot_owner_active: boolean;+ owner_img_src: string;+ }+ | {+ bot_owner_id: null;+ }+);++type BotSettingsSection = {+ dropdown_widget_name: string;+ filters: {+ text_search: string;+ status_code: number;+ };+ handle_events: () => void;+ create_table: () => void;+ list_widget: ListWidgetType<number, BotInfo> | undefined;+};++const all_bots_section: BotSettingsSection = {+ dropdown_widget_name: all_bots_list_dropdown_widget_name,+ filters: {+ text_search: "",+ // 0 status_code signifies Active status for our filter.+ status_code: 0,+ },+ handle_events: all_bots_handle_events,+ create_table: create_all_bots_table,+ list_widget: undefined,+};++const your_bots_section: BotSettingsSection = {+ dropdown_widget_name: your_bots_list_dropdown_widget_name,+ filters: {+ text_search: "",+ status_code: 0,+ },+ handle_events: your_bots_handle_events,+ create_table: create_your_bots_table,+ list_widget: undefined,+};+function is_local_part(value: string): boolean {// Adapted from Django's EmailValidatorreturn /^[\w!#$%&'*+/=?^`{|}~-]+(\.[\w!#$%&'*+/=?^`{|}~-]+)*$/i.test(value);}+function sort_bot_email(a: BotInfo, b: BotInfo): number {+ function email(bot: BotInfo): string {+ return (bot.display_email ?? "").toLowerCase();+ }++ return util.compare_a_b(email(a), email(b));+}++function sort_bot_owner(a: BotInfo, b: BotInfo): number {+ function owner_name(bot: BotInfo): string {+ return (bot.bot_owner_full_name || "").toLowerCase();+ }++ return util.compare_a_b(owner_name(a), owner_name(b));+}+export function generate_botserverrc_content(email: string,api_key: string,@@ -118,6 +214,19 @@ export function get_allowed_bot_types(): BotType[] {return allowed_bot_types;}+function bot_owner_full_name(owner_id: number | null): string | undefined {+ if (!owner_id) {+ return undefined;+ }++ const bot_owner = people.maybe_get_user_by_id(owner_id);+ if (!bot_owner) {+ return undefined;+ }++ return bot_owner.full_name;+}+export function add_a_new_bot(): void {const html_body = render_add_new_bot_form({bot_types: get_allowed_bot_types(),@@ -257,3 +366,342 @@ export function add_a_new_bot(): void {validate_input,});}++function bot_info(bot_user_id: number): BotInfo {+ const bot_user = people.get_by_user_id(bot_user_id);+ assert(bot_user.is_bot);++ const owner_id = bot_user.bot_owner_id;+ const owner_full_name = bot_owner_full_name(owner_id);++ const is_bot_owner = owner_id === current_user.user_id;+ const can_modify_bot = current_user.is_admin || is_bot_owner;++ return {+ is_bot: true,+ role: bot_user.role,+ is_active: people.is_person_active(bot_user.user_id),+ status_code: people.is_person_active(bot_user.user_id) ? 0 : 1,+ user_id: bot_user.user_id,+ full_name: bot_user.full_name,+ user_role_text: people.get_user_type(bot_user_id),+ img_src: people.small_avatar_url_for_person(bot_user),+ // Convert bot type id to string for viewing to the users.+ bot_type: settings_data.bot_type_id_to_string(bot_user.bot_type),+ bot_owner_full_name: owner_full_name ?? $t({defaultMessage: "No owner"}),+ no_owner: !owner_full_name,+ is_current_user: false,+ can_modify: can_modify_bot,+ cannot_deactivate: (bot_user.is_system_bot ?? false) || !can_modify_bot,+ cannot_edit: (bot_user.is_system_bot ?? false) || !can_modify_bot,+ // It's always safe to show the real email addresses for bot users+ display_email: bot_user.email,+ ...(owner_id+ ? {+ bot_owner_id: owner_id,+ is_bot_owner_active: people.is_person_active(owner_id),+ owner_img_src: people.small_avatar_url_for_person(+ people.get_by_user_id(owner_id),+ ),+ }+ : {+ bot_owner_id: null,+ }),+ };+}++function handle_bot_deactivation($tbody: JQuery): void {+ $tbody.on("click", ".deactivate", (e) => {+ e.preventDefault();+ e.stopPropagation();++ const $button_elem = $(e.target);+ const $row = $button_elem.closest(".user_row");+ const bot_id = Number.parseInt($row.attr("data-user-id")!, 10);++ function handle_confirm(): void {+ const url = "/json/bots/" + encodeURIComponent(bot_id);+ dialog_widget.submit_api_request(channel.del, url, {});+ }++ user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);+ });+}++function predicate_for_bot_filtering(item: BotInfo, section: BotSettingsSection): boolean {+ if (!item) {+ return false;+ }+ const search_query = section.filters.text_search.toLowerCase();+ const filter_searches =+ item.full_name.toLowerCase().includes(search_query) ||+ item.display_email.toLowerCase().includes(search_query);++ const filter_status = item.status_code === section.filters.status_code;+ return filter_searches && filter_status;+}++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 redraw_all_bots_list(): void {+ // In order to properly redraw after a user may have been added,+ // we need to update the all_bots_section.list_widget with the new+ // set of bot user IDs to display.+ if (!all_bots_section.list_widget) {+ return;+ }++ const bot_user_ids = people.get_bot_ids();+ all_bots_section.list_widget.replace_list_data(bot_user_ids);+}++export function redraw_your_bots_list(): void {+ // In order to properly redraw after a user may have been added,+ // we need to update the your_bots_list_widget with the new set of bot+ // user IDs to display.+ if (!your_bots_section.list_widget) {+ return;+ }++ const bot_user_ids_for_current_owner = bot_data.get_all_bots_ids_for_current_user();+ your_bots_section.list_widget.replace_list_data(bot_user_ids_for_current_owner);+}++function add_value_to_filters(+ section: BotSettingsSection,+ key: "status_code" | "text_search",+ value: number | string,+): void {+ if (key === "status_code") {+ assert(typeof value === "number");+ section.filters.status_code = value;+ } else {+ assert(key === "text_search");+ assert(typeof value === "string");+ section.filters.text_search = value;+ }+ // This hard_redraw will rerun the relevant predicate function+ // and in turn apply the new filters.+ assert(section.list_widget !== undefined);+ section.list_widget.hard_redraw();+}++function handle_filter_change($tbody: JQuery, section: BotSettingsSection): void {+ // This duplicates the built-in search filter live-update logic in+ // ListWidget for the input.list_widget_filter event type, but we+ // can't use that, because we're also filtering on Role with our+ // custom predicate.+ $tbody+ .closest(".user-or-bot-settings-section")+ .find<HTMLInputElement>(".search")+ .on("input.list_widget_filter", function (this: HTMLInputElement) {+ add_value_to_filters(section, "text_search", this.value.toLocaleLowerCase());+ });+}++function get_bot_status_options(): {unique_id: number; name: string}[] {+ return [+ {unique_id: 0, name: $t({defaultMessage: "Active"})},+ {unique_id: 1, name: $t({defaultMessage: "Deactivated"})},+ ];+}++function status_selected_handler(+ event: JQuery.ClickEvent,+ dropdown: tippy.Instance,+ widget: dropdown_widget.DropdownWidget,+): void {+ event.preventDefault();+ event.stopPropagation();++ const status_code = Number($(event.currentTarget).attr("data-unique-id"));+ if (widget.widget_name === all_bots_section.dropdown_widget_name) {+ add_value_to_filters(all_bots_section, "status_code", status_code);+ } else if (widget.widget_name === your_bots_section.dropdown_widget_name) {+ add_value_to_filters(your_bots_section, "status_code", status_code);+ }+ dropdown.hide();+ widget.render();+}++function create_status_filter_dropdown(+ $events_container: JQuery,+ section: BotSettingsSection,+): void {+ new dropdown_widget.DropdownWidget({+ widget_name: section.dropdown_widget_name,+ unique_id_type: "number",+ get_options: get_bot_status_options,+ $events_container,+ item_click_callback: status_selected_handler,+ default_id: section.filters.status_code,+ hide_search_box: true,+ tippy_props: {+ offset: [0, 0],+ },+ }).setup();+}++function are_filters_active(+ filters: BotSettingsSection["filters"],+ $search_input: JQuery,+): boolean {+ const search_value = String($search_input.val()).trim();+ return Boolean(search_value) || filters.status_code !== 0;+}++function reset_scrollbar($sel: JQuery): () => void {+ return function () {+ scroll_util.reset_scrollbar($sel);+ };+}++function create_all_bots_table(): void {+ loading.make_indicator($("#admin_page_all_bots_loading_indicator"), {+ text: $t({defaultMessage: "Loading…"}),+ });+ const $all_bots_table = $("#admin_all_bots_table");+ $all_bots_table.hide();+ const bot_user_ids = people.get_bot_ids();++ all_bots_section.list_widget = ListWidget.create($all_bots_table, bot_user_ids, {+ name: "admin_bot_list",+ get_item: bot_info,+ modifier_html: render_settings_user_list_row,+ html_selector: (item) => $(`tr[data-user-id='${CSS.escape(item.user_id.toString())}']`),+ filter: {+ predicate(item) {+ return predicate_for_bot_filtering(item, all_bots_section);+ },+ is_active() {+ const $search_input = $("#admin-all-bots-list .search");+ return are_filters_active(all_bots_section.filters, $search_input);+ },+ onupdate: reset_scrollbar($all_bots_table),+ },+ $parent_container: $("#admin-all-bots-list").expectOne(),+ init_sort: "full_name_alphabetic",+ sort_fields: {+ email: sort_bot_email,+ bot_owner: sort_bot_owner,+ role: user_sort.sort_role,+ ...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]),+ },+ $simplebar_container: $("#admin-all-bots-list .progressive-table-wrapper"),+ });+ settings_users.set_text_search_value($all_bots_table, all_bots_section.filters.text_search);++ loading.destroy_indicator($("#admin_page_all_bots_loading_indicator"));+ $all_bots_table.show();+}++function create_your_bots_table(): void {+ loading.make_indicator($("#admin_page_your_bots_loading_indicator"), {+ text: $t({defaultMessage: "Loading…"}),+ });+ const $your_bots_table = $("#admin_your_bots_table");+ $your_bots_table.hide();+ const bot_user_ids = bot_data.get_all_bots_ids_for_current_user();+ your_bots_section.list_widget = ListWidget.create($your_bots_table, bot_user_ids, {+ name: "admin_your_bot_list",+ get_item: bot_info,+ modifier_html: render_settings_user_list_row,+ html_selector: (item) => $(`tr[data-user-id='${CSS.escape(item.user_id.toString())}']`),+ filter: {+ predicate(item) {+ return predicate_for_bot_filtering(item, your_bots_section);+ },+ is_active() {+ const $search_input = $("#admin-your-bots-list .search");+ return are_filters_active(your_bots_section.filters, $search_input);+ },+ onupdate: reset_scrollbar($your_bots_table),+ },+ $parent_container: $("#admin-your-bots-list").expectOne(),+ init_sort: "full_name_alphabetic",+ sort_fields: {+ email: sort_bot_email,+ bot_owner: sort_bot_owner,+ role: user_sort.sort_role,+ ...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]),+ },+ $simplebar_container: $("#admin-your-bots-list .progressive-table-wrapper"),+ });+ settings_users.set_text_search_value($your_bots_table, your_bots_section.filters.text_search);++ loading.destroy_indicator($("#admin_page_your_bots_loading_indicator"));+ $your_bots_table.show();+}++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));+ }++ if (your_bots_section.list_widget) {+ your_bots_section.list_widget.render_item(bot_info(bot_user_id));+ }+}++function all_bots_handle_events(): void {+ const $tbody = $("#admin_all_bots_table").expectOne();++ handle_filter_change($tbody, all_bots_section);+ handle_bot_deactivation($tbody);+ settings_users.handle_reactivation($tbody);+ settings_users.handle_edit_form($tbody);+ settings_users.handle_clear_button_for_table_search_input($tbody);+}++function your_bots_handle_events(): void {+ const $tbody = $("#admin_your_bots_table").expectOne();++ handle_filter_change($tbody, your_bots_section);+ handle_bot_deactivation($tbody);+ settings_users.handle_reactivation($tbody);+ settings_users.handle_edit_form($tbody);+ settings_users.handle_clear_button_for_table_search_input($tbody);+}++export function set_up_bots(): void {+ all_bots_section.handle_events();+ your_bots_section.handle_events();+ all_bots_section.create_table();+ your_bots_section.create_table();++ $("#admin-bot-list .add-a-new-bot").on("click", (e) => {+ e.preventDefault();+ e.stopPropagation();+ add_a_new_bot();+ });+ 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);+ }+ }++ $("#hidden-botserverrc-download").attr(+ "href",+ "data:application/octet-stream;charset=utf-8," + encodeURIComponent(content),+ );+ $("#hidden-botserverrc-download")[0]?.click();+ });+ toggle_bot_config_download_container();+}diff --git a/web/src/settings_panel_menu.ts b/web/src/settings_panel_menu.tsindex 1ab49971c25..fe95846b842 100644--- a/web/src/settings_panel_menu.ts+++ b/web/src/settings_panel_menu.ts@@ -9,13 +9,9 @@ import {$t, $t_html} from "./i18n.ts";import * as keydown_util from "./keydown_util.ts";import * as popovers from "./popovers.ts";import * as scroll_util from "./scroll_util.ts";+import {redraw_all_bots_list, redraw_your_bots_list} from "./settings_bots.ts";import * as settings_sections from "./settings_sections.ts";-import {- redraw_active_users_list,- redraw_all_bots_list,- redraw_deactivated_users_list,- redraw_your_bots_list,-} from "./settings_users.ts";+import {redraw_active_users_list, redraw_deactivated_users_list} from "./settings_users.ts";import * as util from "./util.ts";export let normal_settings: SettingsPanelMenu;diff --git a/web/src/settings_sections.ts b/web/src/settings_sections.tsindex d6d618afd07..43480a47db8 100644--- a/web/src/settings_sections.ts+++ b/web/src/settings_sections.ts@@ -4,6 +4,7 @@ import * as alert_words_ui from "./alert_words_ui.ts";import * as attachments_ui from "./attachments_ui.ts";import * as blueslip from "./blueslip.ts";import * as settings_account from "./settings_account.ts";+import * as settings_bots from "./settings_bots.ts";import * as settings_emoji from "./settings_emoji.ts";import * as settings_exports from "./settings_exports.ts";import * as settings_folders from "./settings_folders.ts";@@ -65,7 +66,7 @@ export function initialize(): void {// orgload_func_dict.set("org_misc", settings_org.set_up);- load_func_dict.set("org_bots", settings_users.set_up_bots);+ load_func_dict.set("org_bots", settings_bots.set_up_bots);load_func_dict.set("org_users", settings_users.set_up_humans);load_func_dict.set("emoji-settings", settings_emoji.set_up);load_func_dict.set("default-channels-list", settings_streams.set_up);diff --git a/web/src/settings_users.ts b/web/src/settings_users.tsindex e4e893aed99..24947a8eed6 100644--- a/web/src/settings_users.ts+++ b/web/src/settings_users.ts@@ -5,8 +5,6 @@ import type * as tippy from "tippy.js";import render_settings_user_list_row from "../templates/settings/settings_user_list_row.hbs";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";@@ -20,9 +18,7 @@ import * as people from "./people.ts";import type {User} from "./people.ts";import * as presence from "./presence.ts";import * as scroll_util from "./scroll_util.ts";-import * as settings_bots from "./settings_bots.ts";import * as settings_config from "./settings_config.ts";-import * as settings_data from "./settings_data.ts";import * as setting_invites from "./settings_invites.ts";import {current_user} from "./state_data.ts";import * as timerender from "./timerender.ts";@@ -31,12 +27,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";-export const your_bots_list_dropdown_widget_name = "your_bots_list_select_bot_status";let should_redraw_active_users_list = false;let should_redraw_deactivated_users_list = false;@@ -79,56 +71,6 @@ const deactivated_section: UserSettingsSection = {list_widget: undefined,};-type BotSettingsSection = {- dropdown_widget_name: string;- filters: {- text_search: string;- status_code: number;- };- handle_events: () => void;- create_table: () => void;- list_widget: ListWidgetType<number, BotInfo> | undefined;-};--const all_bots_section: BotSettingsSection = {- dropdown_widget_name: all_bots_list_dropdown_widget_name,- filters: {- text_search: "",- // 0 status_code signifies Active status for our filter.- status_code: 0,- },- handle_events: all_bots_handle_events,- create_table: create_all_bots_table,- list_widget: undefined,-};--const your_bots_section: BotSettingsSection = {- dropdown_widget_name: your_bots_list_dropdown_widget_name,- filters: {- text_search: "",- status_code: 0,- },- handle_events: your_bots_handle_events,- create_table: create_your_bots_table,- list_widget: undefined,-};--function sort_bot_email(a: BotInfo, b: BotInfo): number {- function email(bot: BotInfo): string {- return (bot.display_email ?? "").toLowerCase();- }-- return util.compare_a_b(email(a), email(b));-}--function sort_bot_owner(a: BotInfo, b: BotInfo): number {- function owner_name(bot: BotInfo): string {- return (bot.bot_owner_full_name || "").toLowerCase();- }-- return util.compare_a_b(owner_name(a), owner_name(b));-}-function sort_last_active(a: User, b: User): number {return util.compare_a_b(presence.last_active_date(a.user_id) ?? 0,@@ -223,18 +165,13 @@ export function update_view_on_reactivate(user_id: number, is_bot: boolean): voi}function add_value_to_filters(- section: UserSettingsSection | BotSettingsSection,- key: "role_code" | "status_code" | "text_search",+ section: UserSettingsSection,+ key: "role_code" | "text_search",value: number | string,): void {if (key === "role_code") {- assert("role_code" in section.filters);assert(typeof value === "number");section.filters.role_code = value;- } else if (key === "status_code") {- assert("status_code" in section.filters);- assert(typeof value === "number");- section.filters.status_code = value;} else {assert(key === "text_search");assert(typeof value === "string");@@ -247,15 +184,11 @@ function add_value_to_filters(}function are_filters_active(- filters: {text_search: string; role_code?: number; status_code?: number},+ filters: UserSettingsSection["filters"],$search_input: JQuery,): boolean {const search_value = String($search_input.val()).trim();- const has_selected_dropdown_filter =- (filters.role_code !== undefined && filters.role_code !== 0) ||- (filters.status_code !== undefined && filters.status_code !== 0);-- return Boolean(search_value) || has_selected_dropdown_filter;+ return Boolean(search_value) || filters.role_code !== 0;}function role_selected_handler(@@ -277,24 +210,6 @@ function role_selected_handler(widget.render();}-function status_selected_handler(- event: JQuery.ClickEvent,- dropdown: tippy.Instance,- widget: dropdown_widget.DropdownWidget,-): void {- event.preventDefault();- event.stopPropagation();-- const status_code = Number($(event.currentTarget).attr("data-unique-id"));- if (widget.widget_name === all_bots_section.dropdown_widget_name) {- add_value_to_filters(all_bots_section, "status_code", status_code);- } else if (widget.widget_name === your_bots_section.dropdown_widget_name) {- add_value_to_filters(your_bots_section, "status_code", status_code);- }- dropdown.hide();- widget.render();-}-function count_users_by_role(user_ids: number[]): Record<number, number> {const role_counts: Record<number, number> = {};@@ -376,31 +291,6 @@ function initialize_user_sections(active_user_ids: number[], deactivated_user_iddeactivated_users_role_dropdown.setup();}-function get_bot_status_options(): {unique_id: number; name: string}[] {- return [- {unique_id: 0, name: $t({defaultMessage: "Active"})},- {unique_id: 1, name: $t({defaultMessage: "Deactivated"})},- ];-}--function create_status_filter_dropdown(- $events_container: JQuery,- section: BotSettingsSection,-): void {- new dropdown_widget.DropdownWidget({- widget_name: section.dropdown_widget_name,- unique_id_type: "number",- get_options: get_bot_status_options,- $events_container,- item_click_callback: status_selected_handler,- default_id: section.filters.status_code,- hide_search_box: true,- tippy_props: {- offset: [0, 0],- },- }).setup();-}-function populate_users(): void {const active_user_ids = people.get_realm_active_human_user_ids();const deactivated_user_ids = people.get_non_active_human_ids();@@ -424,90 +314,6 @@ function reset_scrollbar($sel: JQuery): () => void {};}-function bot_owner_full_name(owner_id: number | null): string | undefined {- if (!owner_id) {- return undefined;- }-- const bot_owner = people.maybe_get_user_by_id(owner_id);- if (!bot_owner) {- return undefined;- }-- return bot_owner.full_name;-}--type BotInfo = {- is_bot: boolean;- role: number;- status_code: number;- is_active: boolean;- user_id: number;- full_name: string;- user_role_text: string | undefined;- img_src: string;- bot_type: string | undefined;- bot_owner_full_name: string;- no_owner: boolean;- is_current_user: boolean;- can_modify: boolean;- cannot_deactivate: boolean;- cannot_edit: boolean;- display_email: string;-} & (- | {- bot_owner_id: number;- is_bot_owner_active: boolean;- owner_img_src: string;- }- | {- bot_owner_id: null;- }-);--function bot_info(bot_user_id: number): BotInfo {- const bot_user = people.get_by_user_id(bot_user_id);- assert(bot_user.is_bot);-- const owner_id = bot_user.bot_owner_id;- const owner_full_name = bot_owner_full_name(owner_id);-- const is_bot_owner = owner_id === current_user.user_id;- const can_modify_bot = current_user.is_admin || is_bot_owner;-- return {- is_bot: true,- role: bot_user.role,- is_active: people.is_person_active(bot_user.user_id),- status_code: people.is_person_active(bot_user.user_id) ? 0 : 1,- user_id: bot_user.user_id,- full_name: bot_user.full_name,- user_role_text: people.get_user_type(bot_user_id),- img_src: people.small_avatar_url_for_person(bot_user),- // Convert bot type id to string for viewing to the users.- bot_type: settings_data.bot_type_id_to_string(bot_user.bot_type),- bot_owner_full_name: owner_full_name ?? $t({defaultMessage: "No owner"}),- no_owner: !owner_full_name,- is_current_user: false,- can_modify: can_modify_bot,- cannot_deactivate: (bot_user.is_system_bot ?? false) || !can_modify_bot,- cannot_edit: (bot_user.is_system_bot ?? false) || !can_modify_bot,- // It's always safe to show the real email addresses for bot users- display_email: bot_user.email,- ...(owner_id- ? {- bot_owner_id: owner_id,- is_bot_owner_active: people.is_person_active(owner_id),- owner_img_src: people.small_avatar_url_for_person(- people.get_by_user_id(owner_id),- ),- }- : {- bot_owner_id: null,- }),- };-}-function get_last_active(user: User): string {const last_active_date = presence.last_active_date(user.user_id);if (!last_active_date && presence_data_fetched) {@@ -561,100 +367,10 @@ function human_info(person: User): {};}-function set_text_search_value($table: JQuery, value: string): void {+export function set_text_search_value($table: JQuery, value: string): void {$table.closest(".user-or-bot-settings-section").find(".search").val(value);}-export function predicate_for_bot_filtering(item: BotInfo, section: BotSettingsSection): boolean {- if (!item) {- return false;- }- const search_query = section.filters.text_search.toLowerCase();- const filter_searches =- item.full_name.toLowerCase().includes(search_query) ||- item.display_email.toLowerCase().includes(search_query);-- const filter_status = item.status_code === section.filters.status_code;- return filter_searches && filter_status;-}--function create_all_bots_table(): void {- loading.make_indicator($("#admin_page_all_bots_loading_indicator"), {- text: $t({defaultMessage: "Loading…"}),- });- const $all_bots_table = $("#admin_all_bots_table");- $all_bots_table.hide();- const bot_user_ids = people.get_bot_ids();-- all_bots_section.list_widget = ListWidget.create($all_bots_table, bot_user_ids, {- name: "admin_bot_list",- get_item: bot_info,- modifier_html: render_settings_user_list_row,- html_selector: (item) => $(`tr[data-user-id='${CSS.escape(item.user_id.toString())}']`),- filter: {- predicate(item) {- return predicate_for_bot_filtering(item, all_bots_section);- },- is_active() {- const $search_input = $("#admin-all-bots-list .search");- return are_filters_active(all_bots_section.filters, $search_input);- },- onupdate: reset_scrollbar($all_bots_table),- },- $parent_container: $("#admin-all-bots-list").expectOne(),- init_sort: "full_name_alphabetic",- sort_fields: {- email: sort_bot_email,- bot_owner: sort_bot_owner,- role: user_sort.sort_role,- ...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]),- },- $simplebar_container: $("#admin-all-bots-list .progressive-table-wrapper"),- });- set_text_search_value($all_bots_table, all_bots_section.filters.text_search);-- loading.destroy_indicator($("#admin_page_all_bots_loading_indicator"));- $all_bots_table.show();-}--function create_your_bots_table(): void {- loading.make_indicator($("#admin_page_your_bots_loading_indicator"), {- text: $t({defaultMessage: "Loading…"}),- });- const $your_bots_table = $("#admin_your_bots_table");- $your_bots_table.hide();- const bot_user_ids = bot_data.get_all_bots_ids_for_current_user();- your_bots_section.list_widget = ListWidget.create($your_bots_table, bot_user_ids, {- name: "admin_your_bot_list",- get_item: bot_info,- modifier_html: render_settings_user_list_row,- html_selector: (item) => $(`tr[data-user-id='${CSS.escape(item.user_id.toString())}']`),- filter: {- predicate(item) {- return predicate_for_bot_filtering(item, your_bots_section);- },- is_active() {- const $search_input = $("#admin-your-bots-list .search");- return are_filters_active(your_bots_section.filters, $search_input);- },- onupdate: reset_scrollbar($your_bots_table),- },- $parent_container: $("#admin-your-bots-list").expectOne(),- init_sort: "full_name_alphabetic",- sort_fields: {- email: sort_bot_email,- bot_owner: sort_bot_owner,- role: user_sort.sort_role,- ...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]),- },- $simplebar_container: $("#admin-your-bots-list .progressive-table-wrapper"),- });- set_text_search_value($your_bots_table, your_bots_section.filters.text_search);-- loading.destroy_indicator($("#admin_page_your_bots_loading_indicator"));- $your_bots_table.show();-}-function active_create_table(active_users: number[]): void {const $users_table = $("#admin_users_table");active_section.list_widget = ListWidget.create($users_table, active_users, {@@ -692,7 +408,7 @@ function active_create_table(active_users: number[]): void {$("#admin_users_table").show();}-function handle_clear_button_for_table_search_input($tbody: JQuery): void {+export function handle_clear_button_for_table_search_input($tbody: JQuery): void {const $container = $tbody.closest(".user-or-bot-settings-section");$container.on("click", ".clear-filter", (e) => {e.stopPropagation();@@ -746,24 +462,6 @@ 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));- }-- if (your_bots_section.list_widget) {- your_bots_section.list_widget.render_item(bot_info(bot_user_id));- }-}-export function update_user_data(user_id: number,new_data: {full_name?: string; role?: number},@@ -787,48 +485,21 @@ export function update_user_data(}}-export function redraw_all_bots_list(): void {- // In order to properly redraw after a user may have been added,- // we need to update the all_bots_section.list_widget with the new- // set of bot user IDs to display.- const bot_user_ids = people.get_bot_ids();- redraw_people_list(all_bots_section, bot_user_ids);-}--function redraw_people_list(- section: UserSettingsSection | BotSettingsSection,- list: number[],-): void {- if (!section.list_widget) {- return;- }-- section.list_widget.replace_list_data(list);-}--export function redraw_your_bots_list(): void {- // In order to properly redraw after a user may have been added,- // we need to update the your_bots_list_widget with the new set of bot- // user IDs to display.- const bot_user_ids_for_current_owner = bot_data.get_all_bots_ids_for_current_user();- redraw_people_list(your_bots_section, bot_user_ids_for_current_owner);-}-export function redraw_deactivated_users_list(): void {- if (!should_redraw_deactivated_users_list) {+ if (!should_redraw_deactivated_users_list || !deactivated_section.list_widget) {return;}const deactivated_user_ids = people.get_non_active_human_ids();- redraw_people_list(deactivated_section, deactivated_user_ids);+ deactivated_section.list_widget.replace_list_data(deactivated_user_ids);should_redraw_deactivated_users_list = false;}export function redraw_active_users_list(): void {- if (!should_redraw_active_users_list) {+ if (!should_redraw_active_users_list || !active_section.list_widget) {return;}const active_user_ids = people.get_realm_active_human_user_ids();- redraw_people_list(active_section, active_user_ids);+ active_section.list_widget.replace_list_data(active_user_ids);should_redraw_active_users_list = false;}@@ -883,25 +554,7 @@ function handle_deactivation($tbody: JQuery): void {});}-function handle_bot_deactivation($tbody: JQuery): void {- $tbody.on("click", ".deactivate", (e) => {- e.preventDefault();- e.stopPropagation();-- const $button_elem = $(e.target);- const $row = $button_elem.closest(".user_row");- const bot_id = Number.parseInt($row.attr("data-user-id")!, 10);-- function handle_confirm(): void {- const url = "/json/bots/" + encodeURIComponent(bot_id);- dialog_widget.submit_api_request(channel.del, url, {});- }-- user_deactivation_ui.confirm_bot_deactivation(bot_id, handle_confirm, true);- });-}--function handle_reactivation($tbody: JQuery): void {+export function handle_reactivation($tbody: JQuery): void {$tbody.on("click", ".reactivate", (e) => {e.preventDefault();e.stopPropagation();@@ -920,7 +573,7 @@ function handle_reactivation($tbody: JQuery): void {});}-function handle_edit_form($tbody: JQuery): void {+export function handle_edit_form($tbody: JQuery): void {$tbody.on("click", ".open-user-form", function (this: HTMLElement, e) {e.stopPropagation();e.preventDefault();@@ -936,10 +589,7 @@ function handle_edit_form($tbody: JQuery): void {});}-function handle_filter_change(- $tbody: JQuery,- section: UserSettingsSection | BotSettingsSection,-): void {+function handle_filter_change($tbody: JQuery, section: UserSettingsSection): void {// This duplicates the built-in search filter live-update logic in// ListWidget for the input.list_widget_filter event type, but we// can't use that, because we're also filtering on Role with our@@ -972,26 +622,6 @@ function deactivated_handle_events(): void {handle_clear_button_for_table_search_input($tbody);}-function all_bots_handle_events(): void {- const $tbody = $("#admin_all_bots_table").expectOne();-- handle_filter_change($tbody, all_bots_section);- handle_bot_deactivation($tbody);- handle_reactivation($tbody);- handle_edit_form($tbody);- handle_clear_button_for_table_search_input($tbody);-}--function your_bots_handle_events(): void {- const $tbody = $("#admin_your_bots_table").expectOne();-- handle_filter_change($tbody, your_bots_section);- handle_bot_deactivation($tbody);- handle_reactivation($tbody);- handle_edit_form($tbody);- handle_clear_button_for_table_search_input($tbody);-}-export function set_up_humans(): void {start_data_load();active_section.handle_events();@@ -999,47 +629,6 @@ export function set_up_humans(): void {setting_invites.set_up();}-export function set_up_bots(): void {- all_bots_section.handle_events();- your_bots_section.handle_events();- all_bots_section.create_table();- your_bots_section.create_table();-- $("#admin-bot-list .add-a-new-bot").on("click", (e) => {- e.preventDefault();- e.stopPropagation();- settings_bots.add_a_new_bot();- });- 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 = {render_table: () => void;};diff --git a/web/src/user_events.ts b/web/src/user_events.tsindex 42a8545b33f..57a11db52c6 100644--- a/web/src/user_events.ts+++ b/web/src/user_events.ts@@ -17,6 +17,7 @@ import * as people from "./people.ts";import * as pm_list from "./pm_list.ts";import * as settings from "./settings.ts";import * as settings_account from "./settings_account.ts";+import * as settings_bots from "./settings_bots.ts";import * as settings_config from "./settings_config.ts";import * as settings_exports from "./settings_exports.ts";import * as settings_linkifiers from "./settings_linkifiers.ts";@@ -226,7 +227,7 @@ export const update_person = function update(event: UserUpdate): void {buddy_list.insert_or_move([event.user_id]);settings_account.maybe_update_deactivate_account_button();if (is_bot_user) {- settings_users.update_bot_data(event.user_id);+ settings_bots.update_bot_data(event.user_id);} else if (!event.is_active) {// A human user deactivated, update 'Export permissions' table.settings_exports.remove_export_consent_data_and_redraw(event.user_id);diff --git a/web/tests/dispatch.test.cjs b/web/tests/dispatch.test.cjsindex 76d87e15786..1e3081ef23e 100644--- a/web/tests/dispatch.test.cjs+++ b/web/tests/dispatch.test.cjs@@ -81,7 +81,6 @@ const settings_realm_user_settings_defaults = mock_esm();const settings_realm_domains = mock_esm("../src/settings_realm_domains");const settings_streams = mock_esm("../src/settings_streams");-const settings_users = mock_esm("../src/settings_users");const sidebar_ui = mock_esm("../src/sidebar_ui");const stream_data = mock_esm("../src/stream_data");const stream_list = mock_esm("../src/stream_list");@@ -838,8 +837,8 @@ run_test("realm_bot add", ({override}) => {const bot_stub = make_stub();override(current_user, "user_id", test_user.user_id);override(bot_data, "add", bot_stub.f);- override(settings_users, "redraw_your_bots_list", noop);- override(settings_users, "toggle_bot_config_download_container", noop);+ override(settings_bots, "redraw_your_bots_list", noop);+ override(settings_bots, "toggle_bot_config_download_container", noop);dispatch(event);@@ -852,8 +851,8 @@ run_test("realm_bot delete", ({override}) => {const event = event_fixtures.realm_bot__delete;const bot_stub = make_stub();override(bot_data, "del", bot_stub.f);- override(settings_users, "redraw_your_bots_list", noop);- override(settings_users, "toggle_bot_config_download_container", noop);+ override(settings_bots, "redraw_your_bots_list", noop);+ override(settings_bots, "toggle_bot_config_download_container", noop);dispatch(event);assert.equal(bot_stub.num_calls, 1);@@ -877,13 +876,13 @@ run_test("realm_bot update", ({override}) => {override(bot_data, "update", bot_stub.f);let toggle_download_container_stub = make_stub();override(- settings_users,+ settings_bots,"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);+ override(settings_bots, "redraw_your_bots_list", noop);dispatch(event);assert.equal(toggle_download_container_stub.num_calls, 1);assert.equal(bot_stub.num_calls, 1);@@ -893,7 +892,7 @@ run_test("realm_bot update", ({override}) => {toggle_download_container_stub = make_stub();override(- settings_users,+ settings_bots,"toggle_bot_config_download_container",toggle_download_container_stub.f,);@@ -1022,13 +1021,13 @@ run_test("realm_user", ({override}) => {// Test bot related functions are being called.const add_bot_stub = make_stub();event = event_fixtures.realm_user__add_bot;- override(settings_users, "redraw_all_bots_list", add_bot_stub.f);+ override(settings_bots, "redraw_all_bots_list", add_bot_stub.f);dispatch({...event});assert.equal(add_bot_stub.num_calls, 1);const update_bot_stub = make_stub();event = event_fixtures.realm_user__update;- override(settings_users, "update_bot_data", update_bot_stub.f);+ override(settings_bots, "update_bot_data", update_bot_stub.f);dispatch(event);assert.equal(update_bot_stub.num_calls, 1);args = update_bot_stub.get_args("update_user_id", "update_bot_data");diff --git a/web/tests/example4.test.cjs b/web/tests/example4.test.cjsindex 3f028ba984c..93d6a387595 100644--- a/web/tests/example4.test.cjs+++ b/web/tests/example4.test.cjs@@ -59,6 +59,7 @@ const {run_test, noop} = require("./lib/test.cjs");const activity_ui = mock_esm("../src/activity_ui");const message_live_update = mock_esm("../src/message_live_update");const pm_list = mock_esm("../src/pm_list");+const settings_bots = mock_esm("../src/settings_bots");const settings_users = mock_esm("../src/settings_users");const user_profile = mock_esm("../src/user_profile");@@ -88,7 +89,7 @@ run_test("add users with event", ({override}) => {// We need to override a stub here before dispatching the event.// Keep reading to see how overriding works!- override(settings_users, "redraw_all_bots_list", noop);+ override(settings_bots, "redraw_all_bots_list", noop);override(activity_ui, "check_should_redraw_new_user", noop);// Let's simulate dispatching our event!server_events_dispatch.dispatch_normal_event(event);@@ -140,7 +141,7 @@ run_test("update user with event", ({override}) => {override(message_live_update, "update_user_full_name", noop);override(pm_list, "update_private_messages", noop);override(settings_users, "update_user_data", noop);- override(settings_users, "update_bot_data", noop);+ override(settings_bots, "update_bot_data", noop);override(user_profile, "update_profile_modal_ui", noop);// Dispatch the realm_user/update event, which will updatediff --git a/web/tests/user_events.test.cjs b/web/tests/user_events.test.cjsindex ea5a0850d6f..4be480123b9 100644--- a/web/tests/user_events.test.cjs+++ b/web/tests/user_events.test.cjs@@ -16,7 +16,8 @@ const settings_account = mock_esm("../src/settings_account", {update_account_settings_display() {},update_role_text() {},});-const settings_users = mock_esm("../src/settings_users", {+const settings_bots = mock_esm("../src/settings_bots");+mock_esm("../src/settings_users", {update_user_data() {},update_view_on_deactivate() {},update_view_on_reactivate() {},@@ -279,7 +280,7 @@ run_test("updates", ({override}) => {assert.ok(people.is_person_active(isaac.user_id));let bot_data_updated = false;- settings_users.update_bot_data = (user_id) => {+ settings_bots.update_bot_data = (user_id) => {assert.equal(user_id, test_bot.user_id);bot_data_updated = true;};