mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-24 20:09:38 +02:00
[fix] SQL error for create webpush notification
(#2533)
* fix: replace all SQL `user = ?` with `"user"" = ?` * fix: surround with try-catch * fix: bad double quote
This commit is contained in:
@@ -1263,7 +1263,7 @@ async def get_webpush_subscription(
|
|||||||
endpoint: str, user: str
|
endpoint: str, user: str
|
||||||
) -> Optional[WebPushSubscription]:
|
) -> Optional[WebPushSubscription]:
|
||||||
row = await db.fetchone(
|
row = await db.fetchone(
|
||||||
"SELECT * FROM webpush_subscriptions WHERE endpoint = ? AND user = ?",
|
"""SELECT * FROM webpush_subscriptions WHERE endpoint = ? AND "user" = ?""",
|
||||||
(
|
(
|
||||||
endpoint,
|
endpoint,
|
||||||
user,
|
user,
|
||||||
@@ -1276,7 +1276,7 @@ async def get_webpush_subscriptions_for_user(
|
|||||||
user: str,
|
user: str,
|
||||||
) -> List[WebPushSubscription]:
|
) -> List[WebPushSubscription]:
|
||||||
rows = await db.fetchall(
|
rows = await db.fetchall(
|
||||||
"SELECT * FROM webpush_subscriptions WHERE user = ?",
|
"""SELECT * FROM webpush_subscriptions WHERE "user" = ?""",
|
||||||
(user,),
|
(user,),
|
||||||
)
|
)
|
||||||
return [WebPushSubscription(**dict(row)) for row in rows]
|
return [WebPushSubscription(**dict(row)) for row in rows]
|
||||||
@@ -1304,7 +1304,7 @@ async def create_webpush_subscription(
|
|||||||
|
|
||||||
async def delete_webpush_subscription(endpoint: str, user: str) -> None:
|
async def delete_webpush_subscription(endpoint: str, user: str) -> None:
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"DELETE FROM webpush_subscriptions WHERE endpoint = ? AND user = ?",
|
"""DELETE FROM webpush_subscriptions WHERE endpoint = ? AND "user" = ?""",
|
||||||
(
|
(
|
||||||
endpoint,
|
endpoint,
|
||||||
user,
|
user,
|
||||||
|
@@ -366,7 +366,8 @@ async def m014_set_deleted_wallets(db):
|
|||||||
inkey = row[4].split(":")[1]
|
inkey = row[4].split(":")[1]
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE wallets SET user = ?, adminkey = ?, inkey = ?, deleted = true
|
UPDATE wallets SET
|
||||||
|
"user" = ?, adminkey = ?, inkey = ?, deleted = true
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
""",
|
""",
|
||||||
(user, adminkey, inkey, row[0]),
|
(user, adminkey, inkey, row[0]),
|
||||||
|
@@ -6,8 +6,10 @@ from urllib.parse import unquote, urlparse
|
|||||||
from fastapi import (
|
from fastapi import (
|
||||||
APIRouter,
|
APIRouter,
|
||||||
Depends,
|
Depends,
|
||||||
|
HTTPException,
|
||||||
Request,
|
Request,
|
||||||
)
|
)
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.models import (
|
from lnbits.core.models import (
|
||||||
CreateWebPushSubscription,
|
CreateWebPushSubscription,
|
||||||
@@ -33,20 +35,27 @@ async def api_create_webpush_subscription(
|
|||||||
data: CreateWebPushSubscription,
|
data: CreateWebPushSubscription,
|
||||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||||
) -> WebPushSubscription:
|
) -> WebPushSubscription:
|
||||||
subscription = json.loads(data.subscription)
|
try:
|
||||||
endpoint = subscription["endpoint"]
|
subscription = json.loads(data.subscription)
|
||||||
host = urlparse(str(request.url)).netloc
|
endpoint = subscription["endpoint"]
|
||||||
|
host = urlparse(str(request.url)).netloc
|
||||||
|
|
||||||
subscription = await get_webpush_subscription(endpoint, wallet.wallet.user)
|
subscription = await get_webpush_subscription(endpoint, wallet.wallet.user)
|
||||||
if subscription:
|
if subscription:
|
||||||
return subscription
|
return subscription
|
||||||
else:
|
else:
|
||||||
return await create_webpush_subscription(
|
return await create_webpush_subscription(
|
||||||
endpoint,
|
endpoint,
|
||||||
wallet.wallet.user,
|
wallet.wallet.user,
|
||||||
data.subscription,
|
data.subscription,
|
||||||
host,
|
host,
|
||||||
)
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug(exc)
|
||||||
|
raise HTTPException(
|
||||||
|
HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
"Cannot create webpush notification",
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
|
||||||
@webpush_router.delete("", status_code=HTTPStatus.OK)
|
@webpush_router.delete("", status_code=HTTPStatus.OK)
|
||||||
@@ -54,7 +63,14 @@ async def api_delete_webpush_subscription(
|
|||||||
request: Request,
|
request: Request,
|
||||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||||
):
|
):
|
||||||
endpoint = unquote(
|
try:
|
||||||
base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8")
|
endpoint = unquote(
|
||||||
)
|
base64.b64decode(str(request.query_params.get("endpoint"))).decode("utf-8")
|
||||||
await delete_webpush_subscription(endpoint, wallet.wallet.user)
|
)
|
||||||
|
await delete_webpush_subscription(endpoint, wallet.wallet.user)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.debug(exc)
|
||||||
|
raise HTTPException(
|
||||||
|
HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
"Cannot delete webpush notification",
|
||||||
|
) from exc
|
||||||
|
Reference in New Issue
Block a user