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.

page_params: Remove `bot_types` field to reduce database queries.

Vector73469bc4f4423f
diff --git a/web/src/base_page_params.ts b/web/src/base_page_params.ts
index 2cdfbc12a22..7e6424fb4b8 100644
--- a/web/src/base_page_params.ts
+++ b/web/src/base_page_params.ts
@@ -19,14 +19,8 @@ const home_params_schema = default_params_schema
.extend({
page_type: z.literal("home"),
apps_page_url: z.string(),
- bot_types: z.array(
- z.object({
- type_id: z.number(),
- name: z.string(),
- allowed: z.boolean(),
- }),
- ),
corporate_enabled: z.boolean(),
+ embedded_bots_enabled: z.boolean(),
furthest_read_time: z.nullable(z.number()),
is_spectator: z.boolean(),
// `language_cookie_name` is only sent for spectators.
diff --git a/web/src/settings_bots.ts b/web/src/settings_bots.ts
index 6863f801854..88016c06ee1 100644
--- a/web/src/settings_bots.ts
+++ b/web/src/settings_bots.ts
@@ -19,6 +19,7 @@ import * as integration_url_modal from "./integration_url_modal.ts";
import * as list_widget from "./list_widget.ts";
import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
+import * as settings_config from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
import {realm} from "./state_data.ts";
import type {HTMLSelectOneElement} from "./types.ts";
@@ -44,6 +45,11 @@ type BotInfo = {
zuliprc: string;
};
+type BotType = {
+ type_id: number;
+ name: string;
+};
+
function add_bot_row(info: BotInfo): void {
const $row = $(render_bot_avatar_row(info));
if (info.is_active) {
@@ -212,9 +218,27 @@ export function update_bot_permissions_ui(): void {
update_add_bot_button();
}
+export function get_allowed_bot_types(): BotType[] {
+ const allowed_bot_types: BotType[] = [];
+ const bot_types = settings_config.bot_type_values;
+ if (can_create_new_bots()) {
+ allowed_bot_types.push(
+ bot_types.default_bot,
+ bot_types.incoming_webhook_bot,
+ bot_types.outgoing_webhook_bot,
+ );
+ if (page_params.embedded_bots_enabled) {
+ allowed_bot_types.push(bot_types.embedded_bot);
+ }
+ } else if (can_create_incoming_webhooks()) {
+ allowed_bot_types.push(bot_types.incoming_webhook_bot);
+ }
+ return allowed_bot_types;
+}
+
export function add_a_new_bot(): void {
const html_body = render_add_new_bot_form({
- bot_types: page_params.bot_types,
+ bot_types: get_allowed_bot_types(),
realm_embedded_bots: realm.realm_embedded_bots,
realm_bot_domain: realm.realm_bot_domain,
});
diff --git a/web/src/settings_config.ts b/web/src/settings_config.ts
index ab27c1cc0ec..60ee1ac9a0c 100644
--- a/web/src/settings_config.ts
+++ b/web/src/settings_config.ts
@@ -1194,3 +1194,22 @@ export const export_type_values = {
default: true,
},
};
+
+export const bot_type_values = {
+ default_bot: {
+ type_id: 1,
+ name: $t({defaultMessage: "Generic bot"}),
+ },
+ incoming_webhook_bot: {
+ type_id: 2,
+ name: $t({defaultMessage: "Incoming webhook"}),
+ },
+ outgoing_webhook_bot: {
+ type_id: 3,
+ name: $t({defaultMessage: "Outgoing webhook"}),
+ },
+ embedded_bot: {
+ type_id: 4,
+ name: $t({defaultMessage: "Embedded bot"}),
+ },
+};
diff --git a/web/src/settings_data.ts b/web/src/settings_data.ts
index 427563de7df..f6f1d59813b 100644
--- a/web/src/settings_data.ts
+++ b/web/src/settings_data.ts
@@ -293,7 +293,9 @@ export function user_email_not_configured(): boolean {
}
export function bot_type_id_to_string(type_id: number): string | undefined {
- const bot_type = page_params.bot_types.find((bot_type) => bot_type.type_id === type_id);
+ const bot_type = Object.values(settings_config.bot_type_values).find(
+ (bot_type) => bot_type.type_id === type_id,
+ );
if (bot_type === undefined) {
return undefined;
diff --git a/web/templates/settings/add_new_bot_form.hbs b/web/templates/settings/add_new_bot_form.hbs
index 0d8dd72e840..702fdb9f423 100644
--- a/web/templates/settings/add_new_bot_form.hbs
+++ b/web/templates/settings/add_new_bot_form.hbs
@@ -7,9 +7,7 @@
</label>
<select name="bot_type" id="create_bot_type" class="modal_select bootstrap-focus-style">
{{#each bot_types}}
- {{#if this.allowed}}
<option value="{{this.type_id}}">{{this.name}}</option>
- {{/if}}
{{/each}}
</select>
</div>
diff --git a/web/tests/settings_data.test.cjs b/web/tests/settings_data.test.cjs
index 79e36934f91..d227ffb9596 100644
--- a/web/tests/settings_data.test.cjs
+++ b/web/tests/settings_data.test.cjs
@@ -501,21 +501,8 @@ run_test("can_remove_members_user_group", ({override}) => {
});
run_test("type_id_to_string", () => {
- page_params.bot_types = [
- {
- type_id: 1,
- name: "Generic bot",
- allowed: true,
- },
- {
- type_id: 2,
- name: "Incoming webhook",
- allowed: true,
- },
- ];
-
- assert.equal(settings_data.bot_type_id_to_string(1), "Generic bot");
- assert.equal(settings_data.bot_type_id_to_string(2), "Incoming webhook");
+ assert.equal(settings_data.bot_type_id_to_string(1), "translated: Generic bot");
+ assert.equal(settings_data.bot_type_id_to_string(2), "translated: Incoming webhook");
assert.equal(settings_data.bot_type_id_to_string(5), undefined);
});
diff --git a/zerver/lib/home.py b/zerver/lib/home.py
index bcceba22950..262476fe46b 100644
--- a/zerver/lib/home.py
+++ b/zerver/lib/home.py
@@ -49,22 +49,6 @@ def get_furthest_read_time(user_profile: UserProfile | None) -> float | None:
return calendar.timegm(user_activity.last_visit.utctimetuple())
-def get_bot_types(user_profile: UserProfile | None) -> list[dict[str, object]]:
- bot_types: list[dict[str, object]] = []
- if user_profile is None:
- return bot_types
-
- for type_id, name in UserProfile.BOT_TYPES.items():
- bot_types.append(
- dict(
- type_id=type_id,
- name=name,
- allowed=type_id in user_profile.allowed_bot_types,
- )
- )
- return bot_types
-
-
def promote_sponsoring_zulip_in_realm(realm: Realm) -> bool:
if not settings.PROMOTE_SPONSORING_ZULIP:
return False
@@ -215,7 +199,7 @@ def build_page_params_for_home_page_load(
## Misc. extra data.
language_list=get_language_list(),
furthest_read_time=furthest_read_time,
- bot_types=get_bot_types(user_profile),
+ embedded_bots_enabled=settings.EMBEDDED_BOTS_ENABLED,
two_fa_enabled=two_fa_enabled,
apps_page_url=get_apps_page_url(),
show_billing=billing_info.show_billing,
diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py
index b230aca5990..f550dbb62b5 100644
--- a/zerver/tests/test_home.py
+++ b/zerver/tests/test_home.py
@@ -45,9 +45,9 @@ class HomeTest(ZulipTestCase):
# Keep this list sorted!!!
expected_page_params_keys = [
"apps_page_url",
- "bot_types",
"corporate_enabled",
"development_environment",
+ "embedded_bots_enabled",
"furthest_read_time",
"insecure_desktop_app",
"is_spectator",
@@ -276,7 +276,7 @@ class HomeTest(ZulipTestCase):
# Verify succeeds once logged-in
with (
- self.assert_database_query_count(59),
+ self.assert_database_query_count(54),
patch("zerver.lib.cache.cache_set") as cache_mock,
):
result = self._get_home_page(stream="Denmark")
@@ -359,9 +359,9 @@ class HomeTest(ZulipTestCase):
self.assertEqual(page_params["is_spectator"], True)
expected_keys = [
"apps_page_url",
- "bot_types",
"corporate_enabled",
"development_environment",
+ "embedded_bots_enabled",
"furthest_read_time",
"insecure_desktop_app",
"is_spectator",
@@ -581,7 +581,7 @@ class HomeTest(ZulipTestCase):
# Verify number of queries for Realm admin isn't much higher than for normal users.
self.login("iago")
with (
- self.assert_database_query_count(59),
+ self.assert_database_query_count(54),
patch("zerver.lib.cache.cache_set") as cache_mock,
):
result = self._get_home_page()
@@ -613,7 +613,7 @@ class HomeTest(ZulipTestCase):
self._get_home_page()
# Then for the second page load, measure the number of queries.
- with self.assert_database_query_count(54):
+ with self.assert_database_query_count(49):
result = self._get_home_page()
# Do a sanity check that our new streams were in the payload.