internal_prep_message: Make realm argument mandatory.

In order to correctly handle messages sent by cross-realm bots, we
need to specify the realm that the messages are being sent into in the
send message code path.  The commit and its successors convert that
code path to include the realm the message is being sent to explicitly.
This commit is contained in:
Tim Abbott
2017-01-21 20:11:53 -08:00
parent b0efa58eb4
commit 8ba7d2080c
2 changed files with 18 additions and 17 deletions

View File

@@ -1247,7 +1247,7 @@ def send_pm_if_empty_stream(sender, stream, stream_name, realm):
"tried to send a message to stream `%s`, but %s"
"click the gear in the left-side stream list." %
(sender.full_name, stream_name, error_msg))
message = internal_prep_message(settings.NOTIFICATION_BOT, "private",
message = internal_prep_message(realm, settings.NOTIFICATION_BOT, "private",
sender.bot_owner.email, "", content)
do_send_messages([message])
@@ -1351,9 +1351,9 @@ def check_message(sender, client, message_type_name, message_to,
return {'message': message, 'stream': stream, 'local_id': local_id, 'sender_queue_id': sender_queue_id}
def internal_prep_message(sender_email, recipient_type_name, recipients,
subject, content, realm=None):
# type: (Text, str, Text, Text, Text, Optional[Realm]) -> Optional[Dict[str, Any]]
def internal_prep_message(realm, sender_email, recipient_type_name, recipients,
subject, content):
# type: (Realm, Text, str, Text, Text, Text) -> Optional[Dict[str, Any]]
"""
Create a message object and checks it, but doesn't send it or save it to the database.
The internal function that calls this can therefore batch send a bunch of created
@@ -1365,7 +1365,7 @@ def internal_prep_message(sender_email, recipient_type_name, recipients,
sender = get_user_profile_by_email(sender_email)
if realm is None:
realm = sender.realm
raise RuntimeError("None is not a valid realm for internal_prep_message!")
parsed_recipients = extract_recipients(recipients)
if recipient_type_name == "stream":
stream, _ = create_stream_if_needed(realm, parsed_recipients[0])
@@ -1381,8 +1381,8 @@ def internal_prep_message(sender_email, recipient_type_name, recipients,
def internal_send_message(sender_email, recipient_type_name, recipients,
subject, content, realm=None):
# type: (Text, str, Text, Text, Text, Optional[Realm]) -> None
msg = internal_prep_message(sender_email, recipient_type_name, recipients,
subject, content, realm)
msg = internal_prep_message(realm, sender_email, recipient_type_name, recipients,
subject, content)
# internal_prep_message encountered an error
if msg is None:
@@ -2167,9 +2167,9 @@ def do_create_realm(string_id, name, restricted_to_domain=None,
This is a message on stream `%s` with the topic `welcome`. We'll use this stream for
system-generated notifications.""" % (product_name, notifications_stream.name,)
msg = internal_prep_message(settings.WELCOME_BOT, 'stream',
msg = internal_prep_message(realm, settings.WELCOME_BOT, 'stream',
notifications_stream.name, "welcome",
content, realm=realm)
content)
do_send_messages([msg])
# Log the event

View File

@@ -369,8 +369,9 @@ def add_subscriptions_backend(request, user_profile,
if len([s for s in subscriptions if not private_streams[s]]) > 0:
msg += "\nYou can see historical content on a non-invite-only stream by narrowing to it."
notifications.append(internal_prep_message(settings.NOTIFICATION_BOT,
"private", email, "", msg))
notifications.append(internal_prep_message(
user_profile.realm, settings.NOTIFICATION_BOT,
"private", email, "", msg))
if announce and len(created_streams) > 0:
notifications_stream = user_profile.realm.notifications_stream
@@ -381,10 +382,9 @@ def add_subscriptions_backend(request, user_profile,
stream_msg = "a new stream #**%s**." % created_streams[0].name
msg = ("%s just created %s" % (user_profile.full_name, stream_msg))
notifications.append(
internal_prep_message(settings.NOTIFICATION_BOT,
internal_prep_message(user_profile.realm, settings.NOTIFICATION_BOT,
"stream",
notifications_stream.name, "Streams", msg,
realm=notifications_stream.realm))
notifications_stream.name, "Streams", msg))
else:
msg = ("Hi there! %s just created a new stream #**%s**."
% (user_profile.full_name, created_streams[0].name))
@@ -393,9 +393,10 @@ def add_subscriptions_backend(request, user_profile,
# (who will get the notification above instead).
if realm_user_dict['email'] in principals or realm_user_dict['email'] == user_profile.email:
continue
notifications.append(internal_prep_message(settings.NOTIFICATION_BOT,
"private",
realm_user_dict['email'], "", msg))
notifications.append(internal_prep_message(
user_profile.realm, settings.NOTIFICATION_BOT,
"private",
realm_user_dict['email'], "", msg))
if len(notifications) > 0:
do_send_messages(notifications)