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: Support digest of web public streams for guest users.

Clara Dantas867428719245
diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py
index 84ba46360de..909d969654e 100644
--- a/zerver/lib/digest.py
+++ b/zerver/lib/digest.py
@@ -148,11 +148,13 @@ def gather_hot_conversations(user_profile: UserProfile, messages: List[Message])
def gather_new_streams(user_profile: UserProfile,
threshold: datetime.datetime) -> Tuple[int, Dict[str, List[str]]]:
- if user_profile.can_access_public_streams():
+ 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))
- else:
- new_streams = []
base_url = f"{user_profile.realm.uri}/#narrow/stream/"
diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py
index 45d498d45d8..3c4ff107c46 100644
--- a/zerver/lib/streams.py
+++ b/zerver/lib/streams.py
@@ -59,6 +59,7 @@ def create_stream_if_needed(realm: Realm,
stream_name: str,
*,
invite_only: bool=False,
+ is_web_public: bool=False,
stream_post_policy: int=Stream.STREAM_POST_POLICY_EVERYONE,
history_public_to_subscribers: Optional[bool]=None,
stream_description: str="",
@@ -74,6 +75,7 @@ def create_stream_if_needed(realm: Realm,
name=stream_name,
description=stream_description,
invite_only=invite_only,
+ is_web_public=is_web_public,
stream_post_policy=stream_post_policy,
history_public_to_subscribers=history_public_to_subscribers,
is_in_zephyr_realm=realm.is_zephyr_mirror_realm,
diff --git a/zerver/tests/test_digest.py b/zerver/tests/test_digest.py
index 5fff68ca0ee..982c08d3913 100644
--- a/zerver/tests/test_digest.py
+++ b/zerver/tests/test_digest.py
@@ -79,6 +79,49 @@ class TestDigestEmailMessages(ZulipTestCase):
self.assertIn('some content', teaser_messages[0]['content'][0]['plain'])
self.assertIn(teaser_messages[0]['sender'], expected_participants)
+ @mock.patch('zerver.lib.digest.enough_traffic')
+ @mock.patch('zerver.lib.digest.send_future_email')
+ def test_guest_user_multiple_stream_sender(self,
+ mock_send_future_email: mock.MagicMock,
+ mock_enough_traffic: mock.MagicMock) -> None:
+ othello = self.example_user('othello')
+ hamlet = self.example_user('hamlet')
+ cordelia = self.example_user('cordelia')
+ polonius = self.example_user('polonius')
+ create_stream_if_needed(cordelia.realm, 'web_public_stream', is_web_public=True)
+ self.subscribe(othello, 'web_public_stream')
+ self.subscribe(hamlet, 'web_public_stream')
+ self.subscribe(cordelia, 'web_public_stream')
+ self.subscribe(polonius, 'web_public_stream')
+
+ one_day_ago = timezone_now() - datetime.timedelta(days=1)
+ Message.objects.all().update(date_sent=one_day_ago)
+ one_hour_ago = timezone_now() - datetime.timedelta(seconds=3600)
+
+ cutoff = time.mktime(one_hour_ago.timetuple())
+
+ senders = ['hamlet', 'cordelia', 'othello', 'desdemona']
+ self.simulate_stream_conversation('web_public_stream', senders)
+
+ flush_per_request_caches()
+ # When this test is run in isolation, one additional query is run which
+ # is equivalent to
+ # ContentType.objects.get(app_label='zerver', model='userprofile')
+ # This code is run when we call `confirmation.models.create_confirmation_link`.
+ # To trigger this, we call the one_click_unsubscribe_link function below.
+ one_click_unsubscribe_link(polonius, 'digest')
+ with queries_captured() as queries:
+ handle_digest_email(polonius.id, cutoff)
+
+ self.assert_length(queries, 6)
+
+ self.assertEqual(mock_send_future_email.call_count, 1)
+ kwargs = mock_send_future_email.call_args[1]
+ self.assertEqual(kwargs['to_user_ids'], [polonius.id])
+
+ new_stream_names = kwargs['context']['new_streams']['plain']
+ self.assertTrue('web_public_stream' in new_stream_names)
+
@mock.patch('zerver.lib.digest.enough_traffic')
@mock.patch('zerver.lib.digest.send_future_email')
def test_soft_deactivated_user_multiple_stream_senders(self,