2013-10-21 23:25:53 +02:00
|
|
|
from __future__ import absolute_import
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Callable, Dict, Iterable, List, Set, Tuple, Text
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
import datetime
|
2017-02-26 04:44:22 +01:00
|
|
|
import pytz
|
2016-06-04 21:50:32 +02:00
|
|
|
import six
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2016-06-04 21:50:32 +02:00
|
|
|
from django.db.models import Q, QuerySet
|
2013-10-21 23:25:53 +02:00
|
|
|
from django.template import loader
|
2013-11-16 00:54:12 +01:00
|
|
|
from django.conf import settings
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2017-03-19 01:00:15 +01:00
|
|
|
from zerver.lib.notifications import build_message_list, hash_util_encode, \
|
2017-05-04 03:11:47 +02:00
|
|
|
one_click_unsubscribe_link
|
2017-05-04 03:23:33 +02:00
|
|
|
from zerver.lib.send_email import display_email, send_future_email
|
2013-10-21 23:25:53 +02:00
|
|
|
from zerver.models import UserProfile, UserMessage, Recipient, Stream, \
|
2014-01-24 23:30:53 +01:00
|
|
|
Subscription, get_active_streams
|
2016-11-08 10:07:47 +01:00
|
|
|
from zerver.context_processors import common_context
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2014-01-08 17:33:12 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log_format = "%(asctime)s: %(message)s"
|
|
|
|
logging.basicConfig(format=log_format)
|
|
|
|
|
|
|
|
formatter = logging.Formatter(log_format)
|
|
|
|
file_handler = logging.FileHandler(settings.DIGEST_LOG_PATH)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
|
2013-10-21 23:25:53 +02:00
|
|
|
# Digests accumulate 4 types of interesting traffic for a user:
|
|
|
|
# 1. Missed PMs
|
|
|
|
# 2. New streams
|
|
|
|
# 3. New users
|
|
|
|
# 4. Interesting stream traffic, as determined by the longest and most
|
|
|
|
# diversely comment upon topics.
|
|
|
|
|
|
|
|
def gather_hot_conversations(user_profile, stream_messages):
|
2016-06-04 21:50:32 +02:00
|
|
|
# type: (UserProfile, QuerySet) -> List[Dict[str, Any]]
|
2013-10-21 23:25:53 +02:00
|
|
|
# Gather stream conversations of 2 types:
|
|
|
|
# 1. long conversations
|
|
|
|
# 2. conversations where many different people participated
|
|
|
|
#
|
|
|
|
# Returns a list of dictionaries containing the templating
|
|
|
|
# information for each hot conversation.
|
|
|
|
|
2017-05-07 17:07:14 +02:00
|
|
|
conversation_length = defaultdict(int) # type: Dict[Tuple[int, Text], int]
|
|
|
|
conversation_diversity = defaultdict(set) # type: Dict[Tuple[int, Text], Set[Text]]
|
2013-10-21 23:25:53 +02:00
|
|
|
for user_message in stream_messages:
|
2013-12-31 22:45:21 +01:00
|
|
|
if not user_message.message.sent_by_human():
|
|
|
|
# Don't include automated messages in the count.
|
|
|
|
continue
|
|
|
|
|
2013-10-21 23:25:53 +02:00
|
|
|
key = (user_message.message.recipient.type_id,
|
|
|
|
user_message.message.subject)
|
|
|
|
conversation_diversity[key].add(
|
|
|
|
user_message.message.sender.full_name)
|
|
|
|
conversation_length[key] += 1
|
|
|
|
|
2016-01-25 01:27:18 +01:00
|
|
|
diversity_list = list(conversation_diversity.items())
|
2013-10-21 23:25:53 +02:00
|
|
|
diversity_list.sort(key=lambda entry: len(entry[1]), reverse=True)
|
|
|
|
|
2016-01-25 01:27:18 +01:00
|
|
|
length_list = list(conversation_length.items())
|
2013-10-21 23:25:53 +02:00
|
|
|
length_list.sort(key=lambda entry: entry[1], reverse=True)
|
|
|
|
|
|
|
|
# Get up to the 4 best conversations from the diversity list
|
|
|
|
# and length list, filtering out overlapping conversations.
|
|
|
|
hot_conversations = [elt[0] for elt in diversity_list[:2]]
|
|
|
|
for candidate, _ in length_list:
|
|
|
|
if candidate not in hot_conversations:
|
|
|
|
hot_conversations.append(candidate)
|
|
|
|
if len(hot_conversations) >= 4:
|
|
|
|
break
|
|
|
|
|
2013-12-13 20:18:44 +01:00
|
|
|
# There was so much overlap between the diversity and length lists that we
|
|
|
|
# still have < 4 conversations. Try to use remaining diversity items to pad
|
|
|
|
# out the hot conversations.
|
|
|
|
num_convos = len(hot_conversations)
|
|
|
|
if num_convos < 4:
|
|
|
|
hot_conversations.extend([elt[0] for elt in diversity_list[num_convos:4]])
|
|
|
|
|
2013-10-21 23:25:53 +02:00
|
|
|
hot_conversation_render_payloads = []
|
|
|
|
for h in hot_conversations:
|
|
|
|
stream_id, subject = h
|
|
|
|
users = list(conversation_diversity[h])
|
|
|
|
count = conversation_length[h]
|
|
|
|
|
|
|
|
# We'll display up to 2 messages from the conversation.
|
2016-12-03 18:07:49 +01:00
|
|
|
first_few_messages = [user_message.message for user_message in
|
2016-12-11 14:30:45 +01:00
|
|
|
stream_messages.filter(
|
2017-01-24 06:02:39 +01:00
|
|
|
message__recipient__type_id=stream_id,
|
|
|
|
message__subject=subject)[:2]]
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2013-10-25 18:53:35 +02:00
|
|
|
teaser_data = {"participants": users,
|
2013-10-21 23:25:53 +02:00
|
|
|
"count": count - len(first_few_messages),
|
2016-12-02 08:15:16 +01:00
|
|
|
"first_few_messages": build_message_list(
|
2017-01-24 06:02:39 +01:00
|
|
|
user_profile, first_few_messages)}
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
hot_conversation_render_payloads.append(teaser_data)
|
2013-12-13 20:26:44 +01:00
|
|
|
return hot_conversation_render_payloads
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
def gather_new_users(user_profile, threshold):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (UserProfile, datetime.datetime) -> Tuple[int, List[Text]]
|
2013-10-21 23:25:53 +02:00
|
|
|
# Gather information on users in the realm who have recently
|
|
|
|
# joined.
|
2016-07-27 01:45:29 +02:00
|
|
|
if user_profile.realm.is_zephyr_mirror_realm:
|
2017-05-07 17:07:14 +02:00
|
|
|
new_users = [] # type: List[UserProfile]
|
2013-12-16 22:26:04 +01:00
|
|
|
else:
|
|
|
|
new_users = list(UserProfile.objects.filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
realm=user_profile.realm, date_joined__gt=threshold,
|
|
|
|
is_bot=False))
|
2013-10-25 18:53:35 +02:00
|
|
|
user_names = [user.full_name for user in new_users]
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2013-10-25 18:53:35 +02:00
|
|
|
return len(user_names), user_names
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
def gather_new_streams(user_profile, threshold):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (UserProfile, datetime.datetime) -> Tuple[int, Dict[str, List[Text]]]
|
2016-07-27 01:45:29 +02:00
|
|
|
if user_profile.realm.is_zephyr_mirror_realm:
|
2017-05-07 17:07:14 +02:00
|
|
|
new_streams = [] # type: List[Stream]
|
2013-12-13 19:46:47 +01:00
|
|
|
else:
|
2014-01-24 23:30:53 +01:00
|
|
|
new_streams = list(get_active_streams(user_profile.realm).filter(
|
2017-01-24 07:06:13 +01:00
|
|
|
invite_only=False, date_created__gt=threshold))
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2017-05-07 17:07:14 +02:00
|
|
|
base_url = u"%s/ # narrow/stream/" % (user_profile.realm.uri,)
|
2013-10-25 18:53:35 +02:00
|
|
|
|
|
|
|
streams_html = []
|
|
|
|
streams_plain = []
|
|
|
|
|
2013-10-21 23:25:53 +02:00
|
|
|
for stream in new_streams:
|
2017-03-19 01:00:15 +01:00
|
|
|
narrow_url = base_url + hash_util_encode(stream.name)
|
2016-06-13 10:14:33 +02:00
|
|
|
stream_link = u"<a href='%s'>%s</a>" % (narrow_url, stream.name)
|
2013-10-25 18:53:35 +02:00
|
|
|
streams_html.append(stream_link)
|
|
|
|
streams_plain.append(stream.name)
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
return len(new_streams), {"html": streams_html, "plain": streams_plain}
|
|
|
|
|
|
|
|
def enough_traffic(unread_pms, hot_conversations, new_streams, new_users):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text, int, int) -> bool
|
2013-10-21 23:25:53 +02:00
|
|
|
if unread_pms or hot_conversations:
|
|
|
|
# If you have any unread traffic, good enough.
|
|
|
|
return True
|
|
|
|
if new_streams and new_users:
|
|
|
|
# If you somehow don't have any traffic but your realm did get
|
|
|
|
# new streams and users, good enough.
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def handle_digest_email(user_profile_id, cutoff):
|
2016-10-11 14:20:05 +02:00
|
|
|
# type: (int, float) -> None
|
2016-11-28 23:29:01 +01:00
|
|
|
user_profile = UserProfile.objects.get(id=user_profile_id)
|
2013-10-21 23:25:53 +02:00
|
|
|
# Convert from epoch seconds to a datetime object.
|
2017-02-26 04:44:22 +01:00
|
|
|
cutoff_date = datetime.datetime.fromtimestamp(int(cutoff), tz=pytz.utc)
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
all_messages = UserMessage.objects.filter(
|
|
|
|
user_profile=user_profile,
|
2016-06-04 21:50:32 +02:00
|
|
|
message__pub_date__gt=cutoff_date).order_by("message__pub_date")
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2017-05-04 23:37:01 +02:00
|
|
|
context = common_context(user_profile)
|
2016-11-08 10:07:47 +01:00
|
|
|
|
2013-10-21 23:25:53 +02:00
|
|
|
# Start building email template data.
|
2017-05-04 23:37:01 +02:00
|
|
|
context.update({
|
2013-12-02 01:39:10 +01:00
|
|
|
'name': user_profile.full_name,
|
|
|
|
'unsubscribe_link': one_click_unsubscribe_link(user_profile, "digest")
|
2017-01-24 06:34:26 +01:00
|
|
|
})
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
# Gather recent missed PMs, re-using the missed PM email logic.
|
2013-12-04 23:49:43 +01:00
|
|
|
# You can't have an unread message that you sent, but when testing
|
|
|
|
# this causes confusion so filter your messages out.
|
|
|
|
pms = all_messages.filter(
|
2016-12-02 08:15:05 +01:00
|
|
|
~Q(message__recipient__type=Recipient.STREAM) &
|
|
|
|
~Q(message__sender=user_profile))
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
# Show up to 4 missed PMs.
|
|
|
|
pms_limit = 4
|
|
|
|
|
2017-05-04 23:37:01 +02:00
|
|
|
context['unread_pms'] = build_message_list(
|
2013-10-21 23:25:53 +02:00
|
|
|
user_profile, [pm.message for pm in pms[:pms_limit]])
|
2017-05-04 23:37:01 +02:00
|
|
|
context['remaining_unread_pms_count'] = min(0, len(pms) - pms_limit)
|
2013-10-21 23:25:53 +02:00
|
|
|
|
2016-12-03 18:07:49 +01:00
|
|
|
home_view_recipients = [sub.recipient for sub in
|
2016-11-30 14:17:35 +01:00
|
|
|
Subscription.objects.filter(
|
|
|
|
user_profile=user_profile,
|
|
|
|
active=True,
|
|
|
|
in_home_view=True)]
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
stream_messages = all_messages.filter(
|
|
|
|
message__recipient__type=Recipient.STREAM,
|
|
|
|
message__recipient__in=home_view_recipients)
|
|
|
|
|
|
|
|
# Gather hot conversations.
|
2017-05-04 23:37:01 +02:00
|
|
|
context["hot_conversations"] = gather_hot_conversations(
|
2013-10-21 23:25:53 +02:00
|
|
|
user_profile, stream_messages)
|
|
|
|
|
|
|
|
# Gather new streams.
|
|
|
|
new_streams_count, new_streams = gather_new_streams(
|
2016-06-04 21:50:32 +02:00
|
|
|
user_profile, cutoff_date)
|
2017-05-04 23:37:01 +02:00
|
|
|
context["new_streams"] = new_streams
|
|
|
|
context["new_streams_count"] = new_streams_count
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
# Gather users who signed up recently.
|
|
|
|
new_users_count, new_users = gather_new_users(
|
2016-06-04 21:50:32 +02:00
|
|
|
user_profile, cutoff_date)
|
2017-05-04 23:37:01 +02:00
|
|
|
context["new_users"] = new_users
|
2013-10-21 23:25:53 +02:00
|
|
|
|
|
|
|
# We don't want to send emails containing almost no information.
|
2017-05-04 23:37:01 +02:00
|
|
|
if enough_traffic(context["unread_pms"], context["hot_conversations"],
|
2013-10-21 23:25:53 +02:00
|
|
|
new_streams_count, new_users_count):
|
2014-01-08 17:33:12 +01:00
|
|
|
logger.info("Sending digest email for %s" % (user_profile.email,))
|
2017-05-04 02:06:31 +02:00
|
|
|
# Send now, as a ScheduledJob
|
2017-05-04 23:37:01 +02:00
|
|
|
send_future_email('zerver/emails/digest', display_email(user_profile), context=context)
|