mirror of https://github.com/zulip/zulip.git
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:
parent
e3edc4d829
commit
0f4de8e37d
|
@ -962,19 +962,15 @@ def stream_welcome_message(stream):
|
|||
def prep_stream_welcome_message(stream):
|
||||
# type: (Stream) -> Optional[Dict[str, Any]]
|
||||
realm = stream.realm
|
||||
sender_email = settings.WELCOME_BOT
|
||||
recipient_type_name = 'stream'
|
||||
recipients = stream.name
|
||||
subject = _('hello')
|
||||
|
||||
sender = get_user_profile_by_email(settings.WELCOME_BOT)
|
||||
topic = _('hello')
|
||||
content = stream_welcome_message(stream)
|
||||
|
||||
message = internal_prep_message(
|
||||
message = internal_prep_stream_message(
|
||||
realm=realm,
|
||||
sender_email=sender_email,
|
||||
recipient_type_name=recipient_type_name,
|
||||
recipients=recipients,
|
||||
subject=subject,
|
||||
sender=sender,
|
||||
stream_name=stream.name,
|
||||
topic=topic,
|
||||
content=content)
|
||||
|
||||
return message
|
||||
|
@ -1316,6 +1312,22 @@ def internal_prep_message(realm, sender_email, recipient_type_name, recipients,
|
|||
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,
|
||||
subject, content):
|
||||
# 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'])
|
||||
|
||||
# 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!
|
||||
|
||||
This is a message on stream `%s` with the topic `welcome`. We'll use this stream for
|
||||
system-generated notifications.""" % (notifications_stream.name,)
|
||||
msg = internal_prep_message(realm, settings.WELCOME_BOT, 'stream',
|
||||
notifications_stream.name, "welcome",
|
||||
content)
|
||||
system-generated notifications.""" % (stream_name,)
|
||||
|
||||
msg = internal_prep_stream_message(
|
||||
realm=realm,
|
||||
sender=sender,
|
||||
stream_name=stream_name,
|
||||
topic=topic,
|
||||
content=content)
|
||||
do_send_messages([msg])
|
||||
|
||||
# Log the event
|
||||
|
|
|
@ -12,6 +12,7 @@ from zerver.decorator import authenticated_json_post_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, \
|
||||
internal_prep_stream_message, \
|
||||
gather_subscriptions, subscribed_to_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, \
|
||||
|
@ -284,10 +285,19 @@ def add_subscriptions_backend(request, user_profile,
|
|||
else:
|
||||
stream_msg = "a new stream #**%s**." % created_streams[0].name
|
||||
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(
|
||||
internal_prep_message(user_profile.realm, settings.NOTIFICATION_BOT,
|
||||
"stream",
|
||||
notifications_stream.name, "Streams", msg))
|
||||
internal_prep_stream_message(
|
||||
realm=user_profile.realm,
|
||||
sender=sender,
|
||||
stream_name=stream_name,
|
||||
topic=topic,
|
||||
content=msg))
|
||||
|
||||
else:
|
||||
msg = ("Hi there! %s just created a new stream #**%s**."
|
||||
% (user_profile.full_name, created_streams[0].name))
|
||||
|
|
Loading…
Reference in New Issue