mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	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:
		@@ -63,7 +63,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity,
 | 
				
			|||||||
    email_allowed_for_realm, email_to_username, display_recipient_cache_key, \
 | 
					    email_allowed_for_realm, email_to_username, display_recipient_cache_key, \
 | 
				
			||||||
    get_user, get_stream_cache_key, \
 | 
					    get_user, get_stream_cache_key, \
 | 
				
			||||||
    UserActivityInterval, active_user_ids, get_active_streams, \
 | 
					    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_owned_bot_dicts, stream_name_in_use, \
 | 
				
			||||||
    get_old_unclaimed_attachments, get_cross_realm_emails, \
 | 
					    get_old_unclaimed_attachments, get_cross_realm_emails, \
 | 
				
			||||||
    Reaction, EmailChangeStatus, CustomProfileField, \
 | 
					    Reaction, EmailChangeStatus, CustomProfileField, \
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ from zerver.models import (
 | 
				
			|||||||
    UserProfile,
 | 
					    UserProfile,
 | 
				
			||||||
    get_user,
 | 
					    get_user,
 | 
				
			||||||
    get_user_profile_by_id,
 | 
					    get_user_profile_by_id,
 | 
				
			||||||
    receives_offline_notifications,
 | 
					    receives_offline_email_notifications,
 | 
				
			||||||
    get_context_for_message,
 | 
					    get_context_for_message,
 | 
				
			||||||
    Message,
 | 
					    Message,
 | 
				
			||||||
    Realm,
 | 
					    Realm,
 | 
				
			||||||
@@ -371,11 +371,12 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile: UserProfile,
 | 
				
			|||||||
    user_profile.last_reminder = timezone_now()
 | 
					    user_profile.last_reminder = timezone_now()
 | 
				
			||||||
    user_profile.save(update_fields=['last_reminder'])
 | 
					    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]
 | 
					    message_ids = [event.get('message_id') for event in missed_email_events]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    user_profile = get_user_profile_by_id(user_profile_id)
 | 
					    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
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    messages = Message.objects.filter(usermessage__user_profile_id=user_profile,
 | 
					    messages = Message.objects.filter(usermessage__user_profile_id=user_profile,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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.timestamp import datetime_to_timestamp, timestamp_to_datetime
 | 
				
			||||||
from zerver.lib.utils import generate_random_token
 | 
					from zerver.lib.utils import generate_random_token
 | 
				
			||||||
from zerver.models import PushDeviceToken, Message, Recipient, UserProfile, \
 | 
					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
 | 
					    receives_online_notifications, receives_stream_notifications, get_user_profile_by_id
 | 
				
			||||||
from version import ZULIP_VERSION
 | 
					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.
 | 
					    zerver.worker.queue_processors.PushNotificationWorker.consume function.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    user_profile = get_user_profile_by_id(user_profile_id)
 | 
					    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)):
 | 
					            receives_online_notifications(user_profile)):
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -747,9 +747,12 @@ class UserGroupMembership(models.Model):
 | 
				
			|||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        unique_together = (('user_group', 'user_profile'),)
 | 
					        unique_together = (('user_group', 'user_profile'),)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def receives_offline_notifications(user_profile: UserProfile) -> bool:
 | 
					def receives_offline_push_notifications(user_profile: UserProfile) -> bool:
 | 
				
			||||||
    return ((user_profile.enable_offline_email_notifications or
 | 
					    return (user_profile.enable_offline_push_notifications and
 | 
				
			||||||
             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)
 | 
					            not user_profile.is_bot)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def receives_online_notifications(user_profile: UserProfile) -> bool:
 | 
					def receives_online_notifications(user_profile: UserProfile) -> bool:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,7 +19,8 @@ from zerver.models import (
 | 
				
			|||||||
    UserProfile,
 | 
					    UserProfile,
 | 
				
			||||||
    Message,
 | 
					    Message,
 | 
				
			||||||
    UserMessage,
 | 
					    UserMessage,
 | 
				
			||||||
    receives_offline_notifications,
 | 
					    receives_offline_email_notifications,
 | 
				
			||||||
 | 
					    receives_offline_push_notifications,
 | 
				
			||||||
    receives_online_notifications,
 | 
					    receives_online_notifications,
 | 
				
			||||||
    receives_stream_notifications,
 | 
					    receives_stream_notifications,
 | 
				
			||||||
    get_client,
 | 
					    get_client,
 | 
				
			||||||
@@ -1107,38 +1108,46 @@ class TestReceivesNotificationsFunctions(ZulipTestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.user.enable_offline_email_notifications = True
 | 
					        self.user.enable_offline_email_notifications = True
 | 
				
			||||||
        self.user.enable_offline_push_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_email_notifications = False
 | 
				
			||||||
        self.user.enable_offline_push_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_email_notifications = True
 | 
				
			||||||
        self.user.enable_offline_push_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 = False
 | 
					        self.user.enable_offline_email_notifications = False
 | 
				
			||||||
        self.user.enable_offline_push_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))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_receivers_offline_notifications_when_user_is_not_a_bot(self) -> None:
 | 
					    def test_receivers_offline_notifications_when_user_is_not_a_bot(self) -> None:
 | 
				
			||||||
        self.user.is_bot = False
 | 
					        self.user.is_bot = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.user.enable_offline_email_notifications = True
 | 
					        self.user.enable_offline_email_notifications = True
 | 
				
			||||||
        self.user.enable_offline_push_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_email_notifications = False
 | 
				
			||||||
        self.user.enable_offline_push_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_email_notifications = True
 | 
				
			||||||
        self.user.enable_offline_push_notifications = False
 | 
					        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_email_notifications = False
 | 
				
			||||||
        self.user.enable_offline_push_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.assertFalse(receives_offline_email_notifications(self.user))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_receivers_stream_notifications_when_user_is_a_bot(self) -> None:
 | 
					    def test_receivers_stream_notifications_when_user_is_a_bot(self) -> None:
 | 
				
			||||||
        self.user.is_bot = True
 | 
					        self.user.is_bot = True
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user