Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

digest: Switch loop to early-abort for clarity.

Alex Vandiverb9f72bdd68a4
diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py
index 5f9e7fb38bd..1065f3f3c1c 100644
--- a/zerver/lib/digest.py
+++ b/zerver/lib/digest.py
@@ -3,7 +3,7 @@ import functools
import heapq
import logging
from collections import defaultdict
-from typing import Any, Collection, Dict, List, Set, Tuple
+from typing import Any, Collection, Dict, Iterator, List, Optional, Set, Tuple
from django.conf import settings
from django.db import transaction
@@ -383,18 +383,21 @@ def bulk_handle_digest_email(user_ids: List[int], cutoff: float) -> None:
context = context_map[user.id]
# We don't want to send emails containing almost no information.
- if enough_traffic(context["hot_conversations"], context["new_streams_count"]):
- digest_users.append(user)
- logger.info("Sending digest email for user %s", user.id)
- # Send now, as a ScheduledEmail
- send_future_email(
- "zerver/emails/digest",
- user.realm,
- to_user_ids=[user.id],
- from_name="Zulip Digest",
- from_address=FromAddress.no_reply_placeholder,
- context=context,
- )
+ if not enough_traffic(context["hot_conversations"], context["new_streams_count"]):
+ continue
+
+ digest_users.append(user)
+ logger.info("Sending digest email for user %s", user.id)
+
+ # Send now, as a ScheduledEmail
+ send_future_email(
+ "zerver/emails/digest",
+ user.realm,
+ to_user_ids=[user.id],
+ from_name="Zulip Digest",
+ from_address=FromAddress.no_reply_placeholder,
+ context=context,
+ )
bulk_write_realm_audit_logs(digest_users)
diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py
index 5395e8bf05f..4f43cf26cf6 100644
--- a/zerver/tests/test_digest.py
+++ b/zerver/tests/test_digest.py
@@ -136,6 +136,23 @@ class TestDigestEmailMessages(ZulipTestCase):
set(emailed_user_ids), {user_id for user_id in user_ids if user_id != hamlet.id}
)
+ @mock.patch("zerver.lib.digest.send_future_email")
+ def test_enough_traffic(self, mock_send_future_email: mock.MagicMock) -> None:
+ othello = self.example_user("othello")
+ self.subscribe(othello, "Verona")
+
+ in_the_future = timezone_now().timestamp() + 60
+
+ bulk_handle_digest_email([othello.id], in_the_future)
+ mock_send_future_email.assert_not_called()
+
+ with mock.patch(
+ "zerver.lib.digest.enough_traffic", return_value=True
+ ) as enough_traffic_mock:
+ bulk_handle_digest_email([othello.id], in_the_future)
+ mock_send_future_email.assert_called()
+ enough_traffic_mock.assert_called_once_with([], 0)
+
@mock.patch("zerver.lib.digest.enough_traffic")
@mock.patch("zerver.lib.digest.send_future_email")
def test_guest_user_multiple_stream_sender(