Add internal_prep_private_message().

The new function takes a full UserProfile object for the sender,
which allows us to avoid O(N) calls when creating the stream to
find the user profile of the notification bot.  (The calls were
already cached, so this won't necessarily be a huge performance
win.)

We also don't have to worry about sending a blank subject any more.
This commit is contained in:
Steve Howell
2017-04-27 11:42:13 -07:00
committed by Tim Abbott
parent 0f4de8e37d
commit 711a50f1e8
2 changed files with 42 additions and 10 deletions

View File

@@ -1160,8 +1160,12 @@ 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(realm, settings.NOTIFICATION_BOT, "private",
sender.bot_owner.email, "", content)
message = internal_prep_private_message(
realm=realm,
sender=get_user_profile_by_email(settings.NOTIFICATION_BOT),
recipient_email=sender.bot_owner.email,
content=content)
do_send_messages([message])
sender.last_reminder = timezone_now()
@@ -1328,6 +1332,22 @@ def internal_prep_stream_message(realm, sender, stream_name, topic, content):
content=content,
)
def internal_prep_private_message(realm, sender, recipient_email, content):
# type: (Realm, UserProfile, Text, Text) -> Optional[Dict[str, Any]]
"""
See _internal_prep_message for details of how this works.
"""
parsed_recipients = [recipient_email]
return _internal_prep_message(
realm=realm,
sender=sender,
recipient_type_name='private',
parsed_recipients=parsed_recipients,
subject='',
content=content,
)
def internal_send_message(realm, sender_email, recipient_type_name, recipients,
subject, content):
# type: (Realm, Text, str, Text, Text, Text) -> None