Skip to content

Commit

Permalink
user settings: Add setting to make login notifications optional.
Browse files Browse the repository at this point in the history
This adds a feature under "Notification" section of "Settings" tab,
which lets user enable or disable login emails notification.

Tweaked by tabbott to simplify the test.

Fixes: zulip#5854.
  • Loading branch information
pragatiagrawal31 committed Aug 25, 2018
1 parent ca272ef commit ace5062
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function setup_settings_label() {

// other_notification_settings
enable_digest_emails: i18n.t("Send digest emails when I'm away"),
enable_login_emails: i18n.t("Send email notifications for new logins to my account"),
message_content_in_email_notifications: i18n.t("Include message content in missed message emails"),
realm_name_in_notifications: i18n.t("Include organization name in subject of missed message emails"),

Expand Down
1 change: 1 addition & 0 deletions static/js/settings_notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var pm_mention_notification_settings = [

var other_notification_settings = [
"enable_digest_emails",
"enable_login_emails",
"realm_name_in_notifications",
"message_content_in_email_notifications",
];
Expand Down
5 changes: 5 additions & 0 deletions static/templates/settings/notification-settings.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
"is_checked" page_params.enable_digest_emails
"label" settings_label.enable_digest_emails}}

{{partial "settings_checkbox"
"setting_name" "enable_login_emails"
"is_checked" page_params.enable_login_emails
"label" settings_label.enable_login_emails}}

<div class="input-group {{#unless page_params.enable_offline_email_notifications}}control-label-disabled{{/unless}}">
<label class="checkbox">
<input type="checkbox" name="message_content_in_email_notifications"
Expand Down
20 changes: 20 additions & 0 deletions zerver/migrations/0187_userprofile_enable_login_emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-08-22 09:57
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('zerver', '0186_userprofile_starred_message_counts'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='enable_login_emails',
field=models.BooleanField(default=True),
),
]
2 changes: 2 additions & 0 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
enable_online_push_notifications = models.BooleanField(default=False) # type: bool

enable_digest_emails = models.BooleanField(default=True) # type: bool
enable_login_emails = models.BooleanField(default=True) # type: bool
realm_name_in_notifications = models.BooleanField(default=False) # type: bool

# Words that trigger a mention for this user, formatted as a json-serialized list of strings
Expand Down Expand Up @@ -829,6 +830,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
notification_setting_types = dict(
enable_desktop_notifications=bool,
enable_digest_emails=bool,
enable_login_emails=bool,
enable_offline_email_notifications=bool,
enable_offline_push_notifications=bool,
enable_online_push_notifications=bool,
Expand Down
2 changes: 2 additions & 0 deletions zerver/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def get_device_os(user_agent: str) -> Optional[str]:

@receiver(user_logged_in, dispatch_uid="only_on_login")
def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: Any) -> None:
if not user.enable_login_emails:
return
# We import here to minimize the dependencies of this module,
# since it runs as part of `manage.py` initialization
from zerver.context_processors import common_context
Expand Down
1 change: 1 addition & 0 deletions zerver/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_home(self) -> None:
"emojiset_choices",
"enable_desktop_notifications",
"enable_digest_emails",
"enable_login_emails",
"enable_offline_email_notifications",
"enable_offline_push_notifications",
"enable_online_push_notifications",
Expand Down
14 changes: 13 additions & 1 deletion zerver/tests/test_new_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from django.conf import settings
from django.core import mail
from django.contrib.auth.signals import user_logged_in
from django.test import override_settings

from zerver.lib.test_classes import ZulipTestCase
from zerver.signals import get_device_browser, get_device_os, JUST_CREATED_THRESHOLD
from zerver.lib.actions import notify_new_user
from zerver.lib.actions import notify_new_user, do_change_notification_settings
from zerver.models import Recipient, Stream, Realm
from zerver.lib.initial_password import initial_password
from unittest import mock
Expand Down Expand Up @@ -84,6 +86,16 @@ def test_without_path_info_dont_send_login_emails_for_new_user_registration_logi
subject = 'New login from an unknown browser on an unknown operating system'
self.assertNotEqual(email.subject, subject)

@override_settings(SEND_LOGIN_EMAILS=True)
def test_enable_login_emails_user_setting(self) -> None:
user = self.example_user('hamlet')
do_change_notification_settings(user, "enable_login_emails", False)

self.assertFalse(user.enable_login_emails)
self.login(user.email)
self.assertEqual(len(mail.outbox), 0)


class TestBrowserAndOsUserAgentStrings(ZulipTestCase):

def setUp(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions zerver/views/user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def json_change_notify_settings(
enable_offline_push_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
enable_online_push_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
enable_digest_emails: Optional[bool]=REQ(validator=check_bool, default=None),
enable_login_emails: Optional[bool]=REQ(validator=check_bool, default=None),
message_content_in_email_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
pm_content_in_desktop_notifications: Optional[bool]=REQ(validator=check_bool, default=None),
realm_name_in_notifications: Optional[bool]=REQ(validator=check_bool, default=None)
Expand Down

0 comments on commit ace5062

Please sign in to comment.