Skip to content

Commit

Permalink
fix(hybrid-cloud): Fix notification helpers for split db and split si…
Browse files Browse the repository at this point in the history
…lo world (#55829)
  • Loading branch information
dashed authored Sep 7, 2023
1 parent 05be00d commit 731bdbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
6 changes: 3 additions & 3 deletions fixtures/backup/model_dependencies/sorted.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
"sentry.DeletedOrganization",
"sentry.Email",
"sentry.Organization",
"sentry.User",
"sentry.IdentityProvider",
"sentry.Identity",
"sentry.DocIntegration",
"sentry.Integration",
"sentry.IntegrationFeature",
"sentry.User",
"sentry.OrganizationMapping",
"sentry.Project",
"sentry.ProjectBookmark",
Expand Down Expand Up @@ -62,12 +61,13 @@
"sentry.ProcessingIssue",
"sentry.OrgAuthToken",
"sentry.OrganizationOnboardingTask",
"sentry.LostPasswordHash",
"sentry.LatestAppConnectBuildsCheck",
"sentry.ProjectIntegration",
"sentry.OrganizationIntegration",
"sentry.ExternalIssue",
"sentry.Identity",
"sentry.GroupTombstone",
"sentry.LostPasswordHash",
"sentry.Release",
"sentry.ReleaseProject",
"sentry.OrganizationMember",
Expand Down
31 changes: 19 additions & 12 deletions src/sentry/notifications/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from sentry.models.notificationsettingoption import NotificationSettingOption
from sentry.models.notificationsettingprovider import NotificationSettingProvider
from sentry.models.user import User
from sentry.notifications.defaults import (
NOTIFICATION_SETTING_DEFAULTS,
NOTIFICATION_SETTINGS_ALL_SOMETIMES,
Expand All @@ -33,6 +32,7 @@
from sentry.services.hybrid_cloud.actor import ActorType, RpcActor
from sentry.services.hybrid_cloud.notifications import RpcNotificationSetting
from sentry.services.hybrid_cloud.user.model import RpcUser
from sentry.services.hybrid_cloud.user.service import user_service
from sentry.types.integrations import (
EXTERNAL_PROVIDERS,
ExternalProviderEnum,
Expand All @@ -42,7 +42,14 @@
)

if TYPE_CHECKING:
from sentry.models import Group, GroupSubscription, Organization, Project, Team # noqa: S005
from sentry.models import ( # noqa: S005
Group,
GroupSubscription,
Organization,
Project,
Team,
User,
)


def _get_notification_setting_default(
Expand Down Expand Up @@ -648,15 +655,15 @@ def get_providers_for_recipient(
return user_providers


def recipient_is_user(recipient: RpcActor | Team | User) -> bool:
def recipient_is_user(recipient: RpcActor | Team | RpcUser) -> bool:
from sentry.models.user import User

if isinstance(recipient, RpcActor) and recipient.actor_type == ActorType.USER:
return True
return isinstance(recipient, (RpcUser, User))


def recipient_is_team(recipient: RpcActor | Team | User) -> bool:
def recipient_is_team(recipient: RpcActor | Team | RpcUser) -> bool:
from sentry.models.team import Team

if isinstance(recipient, RpcActor) and recipient.actor_type == ActorType.TEAM:
Expand All @@ -665,7 +672,7 @@ def recipient_is_team(recipient: RpcActor | Team | User) -> bool:


def get_query(
recipients: Iterable[RpcActor | Team | User] | None = None,
recipients: Iterable[RpcActor | Team | RpcUser] | None = None,
project: Project | None = None,
organization: Organization | None = None,
) -> Q:
Expand Down Expand Up @@ -723,7 +730,7 @@ def get_query(


def get_all_setting_providers(
recipients: Iterable[RpcActor | Team | User] | None = None,
recipients: Iterable[RpcActor | Team | RpcUser] | None = None,
project: Project | None = None,
organization: Organization | None = None,
) -> Iterable[NotificationSettingProvider]:
Expand All @@ -743,7 +750,7 @@ def get_all_setting_providers(


def get_all_setting_options(
recipients: Iterable[RpcActor | Team | User] | None = None,
recipients: Iterable[RpcActor | Team | RpcUser] | None = None,
project: Project | None = None,
organization: Organization | None = None,
) -> Iterable[NotificationSettingOption]:
Expand All @@ -762,7 +769,7 @@ def get_all_setting_options(


def get_setting_options_for_recipient(
recipient: RpcActor | Team | User,
recipient: RpcActor | Team | RpcUser,
project: Project | None = None,
organization: Organization | None = None,
) -> MutableMapping[NotificationSettingEnum, NotificationSettingsOptionEnum]:
Expand Down Expand Up @@ -827,7 +834,7 @@ def get_setting_options_for_users(
if not additional_filters:
additional_filters = Q()

users = User.objects.filter(id__in=user_ids)
users = user_service.get_many(filter={"user_ids": user_ids})

query = get_query(recipients=users, project=project, organization=organization)
notification_settings = NotificationSettingOption.objects.filter(query & additional_filters)
Expand Down Expand Up @@ -896,7 +903,7 @@ def get_setting_providers_for_users(
project: The project to get notification settings for.
organization: The organization to get notification settings for.
"""
users = User.objects.filter(id__in=user_ids)
users = user_service.get_many(filter={"user_ids": user_ids})
query = get_query(recipients=users, project=project, organization=organization)
notification_settings = NotificationSettingProvider.objects.filter(query)

Expand Down Expand Up @@ -953,7 +960,7 @@ def get_notification_recipients(project: Project) -> Mapping[ExternalProviderEnu
Args:
project: The project to get notification settings for.
"""
user_ids = project.member_set.values_list("user_id", flat=True)
user_ids = list(project.member_set.values_list("user_id", flat=True))
options = get_setting_options_for_users(user_ids, project=project)
providers = get_setting_providers_for_users(user_ids, project=project)

Expand All @@ -978,7 +985,7 @@ def get_notification_recipients(project: Project) -> Mapping[ExternalProviderEnu


def user_has_any_provider_settings(
recipient: RpcActor | Team | User, provider: ExternalProviderEnum
recipient: RpcActor | Team | RpcUser, provider: ExternalProviderEnum
) -> bool:
"""
Returns whether the recipient has any notification settings for the given provider.
Expand Down

0 comments on commit 731bdbc

Please sign in to comment.