internal_send_message: Make realm argument mandatory.

A lot of care has been taken to ensure we're using the realm that the
message is being sent into, not the realm of the sender, to correctly
handle the logic for cross-realm bot users such as the notifications
bot.
This commit is contained in:
Tim Abbott 2017-01-21 20:23:36 -08:00
parent 8ba7d2080c
commit 99c5563bc6
9 changed files with 37 additions and 20 deletions

View File

@ -228,14 +228,18 @@ def send_signup_message(sender, signups_stream, user_profile,
# Don't send notification for the first user in a realm # Don't send notification for the first user in a realm
if user_profile.realm.notifications_stream is not None and user_count > 1: if user_profile.realm.notifications_stream is not None and user_count > 1:
internal_send_message( internal_send_message(
user_profile.realm,
sender, sender,
"stream", "stream",
user_profile.realm.notifications_stream.name, user_profile.realm.notifications_stream.name,
"New users", "%s just signed up for Zulip. Say hello!" % ( "New users", "%s just signed up for Zulip. Say hello!" % (
user_profile.full_name,), user_profile.full_name,)
realm=user_profile.realm) )
# We also send a notification to the Zulip administrative realm
admin_realm = get_user_profile_by_email(sender).realm
internal_send_message( internal_send_message(
admin_realm,
sender, sender,
"stream", "stream",
signups_stream, signups_stream,
@ -309,6 +313,7 @@ def process_new_human_user(user_profile, prereg_user=None, newsletter_data=None)
and settings.NOTIFICATION_BOT is not None: and settings.NOTIFICATION_BOT is not None:
# This is a cross-realm private message. # This is a cross-realm private message.
internal_send_message( internal_send_message(
user_profile.realm,
settings.NOTIFICATION_BOT, settings.NOTIFICATION_BOT,
"private", "private",
prereg_user.referred_by.email, prereg_user.referred_by.email,
@ -1378,9 +1383,9 @@ def internal_prep_message(realm, sender_email, recipient_type_name, recipients,
return None return None
def internal_send_message(sender_email, recipient_type_name, recipients, def internal_send_message(realm, sender_email, recipient_type_name, recipients,
subject, content, realm=None): subject, content):
# type: (Text, str, Text, Text, Text, Optional[Realm]) -> None # type: (Realm, Text, str, Text, Text, Text) -> None
msg = internal_prep_message(realm, sender_email, recipient_type_name, recipients, msg = internal_prep_message(realm, sender_email, recipient_type_name, recipients,
subject, content) subject, content)
@ -2179,9 +2184,11 @@ system-generated notifications.""" % (product_name, notifications_stream.name,)
"invite_required": invite_required, "invite_required": invite_required,
"org_type": org_type}) "org_type": org_type})
# Send a notification to the admin realm (if configured)
if settings.NEW_USER_BOT is not None: if settings.NEW_USER_BOT is not None:
signup_message = "Signups enabled" signup_message = "Signups enabled"
internal_send_message(settings.NEW_USER_BOT, "stream", admin_realm = get_user_profile_by_email(settings.NEW_USER_BOT).realm
internal_send_message(admin_realm, settings.NEW_USER_BOT, "stream",
"signups", string_id, signup_message) "signups", string_id, signup_message)
return (realm, created) return (realm, created)

View File

@ -1333,6 +1333,7 @@ def do_convert(content, realm_filters_key=None, message=None, possible_words=Non
return timeout(5, _md_engine.convert, content) return timeout(5, _md_engine.convert, content)
except: except:
from zerver.lib.actions import internal_send_message from zerver.lib.actions import internal_send_message
from zerver.models import get_user_profile_by_email
cleaned = _sanitize_for_log(content) cleaned = _sanitize_for_log(content)
@ -1341,7 +1342,8 @@ def do_convert(content, realm_filters_key=None, message=None, possible_words=Non
% (traceback.format_exc(), cleaned)) % (traceback.format_exc(), cleaned))
subject = "Markdown parser failure on %s" % (platform.node(),) subject = "Markdown parser failure on %s" % (platform.node(),)
if settings.ERROR_BOT is not None: if settings.ERROR_BOT is not None:
internal_send_message(settings.ERROR_BOT, "stream", error_bot_realm = get_user_profile_by_email(settings.ERROR_BOT).realm
internal_send_message(error_bot_realm, settings.ERROR_BOT, "stream",
"errors", subject, "Markdown parser failed, email sent with details.") "errors", subject, "Markdown parser failed, email sent with details.")
mail.mail_admins(subject, "Failed message: %s\n\n%s\n\n" % ( mail.mail_admins(subject, "Failed message: %s\n\n%s\n\n" % (
cleaned, traceback.format_exc()), cleaned, traceback.format_exc()),

View File

@ -176,8 +176,8 @@ def send_to_missed_message_address(address, message):
else: else:
recipient_type_name = 'private' recipient_type_name = 'private'
internal_send_message(user_profile.email, recipient_type_name, internal_send_message(user_profile.realm, user_profile.email,
recipient_str, subject, body) recipient_type_name, recipient_str, subject, body)
logging.info("Successfully processed email from %s to %s" % ( logging.info("Successfully processed email from %s to %s" % (
user_profile.email, recipient_str)) user_profile.email, recipient_str))
@ -189,12 +189,12 @@ class ZulipEmailForwardError(Exception):
def send_zulip(sender, stream, topic, content): def send_zulip(sender, stream, topic, content):
# type: (Text, Stream, Text, Text) -> None # type: (Text, Stream, Text, Text) -> None
internal_send_message( internal_send_message(
stream.realm,
sender, sender,
"stream", "stream",
stream.name, stream.name,
topic[:60], topic[:60],
content[:2000], content[:2000])
stream.realm)
def valid_stream(stream_name, token): def valid_stream(stream_name, token):
# type: (Text, Text) -> bool # type: (Text, Text) -> bool

View File

@ -116,12 +116,14 @@ def highlight_html_differences(s1, s2):
if not verify_html(retval): if not verify_html(retval):
from zerver.lib.actions import internal_send_message from zerver.lib.actions import internal_send_message
from zerver.models import get_user_profile_by_email
# We probably want more information here # We probably want more information here
logging.getLogger('').error('HTML diff produced mal-formed HTML') logging.getLogger('').error('HTML diff produced mal-formed HTML')
if settings.ERROR_BOT is not None: if settings.ERROR_BOT is not None:
subject = "HTML diff failure on %s" % (platform.node(),) subject = "HTML diff failure on %s" % (platform.node(),)
internal_send_message(settings.ERROR_BOT, "stream", realm = get_user_profile_by_email(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT, "stream",
"errors", subject, "HTML diff produced malformed HTML") "errors", subject, "HTML diff produced malformed HTML")
return s2 return s2

View File

@ -79,8 +79,8 @@ def json_bulk_invite_users(request, user_profile,
invited = PreregistrationUser.objects.filter(referred_by=user_profile) invited = PreregistrationUser.objects.filter(referred_by=user_profile)
internal_message = "%s <`%s`> invited %d people to Zulip." % ( internal_message = "%s <`%s`> invited %d people to Zulip." % (
user_profile.full_name, user_profile.email, invited.count()) user_profile.full_name, user_profile.email, invited.count())
internal_send_message(settings.NEW_USER_BOT, "stream", "signups", internal_send_message(user_profile.realm, settings.NEW_USER_BOT, "stream",
user_profile.realm.domain, internal_message) "signups", user_profile.realm.domain, internal_message)
return json_success() return json_success()
@authenticated_json_post_view @authenticated_json_post_view

View File

@ -23,8 +23,8 @@ def json_tutorial_send_message(request, user_profile, type=REQ(validator=check_s
""" """
sender_name = "welcome-bot@zulip.com" sender_name = "welcome-bot@zulip.com"
if type == 'stream': if type == 'stream':
internal_send_message(sender_name, "stream", recipient, topic, content, internal_send_message(user_profile.realm, sender_name,
realm=user_profile.realm) "stream", recipient, topic, content)
return json_success() return json_success()
# For now, there are no PM cases. # For now, there are no PM cases.
return json_error(_('Bad data passed in to tutorial_send_message')) return json_error(_('Bad data passed in to tutorial_send_message'))

View File

@ -301,7 +301,9 @@ class SlowQueryWorker(QueueProcessingWorker):
for query in slow_queries: for query in slow_queries:
content += " %s\n" % (query,) content += " %s\n" % (query,)
internal_send_message(settings.ERROR_BOT, "stream", "logs", topic, content) error_bot_realm = get_user_profile_by_email(settings.ERROR_BOT).realm
internal_send_message(error_bot_realm, settings.ERROR_BOT,
"stream", "logs", topic, content)
reset_queries() reset_queries()

View File

@ -6,6 +6,7 @@ import logging
from django.conf import settings from django.conf import settings
from django.core.mail import mail_admins from django.core.mail import mail_admins
from zerver.models import get_user_profile_by_email
from zerver.lib.actions import internal_send_message from zerver.lib.actions import internal_send_message
from typing import Dict from typing import Dict
import six import six
@ -66,7 +67,8 @@ def zulip_browser_error(report):
body += ("Message: %(message)s\n" body += ("Message: %(message)s\n"
% (report)) % (report))
internal_send_message(settings.ERROR_BOT, realm = get_user_profile_by_email(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT,
"stream", "errors", format_subject(subject), body) "stream", "errors", format_subject(subject), body)
def notify_server_error(report): def notify_server_error(report):
@ -92,7 +94,8 @@ def zulip_server_error(report):
request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower())) request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower()))
request_repr += "~~~~" request_repr += "~~~~"
internal_send_message(settings.ERROR_BOT, realm = get_user_profile_by_email(settings.ERROR_BOT).realm
internal_send_message(realm, settings.ERROR_BOT,
"stream", "errors", format_subject(subject), "stream", "errors", format_subject(subject),
"Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s" % ( "Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s" % (
user_info, stack_trace, request_repr)) user_info, stack_trace, request_repr))

View File

@ -77,7 +77,8 @@ def submit_feedback(request, deployment, message=REQ(validator=check_dict([]))):
content += message['content'] content += message['content']
internal_send_message("feedback@zulip.com", "stream", "support", subject, content) internal_send_message(realm_for_email("feedback@zulip.com"), "feedback@zulip.com",
"stream", "support", subject, content)
return HttpResponse(message['sender_email']) return HttpResponse(message['sender_email'])