def add_subscription(self, request): current_user = endpoints.get_current_user() if current_user is None: return BaseResponse(code=401, message="Unauthorized to add subscription") userId = PushHelper.user_email_to_id(current_user.email()) modelKey = request.model_key sub = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).get() if sub is None: # Subscription doesn't exist, add it Subscription( user_id = userId, model_key = modelKey, notification_types = PushHelper.notification_enums_from_string(request.notifications)).put() if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscription added") else: if sub.notification_types == PushHelper.notification_enums_from_string(request.notifications): # Subscription already exists. Don't add it again return BaseResponse(code=304, message="Subscription already exists") else: # We're updating the settings sub.notification_types = PushHelper.notification_enums_from_string(request.notifications) sub.put() if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscription updated")
def postUpdateHook(cls, matches): ''' To run after the match has been updated. Send push notifications to subscribed users Only if the match is part of an active event ''' for match in matches: if match.event.get().now: logging.info("Sending push notifications for "+match.key_name) try: GCMMessageHelper.send_match_score_update(match) except exception: logging.error("Error sending match updates: "+str(exception)) ''' Enqueue firebase push ''' if matches: event_key = matches[0].event.id() try: FirebasePusher.updated_event(event_key) except Exception: logging.warning("Enqueuing Firebase push failed!") # Enqueue task to calculate matchstats taskqueue.add( url='/tasks/math/do/event_matchstats/' + event_key, method='GET')
def remove_subscription(self, request): current_user = endpoints.get_current_user() if current_user is None: return BaseResponse(code=401, message="Unauthorized to remove subscription") userId = PushHelper.user_email_to_id(current_user.email()) modelKey = request.model_key to_delete = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscriptions deleted") else: # Subscription doesn't exist. Can't delete it return BaseResponse(code=404, message="Subscription not found")
def add_favorite(self, request): current_user = endpoints.get_current_user() if current_user is None: return BaseResponse(code=401, message="Unauthorized to add favorite") userId = PushHelper.user_email_to_id(current_user.email()) modelKey = request.model_key if Favorite.query( Favorite.user_id == userId, Favorite.model_key == modelKey).count() == 0: # Favorite doesn't exist, add it Favorite( user_id = userId, model_key = modelKey).put() if request.device_key: # Send updates to user's other devices logging.info("Sending favorite update to user other devices") GCMMessageHelper.send_favorite_update(userId, request.device_key) return BaseResponse(code=200, message="Favorite added") else: # Favorite already exists. Don't add it again return BaseResponse(code=304, message="Favorite already exists")