email: Apply message content visibility settings to digest too.

Fixes #33190.
This commit is contained in:
opmkumar
2025-04-14 21:07:42 +05:30
committed by Tim Abbott
parent 8c556cafbb
commit 8482a9480b
4 changed files with 184 additions and 20 deletions

View File

@@ -14,7 +14,10 @@ from django.utils.translation import gettext as _
from confirmation.models import one_click_unsubscribe_link
from zerver.context_processors import common_context
from zerver.lib.email_notifications import build_message_list
from zerver.lib.email_notifications import (
build_message_list,
message_content_allowed_in_missedmessage_emails,
)
from zerver.lib.logging_util import log_to_file
from zerver.lib.message import get_last_message_id
from zerver.lib.queue import queue_json_publish_rollback_unsafe
@@ -28,6 +31,7 @@ from zerver.models import (
Stream,
Subscription,
UserActivityInterval,
UserMessage,
UserProfile,
)
from zerver.models.realm_audit_logs import AuditLogEventType
@@ -268,8 +272,21 @@ def gather_new_streams(
return len(new_streams), {"html": channels_html, "plain": channels_plain}
def enough_traffic(hot_conversations: int, new_streams: int) -> bool:
return bool(hot_conversations or new_streams)
def get_new_messages_count(user: UserProfile, threshold: datetime) -> int:
count = UserMessage.objects.filter(
user_profile=user, message__date_sent__gte=threshold, message__sender__is_bot=False
).count()
return count
def enough_traffic(
hot_conversations: int, new_streams: int, new_messages: int, show_message_content: bool
) -> bool:
if new_streams > 0:
return True
if not show_message_content:
return new_messages > 0
return hot_conversations > 0
def get_user_stream_map(user_ids: list[int], cutoff_date: datetime) -> dict[int, set[int]]:
@@ -350,33 +367,46 @@ def bulk_get_digest_context(
user_stream_map = get_user_stream_map(user_ids, cutoff_date)
for user in users:
stream_ids = user_stream_map[user.id]
recent_topics = []
for stream_id in stream_ids:
recent_topics += get_recent_topics(realm.id, stream_id, cutoff_date)
hot_topics = get_hot_topics(recent_topics, stream_ids)
context = common_context(user)
# Start building email template data.
unsubscribe_link = one_click_unsubscribe_link(user, "digest")
context.update(unsubscribe_link=unsubscribe_link)
# Get context data for hot conversations.
context["hot_conversations"] = [
hot_topic.teaser_data(user, stream_id_map) for hot_topic in hot_topics
]
# Gather new streams.
new_streams_count, new_streams = gather_new_streams(
realm=realm,
recently_created_streams=recently_created_streams,
can_access_public=user.can_access_public_streams(),
)
context["new_channels"] = new_streams
context["new_streams_count"] = new_streams_count
context[
"message_content_disabled_by_realm"
] = not realm.message_content_allowed_in_email_notifications
context[
"message_content_disabled_by_user"
] = not user.message_content_in_email_notifications
if not message_content_allowed_in_missedmessage_emails(user):
# Count new messages when message content is hidden in email notifications.
context["new_messages_count"] = get_new_messages_count(user, cutoff_date)
context["hot_conversations"] = []
context["show_message_content"] = False
else:
# Otherwise, get context data for hot conversations.
stream_ids = user_stream_map[user.id]
recent_topics = []
for stream_id in stream_ids:
recent_topics += get_recent_topics(realm.id, stream_id, cutoff_date)
hot_topics = get_hot_topics(recent_topics, stream_ids)
context["hot_conversations"] = [
hot_topic.teaser_data(user, stream_id_map) for hot_topic in hot_topics
]
context["new_channels"] = new_streams
context["new_messages_count"] = 0
context["show_message_content"] = True
yield user, context
@@ -400,7 +430,12 @@ def bulk_handle_digest_email(user_ids: list[int], cutoff: float) -> None:
for user, context in bulk_get_digest_context(users, cutoff):
# We don't want to send emails containing almost no information.
if not enough_traffic(len(context["hot_conversations"]), context["new_streams_count"]):
if not enough_traffic(
len(context["hot_conversations"]),
context["new_streams_count"],
context["new_messages_count"],
context["show_message_content"],
):
continue
digest_users.append(user)