diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 907a96cc85..a194fad124 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -47,7 +47,8 @@ from zerver.lib.message import ( from zerver.lib.realm_icon import realm_icon_url from zerver.lib.realm_logo import realm_logo_url from zerver.lib.retention import move_messages_to_archive -from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins +from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins, \ + clear_scheduled_emails, clear_scheduled_invitation_emails from zerver.lib.stream_subscription import ( get_active_subscriptions_for_stream_id, get_active_subscriptions_for_stream_ids, @@ -137,8 +138,7 @@ from zerver.lib.utils import log_statsd_event, statsd from zerver.lib.i18n import get_language_name from zerver.lib.alert_words import add_user_alert_words, \ remove_user_alert_words, set_user_alert_words -from zerver.lib.notifications import clear_scheduled_emails, \ - clear_scheduled_invitation_emails, enqueue_welcome_emails +from zerver.lib.notifications import enqueue_welcome_emails from zerver.lib.exceptions import JsonableError, ErrorCode, BugdownRenderingException from zerver.lib.sessions import delete_user_sessions from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id, \ diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index 2d2e7cadd1..14638c1a70 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -1,5 +1,5 @@ -from typing import Any, Dict, Iterable, List, Optional, Tuple +from typing import Any, Dict, Iterable, List, Tuple from confirmation.models import one_click_unsubscribe_link from django.conf import settings @@ -15,7 +15,6 @@ from zerver.lib.url_encoding import personal_narrow_url, huddle_narrow_url, \ stream_narrow_url, topic_narrow_url from zerver.models import ( Recipient, - ScheduledEmail, UserMessage, Stream, get_display_recipient, @@ -457,22 +456,6 @@ def handle_missedmessage_emails(user_profile_id: int, message_count_by_bucket[bucket_tup], ) -def clear_scheduled_invitation_emails(email: str) -> None: - """Unlike most scheduled emails, invitation emails don't have an - existing user object to key off of, so we filter by address here.""" - items = ScheduledEmail.objects.filter(address__iexact=email, - type=ScheduledEmail.INVITATION_REMINDER) - items.delete() - -def clear_scheduled_emails(user_ids: List[int], email_type: Optional[int]=None) -> None: - items = ScheduledEmail.objects.filter(users__in=user_ids).distinct() - if email_type is not None: - items = items.filter(type=email_type) - for item in items: - item.users.remove(*user_ids) - if item.users.all().count() == 0: - item.delete() - def log_digest_event(msg: str) -> None: import logging import time diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py index 841e7d77f2..fe3fced5a7 100644 --- a/zerver/lib/send_email.py +++ b/zerver/lib/send_email.py @@ -166,6 +166,22 @@ def send_email_to_admins(template_prefix: str, realm: Realm, from_name: Optional send_email(template_prefix, to_user_ids=admin_user_ids, from_name=from_name, from_address=from_address, context=context) +def clear_scheduled_invitation_emails(email: str) -> None: + """Unlike most scheduled emails, invitation emails don't have an + existing user object to key off of, so we filter by address here.""" + items = ScheduledEmail.objects.filter(address__iexact=email, + type=ScheduledEmail.INVITATION_REMINDER) + items.delete() + +def clear_scheduled_emails(user_ids: List[int], email_type: Optional[int]=None) -> None: + items = ScheduledEmail.objects.filter(users__in=user_ids).distinct() + if email_type is not None: + items = items.filter(type=email_type) + for item in items: + item.users.remove(*user_ids) + if item.users.all().count() == 0: + item.delete() + def handle_send_email_format_changes(job: Dict[str, Any]) -> None: # Reformat any jobs that used the old to_email # and to_user_ids argument formats. diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index e7364a4765..184f1c43f6 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -21,8 +21,7 @@ from zerver.models import UserProfile, Recipient, \ from zerver.lib.avatar import avatar_url from zerver.lib.exceptions import JsonableError -from zerver.lib.send_email import send_future_email -from zerver.lib.notifications import clear_scheduled_emails +from zerver.lib.send_email import send_future_email, clear_scheduled_emails from zerver.lib.actions import ( get_emails_from_user_ids, get_recipient_info, diff --git a/zerver/views/unsubscribe.py b/zerver/views/unsubscribe.py index f55ef86976..bc413a3ed3 100644 --- a/zerver/views/unsubscribe.py +++ b/zerver/views/unsubscribe.py @@ -4,7 +4,8 @@ from typing import Callable from confirmation.models import Confirmation, get_object_from_key, \ ConfirmationKeyException -from zerver.lib.actions import do_change_notification_settings, clear_scheduled_emails +from zerver.lib.actions import do_change_notification_settings +from zerver.lib.send_email import clear_scheduled_emails from zerver.models import UserProfile, ScheduledEmail from zerver.context_processors import common_context