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" "tried to send a message to stream `%s`, but %s"
"click the gear in the left-side stream list." % "click the gear in the left-side stream list." %
(sender.full_name, stream_name, error_msg)) (sender.full_name, stream_name, error_msg))
message = internal_prep_message(realm, settings.NOTIFICATION_BOT, "private", message = internal_prep_private_message(
sender.bot_owner.email, "", content) realm=realm,
sender=get_user_profile_by_email(settings.NOTIFICATION_BOT),
recipient_email=sender.bot_owner.email,
content=content)
do_send_messages([message]) do_send_messages([message])
sender.last_reminder = timezone_now() sender.last_reminder = timezone_now()
@ -1328,6 +1332,22 @@ def internal_prep_stream_message(realm, sender, stream_name, topic, content):
content=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, def internal_send_message(realm, sender_email, recipient_type_name, recipients,
subject, content): subject, content):
# type: (Realm, Text, str, Text, Text, Text) -> None # type: (Realm, Text, str, Text, Text, Text) -> None

View File

@ -11,7 +11,7 @@ from zerver.decorator import authenticated_json_post_view, \
authenticated_json_view, \ authenticated_json_view, \
get_user_profile_by_email, require_realm_admin, to_non_negative_int get_user_profile_by_email, require_realm_admin, to_non_negative_int
from zerver.lib.actions import bulk_remove_subscriptions, \ from zerver.lib.actions import bulk_remove_subscriptions, \
do_change_subscription_property, internal_prep_message, \ do_change_subscription_property, internal_prep_private_message, \
internal_prep_stream_message, \ internal_prep_stream_message, \
gather_subscriptions, subscribed_to_stream, \ gather_subscriptions, subscribed_to_stream, \
bulk_add_subscriptions, do_send_messages, get_subscriber_emails, do_rename_stream, \ bulk_add_subscriptions, do_send_messages, get_subscriber_emails, do_rename_stream, \
@ -273,9 +273,14 @@ def add_subscriptions_backend(request, user_profile,
if len([s for s in subscriptions if not private_streams[s]]) > 0: 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." msg += "\nYou can see historical content on a non-invite-only stream by narrowing to it."
notifications.append(internal_prep_message(
user_profile.realm, settings.NOTIFICATION_BOT, sender = get_user_profile_by_email(settings.NOTIFICATION_BOT)
"private", email, "", msg)) notifications.append(
internal_prep_private_message(
realm=user_profile.realm,
sender=sender,
recipient_email=email,
content=msg))
if announce and len(created_streams) > 0: if announce and len(created_streams) > 0:
notifications_stream = user_profile.realm.notifications_stream # type: Optional[Stream] notifications_stream = user_profile.realm.notifications_stream # type: Optional[Stream]
@ -301,15 +306,22 @@ def add_subscriptions_backend(request, user_profile,
else: else:
msg = ("Hi there! %s just created a new stream #**%s**." msg = ("Hi there! %s just created a new stream #**%s**."
% (user_profile.full_name, created_streams[0].name)) % (user_profile.full_name, created_streams[0].name))
sender = get_user_profile_by_email(settings.NOTIFICATION_BOT)
for realm_user_dict in get_active_user_dicts_in_realm(user_profile.realm): for realm_user_dict in get_active_user_dicts_in_realm(user_profile.realm):
# Don't announce to yourself or to people you explicitly added # Don't announce to yourself or to people you explicitly added
# (who will get the notification above instead). # (who will get the notification above instead).
if realm_user_dict['email'] in principals or realm_user_dict['email'] == user_profile.email: if realm_user_dict['email'] in principals or realm_user_dict['email'] == user_profile.email:
continue continue
notifications.append(internal_prep_message(
user_profile.realm, settings.NOTIFICATION_BOT, recipient_email = realm_user_dict['email']
"private",
realm_user_dict['email'], "", msg)) notifications.append(
internal_prep_private_message(
realm=user_profile.realm,
sender=sender,
recipient_email=recipient_email,
content=msg))
if not user_profile.realm.is_zephyr_mirror_realm: if not user_profile.realm.is_zephyr_mirror_realm:
for stream in created_streams: for stream in created_streams: