Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
emails: Add unsubscribe link infrastructure for marketing emails.
Tim Abbott50f00d3e9745
diff --git a/static/js/settings.js b/static/js/settings.jsindex 35b1f8d46a6..2e12980a47d 100644--- a/static/js/settings.js+++ b/static/js/settings.js@@ -55,6 +55,9 @@ function setup_settings_label() {enable_login_emails: $t({defaultMessage: "Send email notifications for new logins to my account",}),+ enable_marketing_emails: $t({+ defaultMessage: "Send me occasional marketing emails about Zulip (a few times a year)",+ }),message_content_in_email_notifications: $t({defaultMessage: "Include message content in message notification emails",}),diff --git a/static/js/settings_config.js b/static/js/settings_config.jsindex 12abbec3a8d..937e73d3e77 100644--- a/static/js/settings_config.js+++ b/static/js/settings_config.js@@ -346,6 +346,7 @@ const email_notification_settings = ["enable_login_emails","message_content_in_email_notifications","realm_name_in_notifications",+ "enable_marketing_emails",];const presence_notification_settings = ["presence_enabled"];diff --git a/static/js/settings_notifications.js b/static/js/settings_notifications.jsindex be66afa3387..59894742626 100644--- a/static/js/settings_notifications.js+++ b/static/js/settings_notifications.js@@ -66,6 +66,14 @@ export function set_enable_digest_emails_visibility() {}}+export function set_enable_marketing_emails_visibility() {+ if (page_params.enable_marketing_emails_enabled) {+ $("#enable_marketing_emails_label").parent().show();+ } else {+ $("#enable_marketing_emails_label").parent().hide();+ }+}+export function set_up() {$("#notification-settings").on("change", "input, select", function (e) {e.preventDefault();@@ -113,6 +121,7 @@ export function set_up() {}});set_enable_digest_emails_visibility();+ set_enable_marketing_emails_visibility();rerender_ui();}diff --git a/zerver/lib/home.py b/zerver/lib/home.pyindex 8ae00b54fab..7afff294167 100644--- a/zerver/lib/home.py+++ b/zerver/lib/home.py@@ -220,6 +220,8 @@ def build_page_params_for_home_page_load(save_stacktraces=settings.SAVE_FRONTEND_STACKTRACES,warn_no_email=settings.WARN_NO_EMAIL,search_pills_enabled=settings.SEARCH_PILLS_ENABLED,+ # Only show marketing email settings if on Zulip Cloud+ enable_marketing_emails_enabled=settings.CORPORATE_ENABLED,# Misc. extra data.initial_servertime=time.time(), # Used for calculating relative presence agedefault_language_name=get_language_name(register_ret["default_language"]),diff --git a/zerver/migrations/0321_userprofile_enable_marketing_emails.py b/zerver/migrations/0321_userprofile_enable_marketing_emails.pynew file mode 100644index 00000000000..9bd6a1fa057--- /dev/null+++ b/zerver/migrations/0321_userprofile_enable_marketing_emails.py@@ -0,0 +1,18 @@+# Generated by Django 3.1.8 on 2021-04-27 22:00++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("zerver", "0320_realm_move_messages_between_streams_policy"),+ ]++ operations = [+ migrations.AddField(+ model_name="userprofile",+ name="enable_marketing_emails",+ field=models.BooleanField(default=True),+ ),+ ]diff --git a/zerver/models.py b/zerver/models.pyindex fe0d6822b76..f8cc2aeb6f4 100644--- a/zerver/models.py+++ b/zerver/models.py@@ -1238,6 +1238,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):enable_digest_emails: bool = models.BooleanField(default=True)enable_login_emails: bool = models.BooleanField(default=True)+ enable_marketing_emails: bool = models.BooleanField(default=True)realm_name_in_notifications: bool = models.BooleanField(default=False)presence_enabled: bool = models.BooleanField(default=True)@@ -1386,6 +1387,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):enable_desktop_notifications=bool,enable_digest_emails=bool,enable_login_emails=bool,+ enable_marketing_emails=bool,enable_offline_email_notifications=bool,enable_offline_push_notifications=bool,enable_online_push_notifications=bool,diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yamlindex 6db2274fc1e..4dbb97f468c 100644--- a/zerver/openapi/zulip.yaml+++ b/zerver/openapi/zulip.yaml@@ -7697,6 +7697,14 @@ paths:The current value of this global notification setting for the user.See [update-notification-settings](/api/update-notification-settings)for details on notification settings.+ enable_marketing_emails:+ type: boolean+ description: |+ Present if `update_global_notifications` is present in `fetch_event_types`.++ The current value of this global notification setting for the user.+ See [update-notification-settings](/api/update-notification-settings)+ for details on notification settings.enable_offline_email_notifications:type: booleandescription: |@@ -9187,6 +9195,13 @@ paths:schema:type: booleanexample: true+ - name: enable_marketing_emails+ in: query+ description: |+ Enable marketing emails. Has no function outside Zulip Cloud.+ schema:+ type: boolean+ example: true- name: enable_login_emailsin: querydescription: |@@ -9269,6 +9284,11 @@ paths:description: |The setting for `enable_digest_emails`, if it was changed in thisrequest.+ enable_marketing_emails:+ type: boolean+ description: |+ The setting for `enable_marketing_emails`, if it was changed in this+ request.enable_offline_email_notifications:type: booleandescription: |diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 06738062090..2901cfb3dac 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -72,6 +72,8 @@ class HomeTest(ZulipTestCase):"enable_desktop_notifications","enable_digest_emails","enable_login_emails",+ "enable_marketing_emails",+ "enable_marketing_emails_enabled","enable_offline_email_notifications","enable_offline_push_notifications","enable_online_push_notifications",diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 3d313bea7cb..370db16bb94 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -2659,6 +2659,26 @@ class EmailUnsubscribeTests(ZulipTestCase):user_profile.refresh_from_db()self.assertFalse(user_profile.enable_login_emails)+ def test_marketing_unsubscribe(self) -> None:+ """+ We provide one-click unsubscribe links in marketing e-mails that you can+ click even when logged out to stop receiving them.+ """+ user_profile = self.example_user("hamlet")+ self.assertTrue(user_profile.enable_marketing_emails)++ # Simulate unsubscribing from digest e-mails.+ unsubscribe_link = one_click_unsubscribe_link(user_profile, "marketing")+ result = self.client_get(urllib.parse.urlparse(unsubscribe_link).path)++ # The setting is toggled off, and scheduled jobs have been removed.+ self.assertEqual(result.status_code, 200)++ # Circumvent user_profile caching.+ user_profile.refresh_from_db()+ self.assertFalse(user_profile.enable_marketing_emails)+ self.assertEqual(0, ScheduledEmail.objects.filter(users=user_profile).count())+class RealmCreationTest(ZulipTestCase):@override_settings(OPEN_REALM_CREATION=True)diff --git a/zerver/views/unsubscribe.py b/zerver/views/unsubscribe.pyindex 75e36a837f5..6814d57bd4c 100644--- a/zerver/views/unsubscribe.py+++ b/zerver/views/unsubscribe.py@@ -53,6 +53,12 @@ def do_login_unsubscribe(user_profile: UserProfile) -> None:)+def do_marketing_unsubscribe(user_profile: UserProfile) -> None:+ do_change_notification_settings(+ user_profile, "enable_marketing_emails", False, acting_user=user_profile+ )++# The keys are part of the URL for the unsubscribe link and must be valid# without encoding.# The values are a tuple of (display name, unsubscribe function), where the@@ -62,6 +68,7 @@ email_unsubscribers = {"welcome": ("welcome", do_welcome_unsubscribe),"digest": ("digest", do_digest_unsubscribe),"login": ("login", do_login_unsubscribe),+ "marketing": ("marketing", do_marketing_unsubscribe),}# Login NOT required. These are for one-click unsubscribes.diff --git a/zerver/views/user_settings.py b/zerver/views/user_settings.pyindex c52b610acbc..7ae93420b5c 100644--- a/zerver/views/user_settings.py+++ b/zerver/views/user_settings.py@@ -255,6 +255,7 @@ def json_change_notify_settings(enable_online_push_notifications: Optional[bool] = REQ(json_validator=check_bool, default=None),enable_digest_emails: Optional[bool] = REQ(json_validator=check_bool, default=None),enable_login_emails: Optional[bool] = REQ(json_validator=check_bool, default=None),+ enable_marketing_emails: Optional[bool] = REQ(json_validator=check_bool, default=None),message_content_in_email_notifications: Optional[bool] = REQ(json_validator=check_bool, default=None),