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.

bots: Send realm_user events first when changing bot owner.

Sahil Batra0df620c574c8
diff --git a/zerver/actions/bots.py b/zerver/actions/bots.py
index 5836b6aa8d1..f0f0e117140 100644
--- a/zerver/actions/bots.py
+++ b/zerver/actions/bots.py
@@ -11,9 +11,17 @@ from zerver.tornado.django_api import send_event_on_commit
def send_bot_owner_update_events(
user_profile: UserProfile, bot_owner: UserProfile, previous_owner: UserProfile | None
) -> None:
- # 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).
+ # Since `bot_owner_id` is included in the user profile dict we need
+ # to update the users dict with the new bot owner id
+ event = dict(
+ type="realm_user",
+ op="update",
+ person=dict(
+ user_id=user_profile.id,
+ bot_owner_id=bot_owner.id,
+ ),
+ )
+ send_event_on_commit(user_profile.realm, event, active_user_ids(user_profile.realm_id))
# Delete the bot from previous owner's bot data.
if previous_owner and not previous_owner.is_realm_admin:
@@ -36,18 +44,6 @@ def send_bot_owner_update_events(
add_event = created_bot_event(user_profile)
send_event_on_commit(user_profile.realm, add_event, {bot_owner.id})
- # Since `bot_owner_id` is included in the user profile dict we need
- # to update the users dict with the new bot owner id
- event = dict(
- type="realm_user",
- op="update",
- person=dict(
- user_id=user_profile.id,
- bot_owner_id=bot_owner.id,
- ),
- )
- send_event_on_commit(user_profile.realm, event, active_user_ids(user_profile.realm_id))
-
@transaction.atomic(durable=True)
def do_change_bot_owner(
diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py
index d36b52da255..011f15db0d0 100644
--- a/zerver/tests/test_events.py
+++ b/zerver/tests/test_events.py
@@ -3544,16 +3544,16 @@ class NormalActionsTest(BaseAction):
bot = self.create_bot("test1", full_name="Test1 Testerson")
with self.verify_action(num_events=2) as events:
do_change_bot_owner(bot, owner, self.user_profile)
- check_realm_bot_delete("events[0]", events[0])
- check_realm_user_update("events[1]", events[1], "bot_owner_id")
+ check_realm_user_update("events[0]", events[0], "bot_owner_id")
+ check_realm_bot_delete("events[1]", events[1])
previous_owner = self.example_user("aaron")
self.user_profile = self.example_user("hamlet")
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], UserProfile.DEFAULT_BOT)
- check_realm_user_update("events[1]", events[1], "bot_owner_id")
+ check_realm_user_update("events[0]", events[0], "bot_owner_id")
+ check_realm_bot_add("events[1]", events[1], UserProfile.DEFAULT_BOT)
def test_do_update_outgoing_webhook_service(self) -> None:
self.user_profile = self.example_user("iago")