def match_video(cls, match, user_id=None):
        from models.notifications.match_video import MatchVideoNotification
        # Send to Event subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    match.event.get(), NotificationType.MATCH_VIDEO)
            if users:
                cls._send(users, MatchVideoNotification(match))

        # Send to Team subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.MATCH_VIDEO)
                if users:
                    cls._send(users,
                              MatchVideoNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.MATCH_VIDEO)
            if users:
                cls._send(users, MatchVideoNotification(match))
    def match_upcoming(cls, match, user_id=None):
        from models.notifications.match_upcoming import MatchUpcomingNotification
        # Send to Event subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    match.event.get(), NotificationType.UPCOMING_MATCH)
            if users:
                cls._send(users, MatchUpcomingNotification(match))

        # Send to Team subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.UPCOMING_MATCH)
                if users:
                    cls._send(users,
                              MatchUpcomingNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.UPCOMING_MATCH)
            if users:
                cls._send(users, MatchUpcomingNotification(match))

        # Send LEVEL_STARTING for the first match of a new type
        if match.set_number == 1 and match.match_number == 1:
            cls.event_level(match, user_id)
    def match_score(cls, match, user_id=None):
        event = match.event.get()

        from models.notifications.match_score import MatchScoreNotification
        # Send to Event subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    event, NotificationType.MATCH_SCORE)
            if users:
                cls._send(users, MatchScoreNotification(match))

        # Send to Team subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.MATCH_SCORE)
                if users:
                    cls._send(users,
                              MatchScoreNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.MATCH_SCORE)
            if users:
                cls._send(users, MatchScoreNotification(match))

        # Send UPCOMING_MATCH for the N + 2 match after this one
        if not event.matches:
            return
        from helpers.match_helper import MatchHelper
        next_matches = MatchHelper.upcomingMatches(event.matches, num=2)
        # TODO: Think about if we need special-case handling for replayed matches
        # (I don't think we do because if a match gets replayed at EoD, we'll cancel/reschedule
        # for that match notification. If a match gets replayed back-to-back (which doesn't happen?)
        # sending a second notification is probably fine.
        # If there are not 2 scheduled matches (end of Quals, end of Quarters, etc.) don't send
        if len(next_matches) < 2:
            return

        next_match = next_matches.pop()
        cls.schedule_upcoming_match(next_match, user_id)
 def test_users_subscribed_to_match_model_type(self):
     # Make sure we filter for model types
     Subscription(parent=ndb.Key(Account, 'user_id_1'),
                  user_id='user_id_1',
                  model_key='2020miket_qm1',
                  model_type=ModelType.MATCH,
                  notification_types=[NotificationType.UPCOMING_MATCH
                                      ]).put()
     Subscription(parent=ndb.Key(Account, 'user_id_2'),
                  user_id='user_id_2',
                  model_key='2020miket_qm1',
                  model_type=ModelType.EVENT,
                  notification_types=[NotificationType.UPCOMING_MATCH
                                      ]).put()
     users = Subscription.users_subscribed_to_match(
         self.match, NotificationType.UPCOMING_MATCH)
     self.assertItemsEqual(users, ['user_id_1'])