Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion comments/services/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def notify_mentioned_users(comment: Comment):
users.exclude(pk=comment.author_id)
# Exclude users with disabled notifications
.exclude(unsubscribed_mailing_tags__contains=[MailingTags.COMMENT_MENTIONS])
# Inactive users only receive account-related emails
.filter(is_active=True)
)

for user in users:
Expand Down Expand Up @@ -69,7 +71,8 @@ def notify_weekly_top_comments_subscribers(
other_usernames = ", ".join(other_usernames_list)

recipients_qs = (
User.objects.exclude(
User.objects.filter(is_active=True)
.exclude(
Q(unsubscribed_mailing_tags__contains=[MailingTags.WEEKLY_TOP_COMMENTS])
)
.exclude(email__isnull=True)
Expand Down
4 changes: 3 additions & 1 deletion notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

class NotificationQuerySet(models.QuerySet):
def filter_pending_email(self):
return self.filter(email_sent=False, read_at__isnull=True)
return self.filter(
email_sent=False, read_at__isnull=True, recipient__is_active=True
)


class Notification(TimeStampedModel):
Expand Down
22 changes: 20 additions & 2 deletions notifications/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ def schedule(
Schedules a notification to be sent using a cron job.
"""

# Inactive users only receive account-related emails, not notifications
if not recipient.is_active:
return

# Skip notification sending if it was ignored
if mailing_tag and mailing_tag in recipient.unsubscribed_mailing_tags:
return
Expand Down Expand Up @@ -722,6 +726,10 @@ def send_comment_mention_notification(recipient, comment: Comment, mention: str)
Send instant notification of mention in a comment
"""

# Inactive users only receive account-related emails
if not recipient.is_active:
return

mention_label = "you" if mention == recipient.username.lower() else mention
preview_text = generate_email_comment_preview_text(
comment.text, mention, max_chars=1024
Expand Down Expand Up @@ -757,7 +765,7 @@ def send_comment_report_notification_to_staff(
):
recipients = comment.on_post.default_project.get_users_for_permission(
ObjectPermission.CURATOR
)
).filter(is_active=True)

return send_email_with_template(
[x.email for x in recipients],
Expand Down Expand Up @@ -791,7 +799,9 @@ def send_key_factor_report_notification_to_staff(
comment = key_factor.comment
post = comment.on_post

recipients = post.default_project.get_users_for_permission(ObjectPermission.CURATOR)
recipients = post.default_project.get_users_for_permission(
ObjectPermission.CURATOR
).filter(is_active=True)

return send_email_with_template(
[x.email for x in recipients],
Expand Down Expand Up @@ -821,6 +831,10 @@ def send_forecast_autowidrawal_notification(
posts_data: list[dict],
account_settings_url: str,
):
# Inactive users only receive account-related emails
if not user.is_active:
return False

send_email_with_template(
to=user.email,
subject=_(
Expand Down Expand Up @@ -872,6 +886,10 @@ def send_news_category_notebook_publish_notification(user: User, post: Post):
`NotificationPostStatusChange` notification type.
"""

# Inactive users only receive account-related emails
if not user.is_active:
return

preview_text = generate_email_notebook_preview_text(
post.notebook.markdown, max_words=100
)
Expand Down
14 changes: 12 additions & 2 deletions questions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,14 @@ def check_and_schedule_forecast_widrawal_due_notifications():

forecast_already_withdrawn = Q(forecast__end_time__lt=now)

# Inactive users only receive account-related emails
user_is_inactive = Q(user__is_active=False)

all_notifications = UserForecastNotification.objects.filter(due_and_unsent).exclude(
user_is_unsubscribed | question_is_closed | forecast_already_withdrawn
user_is_unsubscribed
| user_is_inactive
| question_is_closed
| forecast_already_withdrawn
)

# Group notifications by user and post
Expand Down Expand Up @@ -311,6 +317,8 @@ def multiple_choice_delete_option_notifications(

forecasters = (
question.get_forecasters()
# Inactive users only receive account-related emails
.filter(is_active=True)
.exclude(
unsubscribed_mailing_tags__contains=[
MailingTags.BEFORE_PREDICTION_AUTO_WITHDRAWAL # seems most reasonable
Expand Down Expand Up @@ -407,7 +415,9 @@ def multiple_choice_add_option_notifications(
User.objects.filter(
forecast__in=question.user_forecasts.filter(
end_time=grace_period_end
) # all effected forecasts have their end_time set to grace_period_end
), # all effected forecasts have their end_time set to grace_period_end
# Inactive users only receive account-related emails
is_active=True,
)
.exclude(
unsubscribed_mailing_tags__contains=[
Expand Down
Loading