refactor: Extract get_digest_context.

This eliminates the union type and boolean parameter,
and it makes it a bit easier to migrate to a
bulk-get approach.
This commit is contained in:
Steve Howell 2020-11-03 19:48:57 +00:00 committed by Tim Abbott
parent 217967f743
commit e31326c823
2 changed files with 13 additions and 15 deletions

View File

@ -2,7 +2,7 @@ import datetime
import logging import logging
from collections import defaultdict from collections import defaultdict
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Dict, List, Set, Tuple, Union from typing import Any, Dict, List, Set, Tuple
from django.conf import settings from django.conf import settings
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
@ -218,10 +218,7 @@ def gather_new_streams(user_profile: UserProfile,
def enough_traffic(hot_conversations: str, new_streams: int) -> bool: def enough_traffic(hot_conversations: str, new_streams: int) -> bool:
return bool(hot_conversations or new_streams) return bool(hot_conversations or new_streams)
def handle_digest_email(user_profile_id: int, cutoff: float, def get_digest_context(user_profile: UserProfile, cutoff: float) -> Dict[str, Any]:
render_to_web: bool = False) -> Union[None, Dict[str, Any]]:
user_profile = get_user_profile_by_id(user_profile_id)
# Convert from epoch seconds to a datetime object. # Convert from epoch seconds to a datetime object.
cutoff_date = datetime.datetime.fromtimestamp(int(cutoff), tz=datetime.timezone.utc) cutoff_date = datetime.datetime.fromtimestamp(int(cutoff), tz=datetime.timezone.utc)
@ -255,19 +252,20 @@ def handle_digest_email(user_profile_id: int, cutoff: float,
context["new_streams"] = new_streams context["new_streams"] = new_streams
context["new_streams_count"] = new_streams_count context["new_streams_count"] = new_streams_count
# TODO: Set has_preheader if we want to include a preheader.
if render_to_web:
return context return context
def handle_digest_email(user_profile_id: int, cutoff: float) -> None:
user_profile = get_user_profile_by_id(user_profile_id)
context = get_digest_context(user_profile, cutoff)
# We don't want to send emails containing almost no information. # We don't want to send emails containing almost no information.
if enough_traffic(context["hot_conversations"], new_streams_count): if enough_traffic(context["hot_conversations"], context["new_streams_count"]):
logger.info("Sending digest email for user %s", user_profile.id) logger.info("Sending digest email for user %s", user_profile.id)
# Send now, as a ScheduledEmail # Send now, as a ScheduledEmail
send_future_email('zerver/emails/digest', user_profile.realm, to_user_ids=[user_profile.id], send_future_email('zerver/emails/digest', user_profile.realm, to_user_ids=[user_profile.id],
from_name="Zulip Digest", from_address=FromAddress.no_reply_placeholder, from_name="Zulip Digest", from_address=FromAddress.no_reply_placeholder,
context=context) context=context)
return None
def exclude_subscription_modified_streams(user_profile: UserProfile, def exclude_subscription_modified_streams(user_profile: UserProfile,
stream_ids: List[int], stream_ids: List[int],

View File

@ -7,14 +7,14 @@ from django.shortcuts import render
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from zerver.decorator import zulip_login_required from zerver.decorator import zulip_login_required
from zerver.lib.digest import DIGEST_CUTOFF, handle_digest_email from zerver.lib.digest import DIGEST_CUTOFF, get_digest_context
@zulip_login_required @zulip_login_required
def digest_page(request: HttpRequest) -> HttpResponse: def digest_page(request: HttpRequest) -> HttpResponse:
user_profile_id = request.user.id user_profile = request.user
cutoff = time.mktime((timezone_now() - timedelta(days=DIGEST_CUTOFF)).timetuple()) cutoff = time.mktime((timezone_now() - timedelta(days=DIGEST_CUTOFF)).timetuple())
context = handle_digest_email(user_profile_id, cutoff, render_to_web=True)
if context: context = get_digest_context(user_profile, cutoff)
context.update(physical_address=settings.PHYSICAL_ADDRESS) context.update(physical_address=settings.PHYSICAL_ADDRESS)
return render(request, 'zerver/digest_base.html', context=context) return render(request, 'zerver/digest_base.html', context=context)