From e8a608f73306a4a2f8eeacc4dfc29b0fec129a44 Mon Sep 17 00:00:00 2001 From: Preston Hansen Date: Sun, 27 Aug 2017 09:30:48 -0500 Subject: [PATCH] management: Move enqueue_digest_email handler to digest. --- zerver/lib/digest.py | 30 +++++++++++++++++-- .../commands/enqueue_digest_emails.py | 27 ++--------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py index 2cfeb5d92d..3c0f721925 100644 --- a/zerver/lib/digest.py +++ b/zerver/lib/digest.py @@ -9,12 +9,14 @@ import six from django.db.models import Q, QuerySet from django.template import loader from django.conf import settings +from django.utils.timezone import now as timezone_now from zerver.lib.notifications import build_message_list, hash_util_encode, \ one_click_unsubscribe_link from zerver.lib.send_email import send_future_email, FromAddress -from zerver.models import UserProfile, UserActivity, UserMessage, Recipient, Stream, \ - Subscription, get_active_streams, get_user_profile_by_id +from zerver.models import UserProfile, UserMessage, Recipient, Stream, \ + Subscription, UserActivity, get_active_streams, get_user_profile_by_id, \ + Realm from zerver.context_processors import common_context from zerver.lib.queue import queue_json_publish @@ -31,6 +33,9 @@ logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) +VALID_DIGEST_DAY = 1 # Tuesdays +DIGEST_CUTOFF = 5 + # Digests accumulate 4 types of interesting traffic for a user: # 1. Missed PMs # 2. New streams @@ -68,6 +73,27 @@ def queue_digest_recipient(user_profile, cutoff): "cutoff": cutoff.strftime('%s')} queue_json_publish("digest_emails", event, lambda event: None) +def enqueue_emails(cutoff): + # type: (datetime.datetime) -> None + # To be really conservative while we don't have user timezones or + # special-casing for companies with non-standard workweeks, only + # try to send mail on Tuesdays. + if timezone_now().weekday() != VALID_DIGEST_DAY: + return + + for realm in Realm.objects.filter(deactivated=False, show_digest_email=True): + if not should_process_digest(realm.string_id): + continue + + user_profiles = UserProfile.objects.filter( + realm=realm, is_active=True, is_bot=False, enable_digest_emails=True) + + for user_profile in user_profiles: + if inactive_since(user_profile, cutoff): + queue_digest_recipient(user_profile, cutoff) + logger.info("%s is inactive, queuing for potential digest" % ( + user_profile.email,)) + def gather_hot_conversations(user_profile, stream_messages): # type: (UserProfile, QuerySet) -> List[Dict[str, Any]] # Gather stream conversations of 2 types: diff --git a/zerver/management/commands/enqueue_digest_emails.py b/zerver/management/commands/enqueue_digest_emails.py index 4077d79ac2..0af69a77d2 100644 --- a/zerver/management/commands/enqueue_digest_emails.py +++ b/zerver/management/commands/enqueue_digest_emails.py @@ -8,8 +8,7 @@ from django.conf import settings from django.core.management.base import BaseCommand from django.utils.timezone import now as timezone_now -from zerver.models import UserProfile, Realm -from zerver.lib.digest import inactive_since, should_process_digest, queue_digest_recipient +from zerver.lib.digest import enqueue_emails, DIGEST_CUTOFF ## Logging setup ## @@ -24,9 +23,6 @@ logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(file_handler) -VALID_DIGEST_DAY = 1 # Tuesdays -DIGEST_CUTOFF = 5 - class Command(BaseCommand): help = """Enqueue digest emails for users that haven't checked the app in a while. @@ -34,22 +30,5 @@ in a while. def handle(self, *args, **options): # type: (*Any, **Any) -> None - # To be really conservative while we don't have user timezones or - # special-casing for companies with non-standard workweeks, only - # try to send mail on Tuesdays. - if timezone_now().weekday() != VALID_DIGEST_DAY: - return - - for realm in Realm.objects.filter(deactivated=False, show_digest_email=True): - if not should_process_digest(realm.string_id): - continue - - user_profiles = UserProfile.objects.filter( - realm=realm, is_active=True, is_bot=False, enable_digest_emails=True) - - for user_profile in user_profiles: - cutoff = timezone_now() - datetime.timedelta(days=DIGEST_CUTOFF) - if inactive_since(user_profile, cutoff): - queue_digest_recipient(user_profile, cutoff) - logger.info("%s is inactive, queuing for potential digest" % ( - user_profile.email,)) + cutoff = timezone_now() - datetime.timedelta(days=DIGEST_CUTOFF) + enqueue_emails(cutoff)