Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
email digests: Extract get_new_streams.
Steve Howelle0b451730a43
diff --git a/zerver/lib/digest.py b/zerver/lib/digest.pyindex fdd2182dbf9..3dba9815a89 100644--- a/zerver/lib/digest.py+++ b/zerver/lib/digest.py@@ -194,17 +194,21 @@ def get_hot_topics(return hot_topics-def gather_new_streams(user_profile: UserProfile,- threshold: datetime.datetime) -> Tuple[int, Dict[str, List[str]]]:- if user_profile.is_guest:- new_streams = list(get_active_streams(user_profile.realm).filter(- is_web_public=True, date_created__gt=threshold))-- elif user_profile.can_access_public_streams():- new_streams = list(get_active_streams(user_profile.realm).filter(- invite_only=False, date_created__gt=threshold))-- base_url = f"{user_profile.realm.uri}/#narrow/stream/"+def get_recent_streams(realm: Realm, threshold: datetime.datetime) -> List[Stream]:+ fields = ["id", "name", "is_web_public", "invite_only"]+ return list(get_active_streams(realm).filter(date_created__gt=threshold).only(*fields))++def gather_new_streams(+ realm: Realm,+ recent_streams: List[Stream], # streams only need id and name+ can_access_public: bool,+) -> Tuple[int, Dict[str, List[str]]]:+ if can_access_public:+ new_streams = [stream for stream in recent_streams if not stream.invite_only]+ else:+ new_streams = [stream for stream in recent_streams if stream.is_web_public]++ base_url = f"{realm.uri}/#narrow/stream/"streams_html = []streams_plain = []@@ -244,6 +248,12 @@ def get_slim_stream_map(stream_ids: Set[int]) -> Dict[int, Stream]:return {stream.id: stream for stream in streams}def bulk_get_digest_context(users: List[UserProfile], cutoff: float) -> Dict[int, Dict[str, Any]]:+ # We expect a non-empty list of users all from the same realm.+ assert(users)+ realm = users[0].realm+ for user in users:+ assert user.realm_id == realm.id+# Convert from epoch seconds to a datetime object.cutoff_date = datetime.datetime.fromtimestamp(int(cutoff), tz=datetime.timezone.utc)@@ -269,6 +279,8 @@ def bulk_get_digest_context(users: List[UserProfile], cutoff: float) -> Dict[intstream_map = get_slim_stream_map(all_stream_ids)+ recent_streams = get_recent_streams(realm, cutoff_date)+for user in users:stream_ids = user_stream_map[user.id]@@ -287,7 +299,11 @@ def bulk_get_digest_context(users: List[UserProfile], cutoff: float) -> Dict[int]# Gather new streams.- new_streams_count, new_streams = gather_new_streams(user, cutoff_date)+ new_streams_count, new_streams = gather_new_streams(+ realm=realm,+ recent_streams=recent_streams,+ can_access_public=user.can_access_public_streams()+ )context["new_streams"] = new_streamscontext["new_streams_count"] = new_streams_countdiff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.pyindex 01b4020b9da..a80eca92b11 100644--- a/zerver/tests/test_digest.py+++ b/zerver/tests/test_digest.py@@ -15,6 +15,7 @@ from zerver.lib.digest import (enqueue_emails,gather_new_streams,get_modified_streams,+ get_recent_streams,handle_digest_email,)from zerver.lib.message import get_last_message_id@@ -182,7 +183,7 @@ class TestDigestEmailMessages(ZulipTestCase):with cache_tries_captured() as cache_tries:bulk_handle_digest_email(digest_user_ids, cutoff)- self.assert_length(queries, 15)+ self.assert_length(queries, 12)self.assert_length(cache_tries, 0)self.assertEqual(mock_send_future_email.call_count, len(digest_users))@@ -404,16 +405,33 @@ class TestDigestEmailMessages(ZulipTestCase):stream.date_created = timezone_now()stream.save()- stream_count, stream_info = gather_new_streams(cordelia, cutoff)+ realm = cordelia.realm++ recent_streams = get_recent_streams(realm, cutoff)+ stream_count, stream_info = gather_new_streams(realm, recent_streams, can_access_public=True)self.assertEqual(stream_count, 1)expected_html = f"<a href='http://zulip.testserver/#narrow/stream/{stream.id}-New-stream'>New stream</a>"self.assertEqual(stream_info['html'][0], expected_html)+ # guests don't see our stream+ stream_count, stream_info = gather_new_streams(realm, recent_streams, can_access_public=False)+ self.assertEqual(stream_count, 0)+ self.assertEqual(stream_info['html'], [])++ # but they do if we make it web public+ stream.is_web_public = True+ stream.save()++ recent_streams = get_recent_streams(realm, cutoff)+ stream_count, stream_info = gather_new_streams(realm, recent_streams, can_access_public=True)+ self.assertEqual(stream_count, 1)+# Make the stream appear to be older.stream.date_created = timezone_now() - datetime.timedelta(days=7)stream.save()- stream_count, stream_info = gather_new_streams(cordelia, cutoff)+ recent_streams = get_recent_streams(realm, cutoff)+ stream_count, stream_info = gather_new_streams(realm, recent_streams, can_access_public=True)self.assertEqual(stream_count, 0)self.assertEqual(stream_info['html'], [])