notifications: Separate push and email notifications checks.

This is an early step in a larger refactor to properly decouple the
email and push notification code paths.
This commit is contained in:
Tim Abbott
2017-11-28 17:49:11 -08:00
parent ba70b542d3
commit 6bc1a682de
5 changed files with 31 additions and 18 deletions

View File

@@ -63,7 +63,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity,
email_allowed_for_realm, email_to_username, display_recipient_cache_key, \
get_user, get_stream_cache_key, \
UserActivityInterval, active_user_ids, get_active_streams, \
realm_filters_for_realm, RealmFilter, receives_offline_notifications, \
realm_filters_for_realm, RealmFilter, \
get_owned_bot_dicts, stream_name_in_use, \
get_old_unclaimed_attachments, get_cross_realm_emails, \
Reaction, EmailChangeStatus, CustomProfileField, \

View File

@@ -17,7 +17,7 @@ from zerver.models import (
UserProfile,
get_user,
get_user_profile_by_id,
receives_offline_notifications,
receives_offline_email_notifications,
get_context_for_message,
Message,
Realm,
@@ -371,11 +371,12 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile: UserProfile,
user_profile.last_reminder = timezone_now()
user_profile.save(update_fields=['last_reminder'])
def handle_missedmessage_emails(user_profile_id: int, missed_email_events: Iterable[Dict[str, Any]]) -> None:
def handle_missedmessage_emails(user_profile_id: int,
missed_email_events: Iterable[Dict[str, Any]]) -> None:
message_ids = [event.get('message_id') for event in missed_email_events]
user_profile = get_user_profile_by_id(user_profile_id)
if not receives_offline_notifications(user_profile):
if not receives_offline_email_notifications(user_profile):
return
messages = Message.objects.filter(usermessage__user_profile_id=user_profile,

View File

@@ -31,7 +31,7 @@ from zerver.lib.queue import retry_event
from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.lib.utils import generate_random_token
from zerver.models import PushDeviceToken, Message, Recipient, UserProfile, \
UserMessage, get_display_recipient, receives_offline_notifications, \
UserMessage, get_display_recipient, receives_offline_push_notifications, \
receives_online_notifications, receives_stream_notifications, get_user_profile_by_id
from version import ZULIP_VERSION
@@ -486,7 +486,7 @@ def handle_push_notification(user_profile_id: int, missed_message: Dict[str, Any
zerver.worker.queue_processors.PushNotificationWorker.consume function.
"""
user_profile = get_user_profile_by_id(user_profile_id)
if not (receives_offline_notifications(user_profile) or
if not (receives_offline_push_notifications(user_profile) or
receives_online_notifications(user_profile)):
return

View File

@@ -747,9 +747,12 @@ class UserGroupMembership(models.Model):
class Meta:
unique_together = (('user_group', 'user_profile'),)
def receives_offline_notifications(user_profile: UserProfile) -> bool:
return ((user_profile.enable_offline_email_notifications or
user_profile.enable_offline_push_notifications) and
def receives_offline_push_notifications(user_profile: UserProfile) -> bool:
return (user_profile.enable_offline_push_notifications and
not user_profile.is_bot)
def receives_offline_email_notifications(user_profile: UserProfile) -> bool:
return (user_profile.enable_offline_email_notifications and
not user_profile.is_bot)
def receives_online_notifications(user_profile: UserProfile) -> bool:

View File

@@ -19,7 +19,8 @@ from zerver.models import (
UserProfile,
Message,
UserMessage,
receives_offline_notifications,
receives_offline_email_notifications,
receives_offline_push_notifications,
receives_online_notifications,
receives_stream_notifications,
get_client,
@@ -1107,38 +1108,46 @@ class TestReceivesNotificationsFunctions(ZulipTestCase):
self.user.enable_offline_email_notifications = True
self.user.enable_offline_push_notifications = True
self.assertFalse(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = False
self.user.enable_offline_push_notifications = False
self.assertFalse(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = True
self.user.enable_offline_push_notifications = False
self.assertFalse(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = False
self.user.enable_offline_push_notifications = True
self.assertFalse(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
def test_receivers_offline_notifications_when_user_is_not_a_bot(self) -> None:
self.user.is_bot = False
self.user.enable_offline_email_notifications = True
self.user.enable_offline_push_notifications = True
self.assertTrue(receives_offline_notifications(self.user))
self.assertTrue(receives_offline_push_notifications(self.user))
self.assertTrue(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = False
self.user.enable_offline_push_notifications = False
self.assertFalse(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = True
self.user.enable_offline_push_notifications = False
self.assertTrue(receives_offline_notifications(self.user))
self.assertFalse(receives_offline_push_notifications(self.user))
self.assertTrue(receives_offline_email_notifications(self.user))
self.user.enable_offline_email_notifications = False
self.user.enable_offline_push_notifications = True
self.assertTrue(receives_offline_notifications(self.user))
self.assertTrue(receives_offline_push_notifications(self.user))
self.assertFalse(receives_offline_email_notifications(self.user))
def test_receivers_stream_notifications_when_user_is_a_bot(self) -> None:
self.user.is_bot = True