digest: On an app server, only send digest emails for domains it manages.

(imported from commit 9aea636a19eb6fdefacdc7dc393bf075a05862c5)
This commit is contained in:
Jessica McKellar 2013-12-19 13:02:41 -05:00
parent 235c4d5f8f
commit 61103d8349
2 changed files with 43 additions and 1 deletions

View File

@ -2321,6 +2321,15 @@ def send_future_email(recipients, email_html, email_text, subject,
'mail_client' is filled in by the decorator 'mail_client' is filled in by the decorator
""" """
# When sending real emails while testing locally, don't accidentally send
# emails to non-zulip.com users.
if not settings.DEPLOYED and \
settings.EMAIL_BACKEND != 'django.core.mail.backends.console.EmailBackend':
for recipient in recipients:
email = recipient.get("email")
if get_user_profile_by_email(email).realm.domain != "zulip.com":
raise ValueError("digest: refusing to send emails to non-zulip.com users.")
# message = {"from_email": "othello@zulip.com", # message = {"from_email": "othello@zulip.com",
# "from_name": "Othello", # "from_name": "Othello",
# "html": "<p>hello</p> there", # "html": "<p>hello</p> there",

View File

@ -52,6 +52,33 @@ def queue_digest_recipient(user_profile, cutoff):
"cutoff": cutoff.strftime('%s')} "cutoff": cutoff.strftime('%s')}
queue_json_publish("digest_emails", event, lambda event: None) queue_json_publish("digest_emails", event, lambda event: None)
def domains_for_this_deployment():
if settings.DEPLOYED and not settings.ENTERPRISE:
# Enterprise deployments don't have a Deployment entry.
# Only send zulip.com digests on staging.
from zilencer.models import Deployment
site_url = settings.EXTERNAL_URI_SCHEME + settings.EXTERNAL_HOST.rstrip("/")
try:
deployment = Deployment.objects.select_related('realms').get(
base_site_url__startswith=site_url)
except Deployment.DoesNotExist:
raise ValueError("digest: Unable to determine deployment.")
return (r.domain for r in deployment.realms.all())
# Enterprise and localhost.
return ()
def should_process_digest(domain, deployment_domains):
if settings.ENTERPRISE:
# Enterprise. We ship with a zulip.com realm for the feedback bot, but
# don't try to send e-mails to it.
return domain != "zulip.com"
elif settings.DEPLOYED:
return domain in deployment_domains
else:
# Localhost.
return True
class Command(BaseCommand): class Command(BaseCommand):
help = """Enqueue digest emails for users that haven't checked the app help = """Enqueue digest emails for users that haven't checked the app
in a while. in a while.
@ -63,7 +90,13 @@ in a while.
if datetime.datetime.utcnow().weekday() not in VALID_DIGEST_DAYS: if datetime.datetime.utcnow().weekday() not in VALID_DIGEST_DAYS:
return return
for domain in ["zulip.com"]: digest_domains = ["zulip.com"]
deployment_domains = domains_for_this_deployment()
for domain in digest_domains:
if not should_process_digest(domain, deployment_domains):
continue
user_profiles = UserProfile.objects.filter( user_profiles = UserProfile.objects.filter(
realm=get_realm(domain), is_active=True, is_bot=False, realm=get_realm(domain), is_active=True, is_bot=False,
enable_digest_emails=True) enable_digest_emails=True)