From 711a50f1e8c3905b4e4e89ca26aad584ec33435d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 27 Apr 2017 11:42:13 -0700 Subject: [PATCH] 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. --- zerver/lib/actions.py | 24 ++++++++++++++++++++++-- zerver/views/streams.py | 28 ++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 33ff4f7c2d..e0b2c08940 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -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 diff --git a/zerver/views/streams.py b/zerver/views/streams.py index d5db9f9554..c21279671b 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -11,7 +11,7 @@ from zerver.decorator import authenticated_json_post_view, \ authenticated_json_view, \ get_user_profile_by_email, require_realm_admin, to_non_negative_int 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, \ gather_subscriptions, subscribed_to_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: 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, - "private", email, "", msg)) + + sender = get_user_profile_by_email(settings.NOTIFICATION_BOT) + notifications.append( + internal_prep_private_message( + realm=user_profile.realm, + sender=sender, + recipient_email=email, + content=msg)) if announce and len(created_streams) > 0: notifications_stream = user_profile.realm.notifications_stream # type: Optional[Stream] @@ -301,15 +306,22 @@ def add_subscriptions_backend(request, user_profile, else: msg = ("Hi there! %s just created a new stream #**%s**." % (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): # Don't announce to yourself or to people you explicitly added # (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( - user_profile.realm, settings.NOTIFICATION_BOT, - "private", - realm_user_dict['email'], "", msg)) + + recipient_email = realm_user_dict['email'] + + 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: for stream in created_streams: