From ba640bf89d7f08ed29e894294e8a10c848dea18c Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Wed, 14 Nov 2018 17:28:35 +0530 Subject: [PATCH] emails: Don't send day2 email if user already has an account. --- zerver/lib/notifications.py | 10 +++++++--- zerver/tests/test_notifications.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index 9a16a3793d..7baf19e025 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -504,6 +504,8 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool=False) -> Non from_name = None from_address = FromAddress.SUPPORT + other_account_count = UserProfile.objects.filter( + email__iexact=user.email).exclude(id=user.id).count() unsubscribe_link = one_click_unsubscribe_link(user, "welcome") context = common_context(user) context.update({ @@ -527,9 +529,11 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool=False) -> Non send_future_email( "zerver/emails/followup_day1", user.realm, to_user_id=user.id, from_name=from_name, from_address=from_address, context=context) - send_future_email( - "zerver/emails/followup_day2", user.realm, to_user_id=user.id, from_name=from_name, - from_address=from_address, context=context, delay=followup_day2_email_delay(user)) + + if other_account_count == 0: + send_future_email( + "zerver/emails/followup_day2", user.realm, to_user_id=user.id, from_name=from_name, + from_address=from_address, context=context, delay=followup_day2_email_delay(user)) def convert_html_to_markdown(html: str) -> str: # On Linux, the tool installs as html2markdown, and there's a command called diff --git a/zerver/tests/test_notifications.py b/zerver/tests/test_notifications.py index d34333b6d7..aeb05a1559 100644 --- a/zerver/tests/test_notifications.py +++ b/zerver/tests/test_notifications.py @@ -87,6 +87,26 @@ class TestFollowupEmails(ZulipTestCase): email_data = ujson.loads(scheduled_emails[0].data) self.assertEqual(email_data["context"]["ldap_username"], True) + def test_followup_emails_count(self) -> None: + hamlet = self.example_user("hamlet") + cordelia = self.example_user("cordelia") + + enqueue_welcome_emails(self.example_user("hamlet")) + # Hamlet has account only in Zulip realm so both day1 and day2 emails should be sent + scheduled_emails = ScheduledEmail.objects.filter(user=hamlet) + self.assertEqual(2, len(scheduled_emails)) + self.assertEqual(ujson.loads(scheduled_emails[0].data)["template_prefix"], 'zerver/emails/followup_day2') + self.assertEqual(ujson.loads(scheduled_emails[1].data)["template_prefix"], 'zerver/emails/followup_day1') + + ScheduledEmail.objects.all().delete() + + enqueue_welcome_emails(cordelia) + scheduled_emails = ScheduledEmail.objects.filter(user=cordelia) + # Cordelia has account in more than 1 realm so day2 email should not be sent + self.assertEqual(len(scheduled_emails), 1) + email_data = ujson.loads(scheduled_emails[0].data) + self.assertEqual(email_data["template_prefix"], 'zerver/emails/followup_day1') + class TestMissedMessages(ZulipTestCase): def normalize_string(self, s: str) -> str: s = s.strip()