diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index b9b4c0d349..cd242b5628 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -371,7 +371,7 @@ def num_push_devices_for_user(user_profile: UserProfile, kind: Optional[int]=Non def add_push_device_token(user_profile: UserProfile, token_str: str, kind: int, - ios_app_id: Optional[str]=None) -> None: + ios_app_id: Optional[str]=None) -> PushDeviceToken: logger.info("Registering push device: %d %r %d %r", user_profile.id, token_str, kind, ios_app_id) @@ -382,7 +382,7 @@ def add_push_device_token(user_profile: UserProfile, # keys for mobile push notifications. try: with transaction.atomic(): - PushDeviceToken.objects.create( + token = PushDeviceToken.objects.create( user_id=user_profile.id, kind=kind, token=token_str, @@ -390,7 +390,11 @@ def add_push_device_token(user_profile: UserProfile, # last_updated is to be renamed to date_created. last_updated=timezone_now()) except IntegrityError: - pass + token = PushDeviceToken.objects.get( + user_id=user_profile.id, + kind=kind, + token=token_str, + ) # If we're sending things to the push notification bouncer # register this user with them here @@ -409,6 +413,8 @@ def add_push_device_token(user_profile: UserProfile, # Calls zilencer.views.register_remote_push_device send_to_push_bouncer('POST', 'push/register', post_data) + return token + def remove_push_device_token(user_profile: UserProfile, token_str: str, kind: int) -> None: try: token = PushDeviceToken.objects.get(token=token_str, kind=kind, user=user_profile)