diff --git a/lnbits/core/views/webpush_api.py b/lnbits/core/views/webpush_api.py index 66a482312..9adf3fc75 100644 --- a/lnbits/core/views/webpush_api.py +++ b/lnbits/core/views/webpush_api.py @@ -9,7 +9,6 @@ from fastapi import ( HTTPException, Request, ) -from loguru import logger from lnbits.core.models import ( CreateWebPushSubscription, @@ -19,6 +18,7 @@ from lnbits.decorators import ( WalletTypeInfo, require_admin_key, ) +from lnbits.exceptions import NotFoundError from ..crud import ( create_webpush_subscription, @@ -38,40 +38,31 @@ async def api_create_webpush_subscription( try: subscription = json.loads(data.subscription) endpoint = subscription["endpoint"] - host = urlparse(str(request.url)).netloc - - subscription = await get_webpush_subscription(endpoint, wallet.wallet.user) - if subscription: - return subscription - else: - return await create_webpush_subscription( - endpoint, - wallet.wallet.user, - data.subscription, - host, - ) except Exception as exc: - logger.debug(exc) raise HTTPException( - HTTPStatus.INTERNAL_SERVER_ERROR, - "Cannot create webpush notification", + status_code=400, detail="Invalid subscription JSON." ) from exc + host = urlparse(str(request.url)).netloc + + try: + return await get_webpush_subscription(endpoint, wallet.wallet.user) + except NotFoundError: + return await create_webpush_subscription( + endpoint, + wallet.wallet.user, + data.subscription, + host, + ) + @webpush_router.delete("", status_code=HTTPStatus.OK) async def api_delete_webpush_subscription( request: Request, wallet: WalletTypeInfo = Depends(require_admin_key), ): - try: - endpoint = unquote( - base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8") - ) - count = await delete_webpush_subscription(endpoint, wallet.wallet.user) - return {"count": count} - except Exception as exc: - logger.debug(exc) - raise HTTPException( - HTTPStatus.INTERNAL_SERVER_ERROR, - "Cannot delete webpush notification", - ) from exc + endpoint = unquote( + base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8") + ) + count = await delete_webpush_subscription(endpoint, wallet.wallet.user) + return {"count": count}