internal_send_private_message: Fix edge case of message to system bot.

I don't think this is used anywhere outside of tests, but we should have
this logic correct. If this function is used to send a message from a
user to a cross-realm bot, the message.realm should be the realm of the
user.

In the normal case, where a user send a message to a cross-realm bot
through the API is already handled correctly, this bug is unrelated.
This commit is contained in:
Mateusz Mandera 2022-10-28 21:58:25 +02:00 committed by Tim Abbott
parent a674de5cf8
commit 78a325ac58
1 changed files with 5 additions and 1 deletions

View File

@ -85,6 +85,7 @@ from zerver.models import (
get_stream_by_id_in_realm,
get_system_bot,
get_user_by_delivery_email,
is_cross_realm_bot_email,
query_for_ids,
)
from zerver.tornado.django_api import send_event
@ -1602,7 +1603,10 @@ def internal_prep_private_message(
See _internal_prep_message for details of how this works.
"""
addressee = Addressee.for_user_profile(recipient_user)
realm = recipient_user.realm
if not is_cross_realm_bot_email(recipient_user.delivery_email):
realm = recipient_user.realm
else:
realm = sender.realm
return _internal_prep_message(
realm=realm,