email_mirror: Use internal_send_stream_message().

This is just a refactoring to the more modern API
for sending internal messages.

To make this work we now plumb the email_gateway
flag through `internal_send_stream_message` instead
of `internal_send_message`.

We also change `send_zulip` to have its callers
pass in a full UserProfile object (which one of
them already had).
This commit is contained in:
Steve Howell 2020-02-10 15:49:19 +00:00 committed by Tim Abbott
parent 6922eef380
commit 28a8ffbc4c
2 changed files with 23 additions and 15 deletions

View File

@ -2516,8 +2516,7 @@ def internal_prep_private_message(realm: Realm,
)
def internal_send_message(realm: Realm, sender_email: str, recipient_type_name: str,
recipients: str, topic_name: str, content: str,
email_gateway: Optional[bool]=False) -> Optional[int]:
recipients: str, topic_name: str, content: str) -> Optional[int]:
"""internal_send_message should only be used where `sender_email` is a
system bot."""
@ -2543,7 +2542,7 @@ def internal_send_message(realm: Realm, sender_email: str, recipient_type_name:
if msg is None:
return None
message_ids = do_send_messages([msg], email_gateway=email_gateway)
message_ids = do_send_messages([msg])
return message_ids[0]
def internal_send_private_message(realm: Realm,
@ -2557,9 +2556,13 @@ def internal_send_private_message(realm: Realm,
return message_ids[0]
def internal_send_stream_message(
realm: Realm, sender: UserProfile,
stream: Stream, topic: str, content: str
) -> Optional[int]:
realm: Realm,
sender: UserProfile,
stream: Stream,
topic: str,
content: str,
email_gateway: Optional[bool]=False) -> Optional[int]:
message = internal_prep_stream_message(
realm, sender, stream,
topic, content
@ -2567,7 +2570,7 @@ def internal_send_stream_message(
if message is None:
return None
message_ids = do_send_messages([message])
message_ids = do_send_messages([message], email_gateway=email_gateway)
return message_ids[0]
def internal_send_stream_message_by_name(

View File

@ -10,7 +10,7 @@ import email.message as message
from django.conf import settings
from django.utils.timezone import timedelta, now as timezone_now
from zerver.lib.actions import internal_send_message, internal_send_private_message, \
from zerver.lib.actions import internal_send_private_message, \
internal_send_stream_message, internal_send_huddle_message, \
truncate_body, truncate_topic
from zerver.lib.email_mirror_helpers import decode_email_address, \
@ -65,8 +65,12 @@ def report_to_zulip(error_message: str) -> None:
return
error_bot = get_system_bot(settings.ERROR_BOT)
error_stream = Stream.objects.get(name="errors", realm=error_bot.realm)
send_zulip(settings.ERROR_BOT, error_stream, "email mirror error",
"""~~~\n%s\n~~~""" % (error_message,))
send_zulip(
error_bot,
error_stream,
"email mirror error",
"""~~~\n%s\n~~~""" % (error_message,)
)
def log_and_report(email_message: message.Message, error_message: str, to: Optional[str]) -> None:
recipient = to or "No recipient found"
@ -166,12 +170,11 @@ def handle_header_content(content: str) -> str:
class ZulipEmailForwardUserError(ZulipEmailForwardError):
pass
def send_zulip(sender: str, stream: Stream, topic: str, content: str) -> None:
internal_send_message(
def send_zulip(sender: UserProfile, stream: Stream, topic: str, content: str) -> None:
internal_send_stream_message(
stream.realm,
sender,
"stream",
stream.name,
stream,
truncate_topic(topic),
truncate_body(content),
email_gateway=True)
@ -337,7 +340,9 @@ def process_stream_message(to: str, message: message.Message) -> None:
options['include_quotes'] = is_forwarded(subject_header)
body = construct_zulip_body(message, stream.realm, **options)
send_zulip(settings.EMAIL_GATEWAY_BOT, stream, subject, body)
send_zulip(
get_system_bot(settings.EMAIL_GATEWAY_BOT),
stream, subject, body)
logger.info("Successfully processed email to %s (%s)" % (
stream.name, stream.realm.string_id))