Skip to content

Commit dd417f1

Browse files
authored
feat(notifications-v2): Read from notification settings for weekly reports (#56621)
Read from the new notifications tables for weekly reports instead of user options.
1 parent 716e78c commit dd417f1

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/sentry/notifications/notificationcontroller.py

+24
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,27 @@ def get_notification_provider_value_for_recipient_and_type(
553553
return value
554554

555555
return NotificationSettingsOptionEnum.NEVER
556+
557+
def get_users_for_weekly_reports(self) -> list[int]:
558+
if not self.organization_id:
559+
raise Exception("Must specify organization_id")
560+
561+
if self.type != NotificationSettingEnum.REPORTS:
562+
raise Exception(f"Type mismatch: the controller was initialized with type: {self.type}")
563+
564+
enabled_settings = self.get_all_enabled_settings(type=NotificationSettingEnum.REPORTS.value)
565+
users = []
566+
for recipient, setting in enabled_settings.items():
567+
if not recipient_is_user(recipient):
568+
continue
569+
570+
for type_map in setting.values():
571+
provider_map = type_map[NotificationSettingEnum.REPORTS]
572+
if (
573+
ExternalProviderEnum.EMAIL in provider_map
574+
and provider_map[ExternalProviderEnum.EMAIL]
575+
== NotificationSettingsOptionEnum.ALWAYS
576+
):
577+
users.append(recipient.id)
578+
579+
return users

src/sentry/tasks/weekly_reports.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
OrganizationMember,
3434
OrganizationStatus,
3535
)
36+
from sentry.notifications.helpers import should_use_notifications_v2
37+
from sentry.notifications.notificationcontroller import NotificationController
38+
from sentry.notifications.types import NotificationSettingEnum
39+
from sentry.services.hybrid_cloud.user.model import RpcUser
3640
from sentry.services.hybrid_cloud.user_option import user_option_service
3741
from sentry.silo import SiloMode
3842
from sentry.snuba.dataset import Dataset
@@ -225,10 +229,16 @@ def prepare_organization_report(
225229
)
226230
return
227231

232+
use_notifications_v2 = should_use_notifications_v2(ctx.organization)
233+
228234
# Finally, deliver the reports
229235
with sentry_sdk.start_span(op="weekly_reports.deliver_reports"):
230236
deliver_reports(
231-
ctx, dry_run=dry_run, target_user=target_user, email_override=email_override
237+
ctx,
238+
dry_run=dry_run,
239+
target_user=target_user,
240+
email_override=email_override,
241+
use_notifications_v2=use_notifications_v2,
232242
)
233243

234244

@@ -637,10 +647,33 @@ def fetch_key_performance_issue_groups(ctx):
637647
# For all users in the organization, we generate the template context for the user, and send the email.
638648

639649

640-
def deliver_reports(ctx, dry_run=False, target_user=None, email_override=None):
650+
def deliver_reports(
651+
ctx, dry_run=False, target_user=None, email_override=None, use_notifications_v2=False
652+
):
641653
# Specify a sentry user to send this email.
642654
if email_override:
643655
send_email(ctx, target_user, dry_run=dry_run, email_override=email_override)
656+
elif use_notifications_v2:
657+
user_list = list(
658+
OrganizationMember.objects.filter(
659+
user_is_active=True,
660+
organization_id=ctx.organization.id,
661+
)
662+
.filter(flags=F("flags").bitand(~OrganizationMember.flags["member-limit:restricted"]))
663+
.values_list("user_id", flat=True)
664+
)
665+
666+
users = [RpcUser(id=user_id) for user_id in user_list]
667+
controller = NotificationController(
668+
recipients=users,
669+
organization_id=ctx.organization.id,
670+
type=NotificationSettingEnum.REPORTS,
671+
)
672+
673+
user_ids = controller.get_users_for_weekly_reports()
674+
for user_id in user_ids:
675+
send_email(ctx, user_id, dry_run=dry_run)
676+
644677
else:
645678
# We save the subscription status of the user in a field in UserOptions.
646679
user_list = list(

tests/sentry/notifications/test_notificationcontroller.py

+23
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,26 @@ def test_get_notification_provider_value_for_recipient_and_type_with_layering(se
752752
)
753753
== NotificationSettingsOptionEnum.NEVER
754754
)
755+
756+
def test_get_users_for_weekly_reports(self):
757+
controller = NotificationController(
758+
recipients=[self.user],
759+
organization_id=self.organization.id,
760+
type=NotificationSettingEnum.REPORTS,
761+
)
762+
assert controller.get_users_for_weekly_reports() == [self.user.id]
763+
764+
add_notification_setting_option(
765+
scope_type=NotificationScopeEnum.USER,
766+
scope_identifier=self.user.id,
767+
type=NotificationSettingEnum.REPORTS,
768+
value=NotificationSettingsOptionEnum.NEVER,
769+
user_id=self.user.id,
770+
)
771+
772+
controller = NotificationController(
773+
recipients=[self.user],
774+
organization_id=self.organization.id,
775+
type=NotificationSettingEnum.REPORTS,
776+
)
777+
assert controller.get_users_for_weekly_reports() == []

0 commit comments

Comments
 (0)