bots: Pass realm to get_system_bot calls in actions.py.

This commit is contained in:
Mateusz Mandera
2021-03-08 11:48:14 +01:00
committed by Tim Abbott
parent 6f6be04f2e
commit a124a4dfcf
3 changed files with 22 additions and 15 deletions

View File

@@ -237,6 +237,7 @@ from zerver.models import (
get_huddle_recipient, get_huddle_recipient,
get_huddle_user_ids, get_huddle_user_ids,
get_old_unclaimed_attachments, get_old_unclaimed_attachments,
get_realm,
get_realm_playgrounds, get_realm_playgrounds,
get_stream, get_stream,
get_stream_by_id_in_realm, get_stream_by_id_in_realm,
@@ -363,7 +364,8 @@ def send_message_to_signup_notification_stream(
def notify_new_user(user_profile: UserProfile) -> None: def notify_new_user(user_profile: UserProfile) -> None:
user_count = realm_user_count(user_profile.realm) user_count = realm_user_count(user_profile.realm)
sender = get_system_bot(settings.NOTIFICATION_BOT) sender_email = settings.NOTIFICATION_BOT
sender = get_system_bot(sender_email, user_profile.realm_id)
is_first_user = user_count == 1 is_first_user = user_count == 1
if not is_first_user: if not is_first_user:
@@ -384,7 +386,8 @@ def notify_new_user(user_profile: UserProfile) -> None:
send_message_to_signup_notification_stream(sender, user_profile.realm, message) send_message_to_signup_notification_stream(sender, user_profile.realm, message)
# We also send a notification to the Zulip administrative realm # We also send a notification to the Zulip administrative realm
admin_realm = sender.realm admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
admin_realm_sender = get_system_bot(sender_email, admin_realm.id)
try: try:
# Check whether the stream exists # Check whether the stream exists
signups_stream = get_signups_stream(admin_realm) signups_stream = get_signups_stream(admin_realm)
@@ -393,7 +396,7 @@ def notify_new_user(user_profile: UserProfile) -> None:
user=f"{user_profile.full_name} <`{user_profile.email}`>", user_count=user_count user=f"{user_profile.full_name} <`{user_profile.email}`>", user_count=user_count
) )
internal_send_stream_message( internal_send_stream_message(
sender, signups_stream, user_profile.realm.display_subdomain, message admin_realm_sender, signups_stream, user_profile.realm.display_subdomain, message
) )
except Stream.DoesNotExist: except Stream.DoesNotExist:
@@ -498,7 +501,7 @@ def process_new_human_user(
# This is a cross-realm private message. # This is a cross-realm private message.
with override_language(prereg_user.referred_by.default_language): with override_language(prereg_user.referred_by.default_language):
internal_send_private_message( internal_send_private_message(
get_system_bot(settings.NOTIFICATION_BOT), get_system_bot(settings.NOTIFICATION_BOT, prereg_user.referred_by.realm_id),
prereg_user.referred_by, prereg_user.referred_by,
_("{user} accepted your invitation to join Zulip!").format( _("{user} accepted your invitation to join Zulip!").format(
user=f"{user_profile.full_name} <`{user_profile.email}`>" user=f"{user_profile.full_name} <`{user_profile.email}`>"
@@ -2065,7 +2068,9 @@ def do_send_messages(
queue_json_publish("embed_links", event_data) queue_json_publish("embed_links", event_data)
if send_request.message.recipient.type == Recipient.PERSONAL: if send_request.message.recipient.type == Recipient.PERSONAL:
welcome_bot_id = get_system_bot(settings.WELCOME_BOT).id welcome_bot_id = get_system_bot(
settings.WELCOME_BOT, send_request.message.sender.realm_id
).id
if ( if (
welcome_bot_id in send_request.active_user_ids welcome_bot_id in send_request.active_user_ids
and welcome_bot_id != send_request.message.sender_id and welcome_bot_id != send_request.message.sender_id
@@ -3070,7 +3075,9 @@ def send_rate_limited_pm_notification_to_bot_owner(
return return
internal_send_private_message( internal_send_private_message(
get_system_bot(settings.NOTIFICATION_BOT), sender.bot_owner, content get_system_bot(settings.NOTIFICATION_BOT, sender.bot_owner.realm_id),
sender.bot_owner,
content,
) )
sender.last_reminder = timezone_now() sender.last_reminder = timezone_now()
@@ -4829,7 +4836,7 @@ def do_rename_stream(stream: Stream, new_name: str, user_profile: UserProfile) -
name=old_name, name=old_name,
) )
send_event(stream.realm, event, can_access_stream_user_ids(stream)) send_event(stream.realm, event, can_access_stream_user_ids(stream))
sender = get_system_bot(settings.NOTIFICATION_BOT) sender = get_system_bot(settings.NOTIFICATION_BOT, stream.realm_id)
with override_language(stream.realm.default_language): with override_language(stream.realm.default_language):
internal_send_stream_message( internal_send_stream_message(
sender, sender,
@@ -4953,8 +4960,8 @@ def do_create_realm(
if plan_type is None and settings.BILLING_ENABLED: if plan_type is None and settings.BILLING_ENABLED:
do_change_plan_type(realm, Realm.LIMITED, acting_user=None) do_change_plan_type(realm, Realm.LIMITED, acting_user=None)
sender = get_system_bot(settings.NOTIFICATION_BOT) admin_realm = get_realm(settings.SYSTEM_BOT_REALM)
admin_realm = sender.realm sender = get_system_bot(settings.NOTIFICATION_BOT, admin_realm.id)
# Send a notification to the admin realm # Send a notification to the admin realm
signup_message = _("Signups enabled") signup_message = _("Signups enabled")
@@ -5741,7 +5748,7 @@ def maybe_send_resolve_topic_notifications(
# not a bug with the "resolve topics" feature. # not a bug with the "resolve topics" feature.
return return
sender = get_system_bot(settings.NOTIFICATION_BOT) sender = get_system_bot(settings.NOTIFICATION_BOT, user_profile.realm_id)
user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**" user_mention = f"@_**{user_profile.full_name}|{user_profile.id}**"
with override_language(stream.realm.default_language): with override_language(stream.realm.default_language):
internal_send_stream_message( internal_send_stream_message(
@@ -5766,7 +5773,7 @@ def send_message_moved_breadcrumbs(
# Since moving content between streams is highly disruptive, # Since moving content between streams is highly disruptive,
# it's worth adding a couple tombstone messages showing what # it's worth adding a couple tombstone messages showing what
# happened. # happened.
sender = get_system_bot(settings.NOTIFICATION_BOT) sender = get_system_bot(settings.NOTIFICATION_BOT, old_stream.realm_id)
if new_topic is None: if new_topic is None:
new_topic = old_topic new_topic = old_topic

View File

@@ -774,13 +774,13 @@ class LoginTest(ZulipTestCase):
with queries_captured() as queries, cache_tries_captured() as cache_tries: with queries_captured() as queries, cache_tries_captured() as cache_tries:
self.register(self.nonreg_email("test"), "test") self.register(self.nonreg_email("test"), "test")
# Ensure the number of queries we make is not O(streams) # Ensure the number of queries we make is not O(streams)
self.assert_length(queries, 88) self.assert_length(queries, 89)
# We can probably avoid a couple cache hits here, but there doesn't # We can probably avoid a couple cache hits here, but there doesn't
# seem to be any O(N) behavior. Some of the cache hits are related # seem to be any O(N) behavior. Some of the cache hits are related
# to sending messages, such as getting the welcome bot, looking up # to sending messages, such as getting the welcome bot, looking up
# the alert words for a realm, etc. # the alert words for a realm, etc.
self.assert_length(cache_tries, 20) self.assert_length(cache_tries, 21)
user_profile = self.nonreg_user("test") user_profile = self.nonreg_user("test")
self.assert_logged_in_user_id(user_profile.id) self.assert_logged_in_user_id(user_profile.id)

View File

@@ -771,8 +771,8 @@ class QueryCountTest(ZulipTestCase):
acting_user=None, acting_user=None,
) )
self.assert_length(queries, 83) self.assert_length(queries, 84)
self.assert_length(cache_tries, 26) self.assert_length(cache_tries, 27)
peer_add_events = [event for event in events if event["event"].get("op") == "peer_add"] peer_add_events = [event for event in events if event["event"].get("op") == "peer_add"]