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 alliance_selection(cls, event, user_id=None): from models.notifications.alliance_selection import AllianceSelectionNotification # Send to Event subscribers if NotificationType.ALLIANCE_SELECTION in NotificationType.enabled_event_notifications: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_event( event, NotificationType.ALLIANCE_SELECTION) if users: cls._send(users, AllianceSelectionNotification(event)) # Send to Team subscribers if NotificationType.ALLIANCE_SELECTION in NotificationType.enabled_team_notifications: for team_key in event.alliance_teams: try: team = Team.get_by_id(team_key) except: continue users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_team( team, NotificationType.ALLIANCE_SELECTION) if users: cls._send(users, AllianceSelectionNotification(event, team))
def send_schedule_update(cls, event): users = Subscription.users_subscribed_to_event( event, NotificationType.SCHEDULE_UPDATED) keys = PushHelper.get_client_ids_for_users(users) notification = ScheduleUpdatedNotification(event) notification.send(keys)
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 send_award_update(cls, event): users = Subscription.users_subscribed_to_event(event, NotificationType.AWARDS) keys = PushHelper.get_client_ids_for_users( users, os_types=[ClientType.OS_ANDROID]) notification = AwardsUpdatedNotification(event) notification.send(keys)
def event_schedule(cls, event, user_id=None): from models.notifications.event_schedule import EventScheduleNotification # Send to Event subscribers if NotificationType.SCHEDULE_UPDATED in NotificationType.enabled_event_notifications: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_event(event, NotificationType.SCHEDULE_UPDATED) if users: cls._send(users, EventScheduleNotification(event))
def event_level(cls, match, user_id=None): from models.notifications.event_level import EventLevelNotification # Send to Event subscribers if NotificationType.LEVEL_STARTING 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.LEVEL_STARTING) if users: cls._send(users, EventLevelNotification(match))
def awards(cls, event, user_id=None): if user_id: users = [user_id] else: users = Subscription.users_subscribed_to_event( event, NotificationType.AWARDS) from models.notifications.awards import AwardsNotification # Send to FCM/webhooks cls._send(users, AwardsNotification(event))
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_event_year(self): # Make sure we match year* Subscription(parent=ndb.Key(Account, 'user_id_1'), user_id='user_id_1', model_key='2020*', model_type=ModelType.EVENT, notification_types=[NotificationType.UPCOMING_MATCH ]).put() Subscription(parent=ndb.Key(Account, 'user_id_2'), user_id='user_id_2', model_key='2020*', model_type=ModelType.EVENT, notification_types=[NotificationType.MATCH_SCORE]).put() users = Subscription.users_subscribed_to_event( self.event, NotificationType.UPCOMING_MATCH) self.assertEqual(users, ['user_id_1'])
def test_users_subscribed_to_event_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', model_type=ModelType.EVENT, notification_types=[NotificationType.UPCOMING_MATCH ]).put() Subscription(parent=ndb.Key(Account, 'user_id_2'), user_id='user_id_2', model_key='frc7332', model_type=ModelType.TEAM, notification_types=[NotificationType.UPCOMING_MATCH ]).put() users = Subscription.users_subscribed_to_event( self.event, NotificationType.UPCOMING_MATCH) self.assertItemsEqual(users, ['user_id_1'])
def test_users_subscribed_to_event_year_key(self): # Make sure we fetch both key and year together Subscription(parent=ndb.Key(Account, 'user_id_1'), user_id='user_id_1', model_key='2020miket', model_type=ModelType.EVENT, notification_types=[NotificationType.UPCOMING_MATCH ]).put() Subscription(parent=ndb.Key(Account, 'user_id_2'), user_id='user_id_2', model_key='2020*', model_type=ModelType.EVENT, notification_types=[NotificationType.UPCOMING_MATCH ]).put() users = Subscription.users_subscribed_to_event( self.event, NotificationType.UPCOMING_MATCH) self.assertItemsEqual(users, ['user_id_1', 'user_id_2'])
def test_users_subscribed_to_event_key(self): # Make sure we event key Subscription(parent=ndb.Key(Account, 'user_id_1'), user_id='user_id_1', model_key='2020miket', model_type=ModelType.EVENT, notification_types=[NotificationType.UPCOMING_MATCH ]).put() Subscription(parent=ndb.Key(Account, 'user_id_2'), user_id='user_id_2', model_key='2020mike2', model_type=ModelType.EVENT, notification_types=[NotificationType.MATCH_SCORE]).put() users = Subscription.users_subscribed_to_event( MockEvent(key_name="2020miket", year=2020), NotificationType.UPCOMING_MATCH) self.assertEqual(users, ['user_id_1'])
def send_match_video(cls, match): """ Sends match_video and event_match_video notifications If the match is current, MatchVideoNotification is sent. Otherwise, EventMatchVideoNotification is sent """ match_users = set( PushHelper.get_users_subscribed_to_match( match, NotificationType.MATCH_VIDEO)) event_users = set( Subscription.users_subscribed_to_event( match.event.get(), NotificationType.MATCH_VIDEO)) users = match_users.union(event_users) if match.within_seconds(60 * 10): user_keys = PushHelper.get_client_ids_for_users(users) MatchVideoNotification(match).send(user_keys) else: user_keys = PushHelper.get_client_ids_for_users(users) EventMatchVideoNotification(match).send(user_keys)
def awards(cls, event, user_id=None): from models.notifications.awards import AwardsNotification # Send to Event subscribers if NotificationType.AWARDS in NotificationType.enabled_event_notifications: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_event(event, NotificationType.AWARDS) if users: cls._send(users, AwardsNotification(event)) # Send to Team subscribers if NotificationType.AWARDS in NotificationType.enabled_team_notifications: # Map all Teams to their Awards so we can populate our Awards notification with more specific info team_awards = event.team_awards() for team_key in team_awards.keys(): team = team_key.get() users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_team(team, NotificationType.AWARDS) if users: cls._send(users, AwardsNotification(event, team))