Add internal_prep_stream_message().

The new, more direct interface for prepping internal stream
messages circumvents the bug-prone extract_recipients() method,
which has the pitfall that it will try to parse a stream name
as JSON.  It also takes a UserProfile object for the sender, so
it's a bit more type-safe.
This commit is contained in:
Steve Howell 2017-04-27 10:44:09 -07:00 committed by Tim Abbott
parent e3edc4d829
commit 0f4de8e37d
2 changed files with 46 additions and 17 deletions

View File

@ -962,19 +962,15 @@ def stream_welcome_message(stream):
def prep_stream_welcome_message(stream): def prep_stream_welcome_message(stream):
# type: (Stream) -> Optional[Dict[str, Any]] # type: (Stream) -> Optional[Dict[str, Any]]
realm = stream.realm realm = stream.realm
sender_email = settings.WELCOME_BOT sender = get_user_profile_by_email(settings.WELCOME_BOT)
recipient_type_name = 'stream' topic = _('hello')
recipients = stream.name
subject = _('hello')
content = stream_welcome_message(stream) content = stream_welcome_message(stream)
message = internal_prep_message( message = internal_prep_stream_message(
realm=realm, realm=realm,
sender_email=sender_email, sender=sender,
recipient_type_name=recipient_type_name, stream_name=stream.name,
recipients=recipients, topic=topic,
subject=subject,
content=content) content=content)
return message return message
@ -1316,6 +1312,22 @@ def internal_prep_message(realm, sender_email, recipient_type_name, recipients,
content=content, content=content,
) )
def internal_prep_stream_message(realm, sender, stream_name, topic, content):
# type: (Realm, UserProfile, Text, Text, Text) -> Optional[Dict[str, Any]]
"""
See _internal_prep_message for details of how this works.
"""
parsed_recipients = [stream_name]
return _internal_prep_message(
realm=realm,
sender=sender,
recipient_type_name='stream',
parsed_recipients=parsed_recipients,
subject=topic,
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
@ -2116,13 +2128,20 @@ def do_create_realm(string_id, name, restricted_to_domain=None,
realm.save(update_fields=['notifications_stream']) realm.save(update_fields=['notifications_stream'])
# Include a welcome message in this notifications stream # Include a welcome message in this notifications stream
stream_name = notifications_stream.name
sender = get_user_profile_by_email(settings.WELCOME_BOT)
topic = "welcome"
content = """Hello, and welcome to Zulip! content = """Hello, and welcome to Zulip!
This is a message on stream `%s` with the topic `welcome`. We'll use this stream for This is a message on stream `%s` with the topic `welcome`. We'll use this stream for
system-generated notifications.""" % (notifications_stream.name,) system-generated notifications.""" % (stream_name,)
msg = internal_prep_message(realm, settings.WELCOME_BOT, 'stream',
notifications_stream.name, "welcome", msg = internal_prep_stream_message(
content) realm=realm,
sender=sender,
stream_name=stream_name,
topic=topic,
content=content)
do_send_messages([msg]) do_send_messages([msg])
# Log the event # Log the event

View File

@ -12,6 +12,7 @@ from zerver.decorator import authenticated_json_post_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_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, \
do_deactivate_stream, do_change_stream_invite_only, do_add_default_stream, \ do_deactivate_stream, do_change_stream_invite_only, do_add_default_stream, \
@ -284,10 +285,19 @@ def add_subscriptions_backend(request, user_profile,
else: else:
stream_msg = "a new stream #**%s**." % created_streams[0].name stream_msg = "a new stream #**%s**." % created_streams[0].name
msg = ("%s just created %s" % (user_profile.full_name, stream_msg)) msg = ("%s just created %s" % (user_profile.full_name, stream_msg))
sender = get_user_profile_by_email(settings.NOTIFICATION_BOT)
stream_name = notifications_stream.name
topic = 'Streams'
notifications.append( notifications.append(
internal_prep_message(user_profile.realm, settings.NOTIFICATION_BOT, internal_prep_stream_message(
"stream", realm=user_profile.realm,
notifications_stream.name, "Streams", msg)) sender=sender,
stream_name=stream_name,
topic=topic,
content=msg))
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))