Skip to content

Commit

Permalink
digest: Support digest of web public streams for guest users.
Browse files Browse the repository at this point in the history
  • Loading branch information
clarammdantas authored and timabbott committed Jul 30, 2020
1 parent a9af80d commit c377933
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zerver/lib/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,14 @@ 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 = []

Expand Down
43 changes: 43 additions & 0 deletions zerver/tests/test_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ def test_multiple_stream_senders(self,
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,
Expand Down

0 comments on commit c377933

Please sign in to comment.