Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
bots: Remove duplicate fields from bot objects in realm_bots.
Sahil Batra32c1c4bb76a8
diff --git a/api_docs/unmerged.d/ZF-1ba2c3.md b/api_docs/unmerged.d/ZF-1ba2c3.mdindex 00c84e247b1..1eac821d581 100644--- a/api_docs/unmerged.d/ZF-1ba2c3.md+++ b/api_docs/unmerged.d/ZF-1ba2c3.md@@ -2,3 +2,9 @@Removed `api_key` field from bot objects.* [`GET /events`](/api/get-events): `realm_bot/update` event is no longersent when a bot's api key is regenerated.+* [`GET /events`](/api/get-events), [`POST /register`](/api/register-queue):+ Removed `avatar_url`, `bot_type`, `email`, `full_name`, `is_active` and+ `owner_id` fields from bot objects.+* [`GET /events`](/api/get-events): `realm_bot/update` event is no longer+ sent when updating a bot's avatar, email, name, or owner and also when+ deactivating or reactivating a bot.diff --git a/web/src/bot_data.ts b/web/src/bot_data.tsindex 266835b5233..9343e216f66 100644--- a/web/src/bot_data.ts+++ b/web/src/bot_data.ts@@ -1,13 +1,14 @@+import assert from "minimalistic-assert";import type * as z from "zod/mini";-import type {services_schema} from "./bot_types.ts";-import {server_add_bot_schema, server_update_bot_schema} from "./bot_types.ts";+import type {bot_schema, services_schema} from "./bot_types.ts";+import {server_add_bot_schema, update_bot_schema} from "./bot_types.ts";import * as people from "./people.ts";import type {StateData} from "./state_data.ts";-export type ServerUpdateBotData = z.infer<typeof server_update_bot_schema>;+export type UpdateBotData = z.infer<typeof update_bot_schema>;export type ServerAddBotData = z.infer<typeof server_add_bot_schema>;-export type Bot = Omit<ServerAddBotData, "services">;+export type Bot = z.infer<typeof bot_schema>;export type Services = z.infer<typeof services_schema>;@@ -22,7 +23,18 @@ export function add(bot_data: ServerAddBotData): void {// TODO/typescript: Move validation to the caller when// server_events_dispatch.js is converted to TypeScript.const {services: bot_services, ...clean_bot} = server_add_bot_schema.parse(bot_data);- bots.set(clean_bot.user_id, clean_bot);+ const user = people.get_by_user_id(clean_bot.user_id);+ const is_active = people.is_person_active(clean_bot.user_id);+ assert(user.is_bot);+ const bot = {+ bot_type: user.bot_type,+ email: user.email,+ full_name: user.full_name,+ is_active,+ owner_id: user.bot_owner_id,+ ...clean_bot,+ };+ bots.set(clean_bot.user_id, bot);services.set(clean_bot.user_id, bot_services);}@@ -32,12 +44,11 @@ export function del(bot_id: number): void {services.delete(bot_id);}-export function update(bot_id: number, bot_update: ServerUpdateBotData): void {+export function update(bot_id: number, bot_update: UpdateBotData): void {const bot = bots.get(bot_id)!;// TODO/typescript: Move validation to the caller when// server_events_dispatch.js is converted to TypeScript.- const {services: services_update, ...bot_update_rest} =- server_update_bot_schema.parse(bot_update);+ const {services: services_update, ...bot_update_rest} = update_bot_schema.parse(bot_update);Object.assign(bot, bot_update_rest);diff --git a/web/src/bot_types.ts b/web/src/bot_types.tsindex 89d656f281a..91fbff42d8f 100644--- a/web/src/bot_types.ts+++ b/web/src/bot_types.ts@@ -1,15 +1,9 @@import * as z from "zod/mini";const basic_bot_schema = z.object({- avatar_url: z.string(),- bot_type: z.number(),default_all_public_streams: z.boolean(),default_events_register_stream: z.nullable(z.string()),default_sending_stream: z.nullable(z.string()),- email: z.string(),- full_name: z.string(),- is_active: z.boolean(),- owner_id: z.nullable(z.number()),user_id: z.number(),});@@ -29,16 +23,29 @@ export const services_schema = z.union([z.array(embedded_service_schema),]);-export const server_update_bot_schema = z.object({+export const update_bot_schema = z.object({...z.partial(basic_bot_schema).shape,user_id: z.number(),services: z.optional(services_schema),+ // Server does not send realm_bot/update events for+ // the below fields. We instead update these fields+ // based on the realm_user/update events.+ email: z.optional(z.string()),+ full_name: z.optional(z.string()),+ is_active: z.optional(z.boolean()),+ owner_id: z.optional(z.nullable(z.number())),});export const server_add_bot_schema = z.object({+ ...basic_bot_schema.shape,+ services: services_schema,+});++export const bot_schema = z.object({...basic_bot_schema.shape,bot_type: z.number(),email: z.string(),+ full_name: z.string(),is_active: z.boolean(),- services: services_schema,+ owner_id: z.nullable(z.number()),});diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.jsindex 033ae7384b5..8e25102bdd9 100644--- a/web/src/server_events_dispatch.js+++ b/web/src/server_events_dispatch.js@@ -490,13 +490,15 @@ export function dispatch_normal_event(event) {case "realm_bot":switch (event.op) {- case "add":+ case "add": {bot_data.add(event.bot);- if (event.bot.owner_id === current_user.user_id) {+ const user = people.get_by_user_id(event.bot.user_id);+ if (user.bot_owner_id === current_user.user_id) {settings_bots.redraw_your_bots_list();settings_bots.toggle_bot_config_download_container();}break;+ }case "delete":bot_data.del(event.bot.user_id);settings_bots.redraw_your_bots_list();@@ -504,13 +506,6 @@ export function dispatch_normal_event(event) {break;case "update":bot_data.update(event.bot.user_id, event.bot);- if ("owner_id" in event.bot) {- settings_bots.redraw_your_bots_list();- settings_bots.toggle_bot_config_download_container();- }- if ("is_active" in event.bot) {- settings_bots.toggle_bot_config_download_container();- }break;default:blueslip.error("Unexpected event type realm_bot/" + event.op);diff --git a/web/src/user_events.ts b/web/src/user_events.tsindex f9aa6b26279..78325c0131f 100644--- a/web/src/user_events.ts+++ b/web/src/user_events.ts@@ -8,6 +8,7 @@ import * as z from "zod/mini";import * as activity_ui from "./activity_ui.ts";import * as blueslip from "./blueslip.ts";+import * as bot_data from "./bot_data.ts";import {buddy_list} from "./buddy_list.ts";import * as compose_pm_pill from "./compose_pm_pill.ts";import * as compose_state from "./compose_state.ts";@@ -89,6 +90,11 @@ export const update_person = function update(event: UserUpdate): void {current_user.delivery_email = delivery_email;settings_account.hide_confirm_email_banner();}++ if (user.is_bot) {+ assert(delivery_email !== null);+ bot_data.update(event.user_id, {user_id: event.user_id, email: delivery_email});+ }}if ("full_name" in event) {@@ -103,6 +109,9 @@ export const update_person = function update(event: UserUpdate): void {current_user.full_name = event.full_name;settings_account.update_full_name(event.full_name);}+ if (user.is_bot) {+ bot_data.update(event.user_id, {user_id: event.user_id, full_name: event.full_name});+ }}if ("role" in event) {@@ -224,10 +233,16 @@ export const update_person = function update(event: UserUpdate): void {assert(user.is_bot);user.bot_owner_id = event.bot_owner_id;user_profile.update_profile_modal_ui(user, event);+ bot_data.update(event.user_id, {user_id: event.user_id, owner_id: event.bot_owner_id});+ settings_bots.redraw_your_bots_list();+ settings_bots.toggle_bot_config_download_container();}if ("is_active" in event) {const is_bot_user = user.is_bot;+ if (is_bot_user) {+ bot_data.update(event.user_id, {user_id: event.user_id, is_active: event.is_active});+ }if (event.is_active) {people.add_active_user(user);settings_users.update_view_on_reactivate(event.user_id, is_bot_user);@@ -248,6 +263,7 @@ export const update_person = function update(event: UserUpdate): void {settings_account.maybe_update_deactivate_account_button();if (is_bot_user) {settings_bots.update_bot_data(event.user_id);+ settings_bots.toggle_bot_config_download_container();} 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/bot_data.test.cjs b/web/tests/bot_data.test.cjsindex a21092ce40b..49a89c3af10 100644--- a/web/tests/bot_data.test.cjs+++ b/web/tests/bot_data.test.cjs@@ -26,32 +26,82 @@ const fred = make_user({user_id: 3,});+const bot_0_user = make_user({+ email: "[email protected]",+ full_name: "Bot 0",+ user_id: 42,+ is_bot: true,+ bot_type: 1, // DEFAULT_BOT+ bot_owner_id: 4,+});+const webhook_bot_user = make_user({+ email: "[email protected]",+ full_name: "Outgoing webhook",+ user_id: 314,+ is_bot: true,+ bot_type: 3, // OUTGOING_WEBHOOK_BOT+ bot_owner_id: 5,+});+const test_bot_user = make_user({+ email: "[email protected]",+ full_name: "Bot 1",+ user_id: 43,+ // Default bot+ bot_type: 1,+ is_bot: true,+ bot_owner_id: 6,+});+const test_embedded_bot_user = make_user({+ email: "[email protected]",+ full_name: "Embedded bot 1",+ user_id: 143,+ is_bot: true,+ bot_type: 4, // EMBEDDED_BOT+ bot_owner_id: 7,+});++const test_bot_1_user = make_user({+ email: "[email protected]",+ full_name: "Bot 1",+ user_id: 44,+ // Default bot+ bot_type: 1,+ is_bot: true,+ bot_owner_id: me.user_id,+});+const test_bot_2_user = make_user({+ email: "[email protected]",+ full_name: "Bot 1",+ user_id: 45,+ // Default bot+ bot_type: 1,+ is_bot: true,+ bot_owner_id: me.user_id,+});+const test_bot_3_user = make_user({+ email: "[email protected]",+ full_name: "Bot 1",+ user_id: 46,+ // Default bot+ bot_type: 1,+ is_bot: true,+ bot_owner_id: fred.user_id,+});+const bot_data_params = {realm_bots: [{- avatar_url: "",- bot_type: 1, // DEFAULT_BOTdefault_all_public_streams: true,default_events_register_stream: "register stream 42",default_sending_stream: "sending stream 42",- email: "[email protected]",- full_name: "Bot 0",- is_active: true,- owner_id: 4,user_id: 42,services: [],extra: "This field should be ignored",},{- avatar_url: "",- bot_type: 3, // OUTGOING_WEBHOOK_BOTdefault_all_public_streams: true,default_events_register_stream: "register stream 314",default_sending_stream: "sending stream 314",- email: "[email protected]",- full_name: "Outgoing webhook",- is_active: true,- owner_id: 5,user_id: 314,services: [{base_url: "http://foo.com", interface: 1, token: "basictoken12345"}],extra: "This field should be ignored",@@ -62,6 +112,13 @@ const bot_data_params = {function test(label, f) {run_test(label, ({override}) => {people.add_active_user(me);+ people.add_active_user(bot_0_user);+ people.add_active_user(webhook_bot_user);+ people.add_active_user(test_bot_user);+ people.add_active_user(test_embedded_bot_user);+ people.add_active_user(test_bot_1_user);+ people.add_active_user(test_bot_2_user);+ people.add_active_user(test_bot_3_user);people.initialize_current_user(me.user_id);bot_data.initialize(bot_data_params);// Our startup logic should have added Bot 0 from page_params.@@ -74,30 +131,17 @@ function test(label, f) {test("test_basics", () => {people.add_active_user(fred);const test_bot = {- avatar_url: "",- // Default bot- bot_type: 1,default_all_public_streams: true,default_events_register_stream: "register stream 43",default_sending_stream: "sending stream 43",- email: "[email protected]",- full_name: "Bot 1",- is_active: true,- owner_id: 6,user_id: 43,services: [],extra: "This field should be ignored",};const test_embedded_bot = {- avatar_url: "",- bot_type: 4, // EMBEDDED_BOTdefault_all_public_streams: true,default_events_register_stream: "register stream 143",default_sending_stream: "sending stream 143",- email: "[email protected]",- full_name: "Embedded bot 1",- is_active: true,- owner_id: 7,user_id: 143,services: [{@@ -111,7 +155,6 @@ test("test_basics", () => {(function test_add() {bot_data.add(test_bot);const bot = bot_data.get(43);- assert.equal("", bot.avatar_url);assert.equal(1, bot.bot_type);assert.equal(true, bot.default_all_public_streams);assert.equal("register stream 43", bot.default_events_register_stream);@@ -124,33 +167,6 @@ test("test_basics", () => {assert.equal(undefined, bot.extra);})();- (function test_update() {- bot_data.add(test_bot);-- let bot = bot_data.get(43);- assert.equal("Bot 1", bot.full_name);- bot_data.update(43, {- ...test_bot,- full_name: "New Bot 1",- });- bot = bot_data.get(43);- assert.equal("New Bot 1", bot.full_name);-- const change_owner_event = {- owner_id: fred.user_id,- };- bot_data.update(43, {...test_bot, ...change_owner_event});-- bot = bot_data.get(43);- assert.equal(bot.owner_id, fred.user_id);-- bot_data.update(43, {...test_bot, is_active: false});- assert.ok(!bot_data.get(43).is_active);-- bot_data.update(43, {...test_bot, is_active: true});- assert.ok(bot_data.get(43).is_active);- })();-(function test_embedded_bot_update() {bot_data.add(test_embedded_bot);const bot_id = 143;@@ -215,52 +231,23 @@ test("test_basics", () => {});test("get_all_bots_ids_for_current_user", () => {+ // Ensure bots owned by others are not includedbot_data.add({- avatar_url: "",- bot_type: 1,default_all_public_streams: true,default_events_register_stream: "register stream test",default_sending_stream: "sending stream test",- email: "[email protected]",- full_name: "Test Bot",- is_active: true,- owner_id: me.user_id,- user_id: 101,+ user_id: 44,services: [],});bot_data.add({- avatar_url: "",- bot_type: 1,default_all_public_streams: true,default_events_register_stream: "register stream another",default_sending_stream: "sending stream another",- email: "[email protected]",- full_name: "Another Bot",- is_active: true,- owner_id: fred.user_id,- user_id: 102,+ user_id: 46,services: [],});const my_bot_ids = bot_data.get_all_bots_ids_for_current_user();- assert.deepEqual(my_bot_ids, [101]);-- // Ensure bots owned by others are not included- bot_data.add({- avatar_url: "",- bot_type: 1,- default_all_public_streams: true,- default_events_register_stream: "register stream extra",- default_sending_stream: "sending stream extra",- email: "[email protected]",- full_name: "Extra Bot",- is_active: true,- owner_id: fred.user_id,- user_id: 103,- services: [],- });-- const my_updated_bot_ids = bot_data.get_all_bots_ids_for_current_user();- assert.deepEqual(my_updated_bot_ids, [101]);+ assert.deepEqual(my_bot_ids, [44]);});diff --git a/web/tests/dispatch.test.cjs b/web/tests/dispatch.test.cjsindex e7050c03a5a..5e9cc7dfdfc 100644--- a/web/tests/dispatch.test.cjs+++ b/web/tests/dispatch.test.cjs@@ -181,6 +181,16 @@ people.add_active_user(me);people.add_active_user(test_user);people.initialize_current_user(me.user_id);+const bot_user = {+ email: "[email protected]",+ user_id: 42,+ full_name: "The Bot",+ bot_type: 1,+ is_active: true,+ bot_owner_id: test_user.user_id,+};+people.add_active_user(bot_user);+message_store.update_message_cache({type: "server_message",message: test_message,@@ -861,45 +871,16 @@ run_test("realm_bot delete", ({override}) => {});run_test("realm_bot update", ({override}) => {- let event = event_fixtures.realm_bot__update;- let bot_stub = make_stub();+ const event = event_fixtures.realm_bot__update;+ const bot_stub = make_stub();override(bot_data, "update", bot_stub.f);dispatch(event);assert.equal(bot_stub.num_calls, 1);- let args = bot_stub.get_args("user_id", "bot");- assert_same(args.user_id, event.bot.user_id);- assert_same(args.bot, event.bot);-- bot_stub = make_stub();- override(bot_data, "update", bot_stub.f);- let toggle_download_container_stub = make_stub();- override(- settings_bots,- "toggle_bot_config_download_container",- toggle_download_container_stub.f,- );-- event = event_fixtures.realm_bot__update_owner;- 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);- args = bot_stub.get_args("user_id", "bot");+ const 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_bots,- "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/example4.test.cjs b/web/tests/example4.test.cjsindex 8318bf805be..976b8da76da 100644--- a/web/tests/example4.test.cjs+++ b/web/tests/example4.test.cjs@@ -65,6 +65,7 @@ const settings_users = mock_esm("../src/settings_users");const user_profile = mock_esm("../src/user_profile");// Use real versions of these modules.+const bot_data = zrequire("bot_data");const people = zrequire("people");const server_events_dispatch = zrequire("server_events_dispatch");const {set_current_user} = zrequire("state_data");@@ -120,6 +121,13 @@ run_test("add users with event", ({override}) => {run_test("update user with event", ({override}) => {people.init();people.add_active_user(bob);+ bot_data.add({+ default_all_public_streams: true,+ default_events_register_stream: "register stream test",+ default_sending_stream: "sending stream test",+ user_id: bob.user_id,+ services: [],+ });set_current_user({user_id: bob.user_id});diff --git a/web/tests/lib/events.cjs b/web/tests/lib/events.cjsindex fb6a80e8f10..b34c559d605 100644--- a/web/tests/lib/events.cjs+++ b/web/tests/lib/events.cjs@@ -524,16 +524,10 @@ exports.fixtures = {type: "realm_bot",op: "add",bot: {- email: "[email protected]",user_id: 42,- avatar_url: "/avatar/42",- full_name: "The Bot",- bot_type: 1,default_all_public_streams: true,default_events_register_stream: "whatever",default_sending_stream: "whatever",- is_active: true,- owner_id: test_user.user_id,services: [],},},@@ -551,25 +545,7 @@ exports.fixtures = {op: "update",bot: {user_id: 4321,- full_name: "The Bot Has A New Name",- },- },-- 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",- bot: {- user_id: 4321,- owner_id: test_user.user_id,+ default_sending_stream: "new-stream",},},diff --git a/web/tests/lib/example_user.cjs b/web/tests/lib/example_user.cjsindex 1dd5b268f4c..161db60fddb 100644--- a/web/tests/lib/example_user.cjs+++ b/web/tests/lib/example_user.cjs@@ -48,7 +48,7 @@ const bot_or_user_props = (opts = {}) => {const make_user = (opts = {}) => ({...bot_or_user_props(opts),- is_bot: false,+ is_bot: opts.is_bot ?? false,// By default an empty dictionary.profile_data: opts.profile_data ?? {},});diff --git a/web/tests/settings_bots.test.cjs b/web/tests/settings_bots.test.cjsindex 410d56f7566..e341705c96a 100644--- a/web/tests/settings_bots.test.cjs+++ b/web/tests/settings_bots.test.cjs@@ -3,23 +3,33 @@const assert = require("node:assert/strict");const {make_realm} = require("./lib/example_realm.cjs");+const {make_user} = require("./lib/example_user.cjs");const {zrequire} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");+const bot_data = zrequire("bot_data");+const bot_helper = zrequire("bot_helper");+const people = zrequire("people");+const settings_bots = zrequire("settings_bots");+const {set_current_user, set_realm} = zrequire("state_data");++const bot_user = make_user({+ email: "[email protected]",+ full_name: "Error bot",+ user_id: 1,+ is_bot: true,+ bot_type: 1, // DEFAULT_BOT+ owner_id: 4,+});+people.add_active_user(bot_user);+const bot_api_key = "QadL788EkiottHmukyhHgePUFHREiu8b";const bot_data_params = {realm_bots: [{- avatar_url: "",- bot_type: 1, // DEFAULT_BOTdefault_all_public_streams: true,default_events_register_stream: "register stream 1",default_sending_stream: "sending stream 1",- email: "[email protected]",- full_name: "Error bot",- is_active: true,- owner: "someone 4",- owner_id: 4,user_id: 1,services: [],extra: "This field should be ignored",@@ -27,11 +37,6 @@ 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");-const current_user = {};set_current_user(current_user);const realm = make_realm();diff --git a/web/tests/user_events.test.cjs b/web/tests/user_events.test.cjsindex 3ea85dadd68..bd79df791f1 100644--- a/web/tests/user_events.test.cjs+++ b/web/tests/user_events.test.cjs@@ -20,7 +20,10 @@ const settings_account = mock_esm("../src/settings_account", {add_or_remove_owner_from_role_dropdown() {},update_user_own_role_dropdown_state() {},});-const settings_bots = mock_esm("../src/settings_bots");+const settings_bots = mock_esm("../src/settings_bots", {+ redraw_your_bots_list() {},+ toggle_bot_config_download_container() {},+});mock_esm("../src/settings_users", {update_user_data() {},update_view_on_deactivate() {},@@ -73,6 +76,7 @@ mock_esm("../src/settings_streams", {maybe_disable_widgets() {},});+const bot_data = zrequire("bot_data");const people = zrequire("people");const settings_config = zrequire("settings_config");const {set_current_user, set_realm} = zrequire("state_data");@@ -273,6 +277,13 @@ run_test("updates", ({override}) => {bot_owner_id: isaac.id,});people.add_active_user(test_bot);+ bot_data.add({+ default_all_public_streams: true,+ default_events_register_stream: "register stream test",+ default_sending_stream: "sending stream test",+ user_id: 35,+ services: [],+ });user_events.update_person({user_id: test_bot.user_id, bot_owner_id: me.user_id});person = people.get_by_email(test_bot.email);diff --git a/zerver/actions/bots.py b/zerver/actions/bots.pyindex ca2239af2ff..5836b6aa8d1 100644--- a/zerver/actions/bots.py+++ b/zerver/actions/bots.py@@ -11,8 +11,6 @@ from zerver.tornado.django_api import send_event_on_commitdef send_bot_owner_update_events(user_profile: UserProfile, bot_owner: UserProfile, previous_owner: UserProfile | None) -> None:- update_users = bot_owner_user_ids(user_profile)-# For admins, update event is sent instead of delete/add# event. bot_data of admin contains all the# bots and none of them should be removed/(added again).@@ -32,29 +30,11 @@ def send_bot_owner_update_events(delete_event,{previous_owner_id},)- # Do not send update event for previous bot owner.- update_users.discard(previous_owner.id)# Notify the new owner that the bot has been added.if not bot_owner.is_realm_admin:add_event = created_bot_event(user_profile)send_event_on_commit(user_profile.realm, add_event, {bot_owner.id})- # Do not send update event for bot_owner.- update_users.discard(bot_owner.id)-- bot_event = dict(- type="realm_bot",- op="update",- bot=dict(- user_id=user_profile.id,- owner_id=bot_owner.id,- ),- )- send_event_on_commit(- user_profile.realm,- bot_event,- update_users,- )# Since `bot_owner_id` is included in the user profile dict we need# to update the users dict with the new bot owner iddiff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.pyindex 649dc90fc9c..e26eaeab21c 100644--- a/zerver/actions/create_user.py+++ b/zerver/actions/create_user.py@@ -26,7 +26,7 @@ from zerver.actions.users import (get_service_dicts_for_bot,send_update_events_for_anonymous_group_settings,)-from zerver.lib.avatar import avatar_url, generate_and_upload_jdenticon_avatar+from zerver.lib.avatar import generate_and_upload_jdenticon_avatarfrom zerver.lib.create_user import create_userfrom zerver.lib.default_streams import get_slim_realm_default_streamsfrom zerver.lib.email_notifications import enqueue_welcome_emails, send_account_registered_email@@ -482,24 +482,13 @@ def created_bot_event(user_profile: UserProfile) -> dict[str, Any]:default_events_register_stream_name = stream_name(user_profile.default_events_register_stream)bot = dict(- email=user_profile.email,user_id=user_profile.id,- full_name=user_profile.full_name,- bot_type=user_profile.bot_type,- is_active=user_profile.is_active,default_sending_stream=default_sending_stream_name,default_events_register_stream=default_events_register_stream_name,default_all_public_streams=user_profile.default_all_public_streams,- avatar_url=avatar_url(user_profile),services=get_service_dicts_for_bot(user_profile.id),)- # Set the owner key only when the bot has an owner.- # The default bots don't have an owner. So don't- # set the owner key while reactivating them.- if user_profile.bot_owner_id is not None:- bot["owner_id"] = user_profile.bot_owner_id-return dict(type="realm_bot", op="add", bot=bot)@@ -781,22 +770,11 @@ def do_reactivate_user(user_profile: UserProfile, *, acting_user: UserProfile |list(user_profile.realm.get_human_admin_users().values_list("id", flat=True)),)- if user_profile.is_bot:- event = dict(- type="realm_bot",- op="update",- bot=dict(- user_id=user_profile.id,- is_active=True,- ),- )- send_event_on_commit(user_profile.realm, event, bot_owner_user_ids(user_profile))-- if bot_owner_changed:- from zerver.actions.bots import send_bot_owner_update_events+ if user_profile.is_bot and bot_owner_changed:+ from zerver.actions.bots import send_bot_owner_update_events- assert acting_user is not None- send_bot_owner_update_events(user_profile, acting_user, previous_owner)+ assert acting_user is not None+ send_bot_owner_update_events(user_profile, acting_user, previous_owner)subscribed_recipient_ids = Subscription.objects.filter(user_profile_id=user_profile.id, active=True, recipient__type=Recipient.STREAMdiff --git a/zerver/actions/user_settings.py b/zerver/actions/user_settings.pyindex ae5e8531a94..d8a90d75880 100644--- a/zerver/actions/user_settings.py+++ b/zerver/actions/user_settings.py@@ -46,7 +46,7 @@ from zerver.models import ()from zerver.models.clients import get_clientfrom zerver.models.realm_audit_logs import AuditLogEventType-from zerver.models.users import bot_owner_user_ids, get_user_profile_by_id+from zerver.models.users import get_user_profile_by_idfrom zerver.tornado.django_api import send_event_on_commit@@ -134,15 +134,6 @@ def do_change_user_delivery_email(send_event_on_commit(user_profile.realm, event, delivery_email_visible_user_ids)- if user_profile.is_bot:- bot_payload = dict(user_id=user_profile.id, email=new_email)- bot_event = dict(type="realm_bot", op="update", bot=bot_payload)- send_event_on_commit(- user_profile.realm,- bot_event,- bot_owner_user_ids(user_profile),- )-if user_profile.avatar_source == UserProfile.AVATAR_FROM_GRAVATAR:# If the user is using Gravatar to manage their email address,# their Gravatar just changed, and we need to notify other@@ -257,12 +248,6 @@ def do_change_full_name(dict(type="realm_user", op="update", person=payload),get_user_ids_who_can_access_user(user_profile),)- if user_profile.is_bot:- send_event_on_commit(- user_profile.realm,- dict(type="realm_bot", op="update", bot=payload),- bot_owner_user_ids(user_profile),- )if notify:changes: list[UserProfileChangeDict] = [@@ -364,21 +349,6 @@ def bulk_regenerate_api_keys(user_profile_ids: Iterable[int]) -> None:def notify_avatar_url_change(user_profile: UserProfile) -> None:- if user_profile.is_bot:- bot_event = dict(- type="realm_bot",- op="update",- bot=dict(- user_id=user_profile.id,- avatar_url=avatar_url(user_profile),- ),- )- send_event_on_commit(- user_profile.realm,- bot_event,- bot_owner_user_ids(user_profile),- )-payload = dict(avatar_source=user_profile.avatar_source,avatar_url=avatar_url(user_profile),diff --git a/zerver/actions/users.py b/zerver/actions/users.pyindex 415eed64480..aabdfaa67b5 100644--- a/zerver/actions/users.py+++ b/zerver/actions/users.py@@ -25,7 +25,6 @@ from zerver.actions.user_groups import (update_users_in_full_members_system_group,)from zerver.actions.user_settings import do_change_avatar_fields, do_change_full_name-from zerver.lib.avatar import get_avatar_fieldfrom zerver.lib.bot_config import ConfigError, get_bot_config, get_bot_configs, set_bot_configfrom zerver.lib.cache import bot_dict_fields, flush_user_profilefrom zerver.lib.create_user import create_user_profile@@ -548,16 +547,6 @@ def do_deactivate_user(send_events_for_user_deactivation(user_profile)- if user_profile.is_bot:- event_deactivate_bot = dict(- type="realm_bot",- op="update",- bot=dict(user_id=user_profile.id, is_active=False),- )- send_event_on_commit(- user_profile.realm, event_deactivate_bot, bot_owner_user_ids(user_profile)- )-def send_stream_events_for_role_update(user_profile: UserProfile, old_accessible_streams: list[Stream]@@ -931,24 +920,10 @@ def get_owned_bot_dicts(services_by_ids = get_service_dicts_for_bots(result, user_profile.realm)return [{- "email": botdict["email"],"user_id": botdict["id"],- "full_name": botdict["full_name"],- "bot_type": botdict["bot_type"],- "is_active": botdict["is_active"],"default_sending_stream": botdict["default_sending_stream__name"],"default_events_register_stream": botdict["default_events_register_stream__name"],"default_all_public_streams": botdict["default_all_public_streams"],- "owner_id": botdict["bot_owner_id"],- "avatar_url": get_avatar_field(- user_id=botdict["id"],- realm_id=botdict["realm_id"],- email=botdict["email"],- avatar_source=botdict["avatar_source"],- avatar_version=botdict["avatar_version"],- medium=False,- client_gravatar=False,- ),"services": services_by_ids[botdict["id"]],}for botdict in resultdiff --git a/zerver/lib/event_schema.py b/zerver/lib/event_schema.pyindex b03bc0fad78..6fce093cdfa 100644--- a/zerver/lib/event_schema.py+++ b/zerver/lib/event_schema.py@@ -374,11 +374,11 @@ def check_modern_presence(var_name: str, event: dict[str, object], user_id: int)def check_realm_bot_add(var_name: str,event: dict[str, object],+ bot_type: int,) -> None:_check_realm_bot_add(var_name, event)assert isinstance(event["bot"], dict)- bot_type = event["bot"]["bot_type"]services = event["bot"]["services"]diff --git a/zerver/lib/event_types.py b/zerver/lib/event_types.pyindex 530d14417ed..beb9f0dc066 100644--- a/zerver/lib/event_types.py+++ b/zerver/lib/event_types.py@@ -411,15 +411,9 @@ class BotServicesEmbedded(BaseModel):class Bot(BaseModel):user_id: int- avatar_url: str- bot_type: intdefault_all_public_streams: booldefault_events_register_stream: str | Nonedefault_sending_stream: str | None- email: str- full_name: str- is_active: bool- owner_id: intservices: list[BotServicesOutgoing | BotServicesEmbedded]@@ -445,13 +439,9 @@ class BotTypeForUpdateCore(BaseModel):class BotTypeForUpdate(BotTypeForUpdateCore):# TODO: fix types to avoid optional fields- avatar_url: str | None = Nonedefault_all_public_streams: bool | None = Nonedefault_events_register_stream: str | None = Nonedefault_sending_stream: str | None = None- full_name: str | None = None- is_active: bool | None = None- owner_id: int | None = Noneservices: list[BotServicesOutgoing | BotServicesEmbedded] | None = Nonediff --git a/zerver/lib/events.py b/zerver/lib/events.pyindex 437c3c40123..e1d7038237f 100644--- a/zerver/lib/events.py+++ b/zerver/lib/events.py@@ -1365,11 +1365,7 @@ def apply_event(elif event["op"] == "update":for bot in state["realm_bots"]:if bot["user_id"] == event["bot"]["user_id"]:- if "owner_id" in event["bot"]:- bot_owner_id = event["bot"]["owner_id"]- bot["owner_id"] = bot_owner_id- else:- bot.update(event["bot"])+ bot.update(event["bot"])else:raise AssertionError("Unexpected event type {type}/{op}".format(**event))elif event["type"] == "stream":diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 3593801b29f..493258ddab1 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -4387,17 +4387,11 @@ paths:"op": "add","bot":{- "email": "[email protected]","user_id": 36,- "full_name": "Foo Bot",- "bot_type": 1,- "is_active": true,"default_sending_stream": null,"default_events_register_stream": null,"default_all_public_streams": false,- "avatar_url": "https://secure.gravatar.com/avatar/af8abc2537d283b212a6bd4d1289956d?d=identicon&version=1","services": [],- "owner_id": 10,},"id": 1,}@@ -4414,9 +4408,13 @@ paths:Only organization administrators and the user who owns the bot willreceive this event.- **Changes**: Starting from Zulip 12.0 (feature level ZF-1ba2c3), this- event is no longer sent when a bot's API key is regenerated. Clients- now use [`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key) to get+ **Changes**: Starting from Zulip 12.0 (feature level ZF-1ba2c3),+ this event is not sent when updating bot's avatar, email, name or+ owner and also when reactivating or deactivating a bot.++ Starting from Zulip 12.0 (feature level ZF-1ba2c3), this event is+ no longer sent when a bot's API key is regenerated. Clients now+ use [`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key) to getapi key for the bot.additionalProperties: falseproperties:@@ -17689,8 +17687,12 @@ paths:it will only include bots owned by the user (either because the user createdthe bot or an administrator transferred the bot's ownership to the user).- **Changes**: Removed `api_key` field from the dictionary in Zulip 12.0- (feature level ZF-1ba2c3). Clients now use [`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key)+ **Changes**: Removed `avatar_url`, `bot_type`, `email`, `full_name`, `is_active`+ and `owner_id` fields from the dictionary in Zulip 12.0 (feature level ZF-1ba2c3).+ Clients can get all these data from the corresponding user object.++ Removed `api_key` field from the dictionary in Zulip 12.0 (feature level ZF-1ba2c3).+ Clients now use [`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key)to get api key for the bot.realm_embedded_bots:type: array@@ -26269,32 +26271,12 @@ components:- additionalProperties: falseproperties:user_id: {}- full_name: {}default_sending_stream:nullable: truedefault_events_register_stream:nullable: truedefault_all_public_streams: {}- avatar_url: {}- owner_id:- nullable: trueservices: {}- email:- type: string- description: |- The Zulip display email address of the bot, used for authentication.-- **Changes**: New in Zulip 12.0 (feature level 461). Previously,- only `realm_user` events were sent when updating a bot’s email.- is_active:- type: boolean- description: |- A boolean describing whether the user account has been deactivated.-- **Changes**: New in Zulip 8.0 (feature level 222). Previously- we sent `realm_user` event with `op` field set to `remove`- when deactivating a bot and `realm_user` event with `op`- field set to `add` when reactivating a bot.BasicBotBase:type: objectproperties:@@ -26302,10 +26284,6 @@ components:type: integerdescription: |The user ID of the bot.- full_name:- type: string- description: |- The full name of the bot.default_sending_stream:type: stringnullable: true@@ -26322,17 +26300,6 @@ components:type: booleandescription: |Whether the bot can send messages to all channels by default.- avatar_url:- type: string- description: |- The URL of the bot's avatar.- owner_id:- type: integer- nullable: true- description: |- The user ID of the bot's owner.-- If `null`, the bot has no owner.services:type: arraydescription: |@@ -26385,41 +26352,24 @@ components:- description: |Object containing details of a bot.- **Changes**: Removed `api_key` field from the dictionary in+ **Changes**: Removed `avatar_url`, `bot_type`, `email`,+ `full_name`, `is_active` and `owner_id` fields from the+ dictionary in Zulip 12.0 (feature level ZF-1ba2c3). Clients+ can get all these data from the corresponding user object.++ Removed `api_key` field from the dictionary inZulip 12.0 (feature level ZF-1ba2c3). Clients now use[`GET /bots/{bot_id}/api_key`](/api/get-bot-api-key) to getapi key for the bot.additionalProperties: falseproperties:user_id: {}- full_name: {}default_sending_stream:nullable: truedefault_events_register_stream:nullable: truedefault_all_public_streams: {}- avatar_url: {}- owner_id:- nullable: trueservices: {}- email:- type: string- description: |- The email of the bot.- bot_type:- type: integer- nullable: true- description: |- An integer describing the type of bot:-- - `1` for a `Generic` bot.- - `2` for an `Incoming webhook` bot.- - `3` for an `Outgoing webhook` bot.- - `4` for an `Embedded` bot.- is_active:- type: boolean- description: |- A boolean describing whether the user account has been deactivated.BotConfiguration:type: objectdescription: |diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 4b0e2b4c0d2..0d7bf36f034 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -168,7 +168,6 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_json_success(users_result)def test_add_bot(self) -> None:- hamlet = self.example_user("hamlet")self.login("hamlet")self.assert_num_bots_equal(0)with self.capture_send_event_calls(expected_num_events=4) as events:@@ -186,17 +185,11 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):type="realm_bot",op="add",bot=dict(- email="[email protected]",user_id=bot.id,- bot_type=bot.bot_type,- full_name="The Bot of Hamlet",- is_active=True,- avatar_url=result["avatar_url"],default_sending_stream=None,default_events_register_stream=None,default_all_public_streams=False,services=[],- owner_id=hamlet.id,),),event["event"],@@ -337,7 +330,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.login_user(user)self.assert_num_bots_equal(0)with self.capture_send_event_calls(expected_num_events=4) as events:- result = self.create_bot()+ self.create_bot()self.assert_num_bots_equal(1)email = "[email protected]"@@ -349,17 +342,11 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):type="realm_bot",op="add",bot=dict(- email="[email protected]",user_id=bot.id,- bot_type=bot.bot_type,- full_name="The Bot of Hamlet",- is_active=True,- avatar_url=result["avatar_url"],default_sending_stream=None,default_events_register_stream=None,default_all_public_streams=False,services=[],- owner_id=user.id,),),event["event"],@@ -447,17 +434,11 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):type="realm_bot",op="add",bot=dict(- email="[email protected]",user_id=profile.id,- full_name="The Bot of Hamlet",- bot_type=profile.bot_type,- is_active=True,- avatar_url=result["avatar_url"],default_sending_stream="Denmark",default_events_register_stream=None,default_all_public_streams=False,services=[],- owner_id=user_profile.id,),),event["event"],@@ -529,17 +510,11 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):type="realm_bot",op="add",bot=dict(- email="[email protected]",- full_name="The Bot of Hamlet",user_id=bot_profile.id,- bot_type=bot_profile.bot_type,- is_active=True,- avatar_url=result["avatar_url"],default_sending_stream=None,default_events_register_stream="Denmark",default_all_public_streams=False,services=[],- owner_id=user_profile.id,),),event["event"],diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.pyindex 4485b7be475..d36b52da255 100644--- a/zerver/tests/test_events.py+++ b/zerver/tests/test_events.py@@ -525,7 +525,7 @@ class BaseAction(ZulipTestCase):# this isn't guaranteed to matchdel stream["subscriber_count"]if "realm_bots" in state:- state["realm_bots"] = {u["email"]: u for u in state["realm_bots"]}+ state["realm_bots"] = {u["user_id"]: u for u in state["realm_bots"]}# Since time is different for every call, just fix the valuestate["server_timestamp"] = 0if "presence_last_update_id" in state:@@ -3441,7 +3441,7 @@ class NormalActionsTest(BaseAction):def test_create_bot(self) -> None:with self.verify_action(num_events=4) as events:self.create_bot("test")- check_realm_bot_add("events[3]", events[3])+ check_realm_bot_add("events[3]", events[3], UserProfile.DEFAULT_BOT)with self.verify_action(num_events=4) as events:self.create_bot(@@ -3453,7 +3453,7 @@ class NormalActionsTest(BaseAction):)# The third event is the second call of notify_created_bot, which contains additional# data for services (in contrast to the first call).- check_realm_bot_add("events[3]", events[3])+ check_realm_bot_add("events[3]", events[3], UserProfile.OUTGOING_WEBHOOK_BOT)with self.verify_action(num_events=4) as events:self.create_bot(@@ -3463,13 +3463,13 @@ class NormalActionsTest(BaseAction):config_data=orjson.dumps({"foo": "bar"}).decode(),bot_type=UserProfile.EMBEDDED_BOT,)- check_realm_bot_add("events[3]", events[3])+ check_realm_bot_add("events[3]", events[3], UserProfile.EMBEDDED_BOT)def test_change_bot_full_name(self) -> None:bot = self.create_bot("test")- with self.verify_action(num_events=2) as events:+ with self.verify_action(num_events=1) as events:do_change_full_name(bot, "New Bot Name", self.user_profile, notify=False)- check_realm_bot_update("events[1]", events[1], "full_name")+ check_realm_user_update("events[0]", events[0], "full_name")def test_regenerate_bot_api_key(self) -> None:bot = self.create_bot("test")@@ -3478,10 +3478,9 @@ class NormalActionsTest(BaseAction):def test_change_bot_avatar_source(self) -> None:bot = self.create_bot("test")- with self.verify_action(num_events=2) as events:+ with self.verify_action(num_events=1) as events:do_change_avatar_fields(bot, bot.AVATAR_FROM_USER, acting_user=self.user_profile)- check_realm_bot_update("events[0]", events[0], "avatar_url")- self.assertEqual(events[1]["type"], "realm_user")+ check_realm_user_update("events[0]", events[0], "avatar_fields")def test_change_realm_icon_source(self) -> None:with self.verify_action(state_change_expected=True) as events:@@ -3536,10 +3535,9 @@ class NormalActionsTest(BaseAction):self.user_profile = self.example_user("iago")owner = self.example_user("hamlet")bot = self.create_bot("test")- with self.verify_action(num_events=2) as events:+ with self.verify_action(num_events=1) as events:do_change_bot_owner(bot, owner, self.user_profile)- check_realm_bot_update("events[0]", events[0], "owner_id")- check_realm_user_update("events[1]", events[1], "bot_owner_id")+ check_realm_user_update("events[0]", events[0], "bot_owner_id")self.user_profile = self.example_user("aaron")owner = self.example_user("hamlet")@@ -3554,7 +3552,7 @@ class NormalActionsTest(BaseAction):bot = self.create_test_bot("test2", previous_owner, full_name="Test2 Testerson")with self.verify_action(num_events=2) as events:do_change_bot_owner(bot, self.user_profile, previous_owner)- check_realm_bot_add("events[0]", events[0])+ check_realm_bot_add("events[0]", events[0], UserProfile.DEFAULT_BOT)check_realm_user_update("events[1]", events[1], "bot_owner_id")def test_do_update_outgoing_webhook_service(self) -> None:@@ -3603,10 +3601,9 @@ class NormalActionsTest(BaseAction):def test_do_deactivate_bot(self) -> None:bot = self.create_bot("test")- with self.verify_action(num_events=2) as events:+ with self.verify_action(num_events=1) as events:do_deactivate_user(bot, acting_user=None)check_realm_user_update("events[0]", events[0], "is_active")- check_realm_bot_update("events[1]", events[1], "is_active")def test_do_deactivate_user(self) -> None:user_profile = self.example_user("cordelia")@@ -3761,21 +3758,21 @@ class NormalActionsTest(BaseAction):self.make_stream("Test private stream", invite_only=True)self.subscribe(bot, "Test private stream")do_deactivate_user(bot, acting_user=None)- with self.verify_action(num_events=5) as events:+ with self.verify_action(num_events=4) as events:do_reactivate_user(bot, acting_user=None)- check_realm_bot_update("events[1]", events[1], "is_active")- check_subscription_peer_add("events[2]", events[2])+ check_realm_user_update("events[0]", events[0], "is_active")+ check_subscription_peer_add("events[1]", events[1])+ check_user_group_add_members("events[2]", events[2])check_user_group_add_members("events[3]", events[3])- check_user_group_add_members("events[4]", events[4])# Test 'peer_add' event for private stream is received only if user is subscribed to it.do_deactivate_user(bot, acting_user=None)self.subscribe(self.example_user("hamlet"), "Test private stream")- with self.verify_action(num_events=6) as events:+ with self.verify_action(num_events=5) as events:do_reactivate_user(bot, acting_user=None)- check_realm_bot_update("events[1]", events[1], "is_active")+ check_realm_user_update("events[0]", events[0], "is_active")+ check_subscription_peer_add("events[1]", events[1])check_subscription_peer_add("events[2]", events[2])- check_subscription_peer_add("events[3]", events[3])do_deactivate_user(bot, acting_user=None)do_deactivate_user(self.example_user("hamlet"), acting_user=None)@@ -3784,13 +3781,12 @@ class NormalActionsTest(BaseAction):bot.refresh_from_db()self.user_profile = self.example_user("iago")- with self.verify_action(num_events=8) as events:+ with self.verify_action(num_events=6) as events:do_reactivate_user(bot, acting_user=self.example_user("iago"))- check_realm_bot_update("events[1]", events[1], "is_active")- check_realm_bot_update("events[2]", events[2], "owner_id")- check_realm_user_update("events[3]", events[3], "bot_owner_id")- check_subscription_peer_add("events[4]", events[4])- check_subscription_peer_add("events[5]", events[5])+ check_realm_user_update("events[0]", events[0], "is_active")+ check_realm_user_update("events[1]", events[1], "bot_owner_id")+ check_subscription_peer_add("events[2]", events[2])+ check_subscription_peer_add("events[3]", events[3])user_profile = self.example_user("cordelia")members_group = NamedUserGroup.objects.get(diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 18e030cdcf4..258345b7094 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -324,15 +324,9 @@ class HomeTest(ZulipTestCase):# TODO: Inspect the page_params data further.# print(orjson.dumps(page_params, option=orjson.OPT_INDENT_2).decode())realm_bots_expected_keys = [- "avatar_url",- "bot_type","default_all_public_streams","default_events_register_stream","default_sending_stream",- "email",- "full_name",- "is_active",- "owner_id","services","user_id",]@@ -923,12 +917,13 @@ class HomeTest(ZulipTestCase):users = page_params["state_data"][field]self.assertGreaterEqual(len(users), 3, field)for rec in users:- self.assertEqual(rec["user_id"], get_user(rec["email"], realm).id)if field == "realm_bots":+ self.assertIn("user_id", rec)self.assertNotIn("is_bot", rec)- self.assertIn("is_active", rec)- self.assertIn("owner_id", rec)+ self.assertNotIn("owner_id", rec)+ self.assertNotIn("is_active", rec)else:+ self.assertEqual(rec["user_id"], get_user(rec["email"], realm).id)self.assertIn("is_bot", rec)self.assertNotIn("is_active", rec)