Example #1
0
 def test_where_should_be_participating(self):
     subscriptions_by_user_id = {ExternalProviders.EMAIL: {self.user: -1}}
     notification_settings = {
         self.user: {
             NotificationScopeType.USER: {
                 ExternalProviders.EMAIL:
                 NotificationSettingOptionValues.ALWAYS
             }
         }
     }
     assert (where_should_be_participating(
         self.user,
         subscriptions_by_user_id,
         notification_settings,
     ) == [ExternalProviders.EMAIL])
Example #2
0
    def test_subscription_null(self):
        notification_settings = {
            self.user: {
                NotificationScopeType.USER: {
                    ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
                    ExternalProviders.SLACK: NotificationSettingOptionValues.SUBSCRIBE_ONLY,
                    ExternalProviders.PAGERDUTY: NotificationSettingOptionValues.NEVER,
                }
            }
        }

        providers = where_should_be_participating(
            self.user,
            None,
            notification_settings,
        )
        assert providers == [ExternalProviders.EMAIL]
Example #3
0
    def test_where_should_be_participating(self):
        subscription = GroupSubscription(is_active=True)
        notification_settings = {
            self.user: {
                NotificationScopeType.USER: {
                    ExternalProviders.EMAIL: NotificationSettingOptionValues.ALWAYS,
                    ExternalProviders.SLACK: NotificationSettingOptionValues.SUBSCRIBE_ONLY,
                    ExternalProviders.PAGERDUTY: NotificationSettingOptionValues.NEVER,
                }
            }
        }

        providers = where_should_be_participating(
            self.user,
            subscription,
            notification_settings,
        )
        assert providers == [ExternalProviders.EMAIL, ExternalProviders.SLACK]
Example #4
0
    def get_participants(
            self, group: "Group"
    ) -> Mapping[ExternalProviders, Mapping["User", int]]:
        """
        Identify all users who are participating with a given issue.
        :param group: Group object
        """
        from sentry.models import NotificationSetting, User

        all_possible_users = User.objects.get_from_group(group)
        active_and_disabled_subscriptions = self.filter(
            group=group, user__in=all_possible_users)
        notification_settings = NotificationSetting.objects.get_for_recipient_by_parent(
            NotificationSettingTypes.WORKFLOW,
            recipients=all_possible_users,
            parent=group.project,
        )
        subscriptions_by_user_id = {
            subscription.user_id: subscription
            for subscription in active_and_disabled_subscriptions
        }
        notification_settings_by_user = transform_to_notification_settings_by_user(
            notification_settings, all_possible_users)

        should_use_slack_automatic = features.has(
            "organizations:notification-slack-automatic", group.organization)

        result: MutableMapping[ExternalProviders,
                               MutableMapping["User", int]] = defaultdict(dict)
        for user in all_possible_users:
            subscription_option = subscriptions_by_user_id.get(user.id)
            providers = where_should_be_participating(
                user,
                subscription_option,
                notification_settings_by_user,
                should_use_slack_automatic=should_use_slack_automatic,
            )
            for provider in providers:
                result[provider][user] = (subscription_option
                                          and subscription_option.reason
                                          or GroupSubscriptionReason.implicit)

        return result
Example #5
0
    def get_participants(
            self, group) -> Mapping[ExternalProviders, Mapping[Any, int]]:
        """
        Identify all users who are participating with a given issue.
        :param group: Group object
        """
        from sentry.models import NotificationSetting, User

        users = User.objects.get_from_group(group)
        user_ids = [user.id for user in users]
        subscriptions = self.filter(group=group, user_id__in=user_ids)
        notification_settings = NotificationSetting.objects.get_for_users_by_parent(
            NotificationSettingTypes.WORKFLOW,
            users=users,
            parent=group.project,
        )
        subscriptions_by_user_id = {
            subscription.user_id: subscription
            for subscription in subscriptions
        }
        notification_settings_by_user = transform_to_notification_settings_by_user(
            notification_settings, users)

        result = defaultdict(dict)
        for user in users:
            providers = where_should_be_participating(
                user,
                subscriptions_by_user_id,
                notification_settings_by_user,
            )
            for provider in providers:
                value = getattr(
                    subscriptions_by_user_id.get(user.id),
                    "reason",
                    GroupSubscriptionReason.implicit,
                )
                result[provider][user] = value

        return result