Compare commits

...

1 Commits

Author SHA1 Message Date
Luke Faraone
2738b7f3a8 email_mirror: Use internal_send_message instead of the API
Previously, the email mirror queue worker used the API bindings to send
messages to Zulip, as if it were any other API client.

This is inefficient since we're running the worker inside the Django
context on a machine with database access; we can instead just use the
internal message-sending functions we use elsewhere. This also resolves
potential issues with SSL certificates, etc. that might occur when we
were previously making a HTTPS connection.

(imported from commit 3f0d200eec66d926a8c6b3b33b825ac5d0edfe12)
2014-03-10 14:04:08 -04:00

View File

@@ -1,55 +1,19 @@
from __future__ import absolute_import from __future__ import absolute_import
import email
import logging import logging
import os
import re import re
import sys
from email.header import decode_header from email.header import decode_header
from django.conf import settings from django.conf import settings
from zerver.lib.actions import decode_email_address from zerver.lib.actions import decode_email_address, internal_send_message
from zerver.lib.notifications import convert_html_to_markdown from zerver.lib.notifications import convert_html_to_markdown
from zerver.lib.upload import upload_message_image from zerver.lib.upload import upload_message_image
from zerver.models import Stream, get_user_profile_by_email, UserProfile from zerver.models import Stream
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../api"))
import zulip
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
email_gateway_user = None
api_key = None
try:
email_gateway_user = get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT)
api_key = email_gateway_user.api_key
except UserProfile.DoesNotExist:
print "No configured %s user" % (settings.EMAIL_GATEWAY_BOT,)
sys.exit(1)
if settings.DEPLOYED:
prod_api_client = zulip.Client(
client="ZulipEmailMirror/0.1",
site="https://zulip.com",
email=settings.EMAIL_GATEWAY_BOT,
api_key=api_key)
api_client = zulip.Client(
client="ZulipEmailMirror/0.1",
site=settings.EXTERNAL_API_URI,
email=settings.EMAIL_GATEWAY_BOT,
api_key=api_key)
else:
api_client = prod_api_client = zulip.Client(
client="ZulipEmailMirror/0.1",
site=settings.EXTERNAL_API_URI,
email=settings.EMAIL_GATEWAY_BOT,
api_key=api_key)
def redact_stream(error_message): def redact_stream(error_message):
domain = settings.EMAIL_GATEWAY_PATTERN.rsplit('@')[-1] domain = settings.EMAIL_GATEWAY_PATTERN.rsplit('@')[-1]
stream_match = re.search(r'\b(.*?)@' + domain, error_message) stream_match = re.search(r'\b(.*?)@' + domain, error_message)
@@ -84,22 +48,13 @@ class ZulipEmailForwardError(Exception):
pass pass
def send_zulip(stream, topic, content): def send_zulip(stream, topic, content):
if stream.realm.domain != 'zulip.com' and not settings.ENTERPRISE: internal_send_message(
client = prod_api_client settings.EMAIL_GATEWAY_BOT,
else: "stream",
client = api_client stream.name,
topic[:60],
message_data = { content[:2000],
"type": "stream", stream.realm)
"content": content[:2000],
"subject": topic[:60],
"to": stream.name,
"domain": stream.realm.domain
}
response = client.send_message(message_data)
if response["result"] != "success":
raise ZulipEmailForwardError(response["msg"])
def valid_stream(stream_name, token): def valid_stream(stream_name, token):
try: try:
@@ -157,7 +112,7 @@ def extract_and_upload_attachments(message, realm):
if filename: if filename:
s3_url = upload_message_image(filename, content_type, s3_url = upload_message_image(filename, content_type,
part.get_payload(decode=True), part.get_payload(decode=True),
email_gateway_user, settings.EMAIL_GATEWAY_BOT,
target_realm=realm) target_realm=realm)
formatted_link = "[%s](%s)" % (filename, s3_url) formatted_link = "[%s](%s)" % (filename, s3_url)
attachment_links.append(formatted_link) attachment_links.append(formatted_link)