From 1c451674cf7eab63d621ffe345796e408eefae25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 14 Oct 2024 10:30:44 +0200 Subject: [PATCH] make get_user better --- lnbits/core/crud.py | 54 ++++++++---------------------------- lnbits/core/views/generic.py | 4 +-- 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 6ce3ce5ae..3aa00f790 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -1,7 +1,7 @@ import json from datetime import datetime, timezone from time import time -from typing import Literal, Optional, Union +from typing import Literal, Optional from uuid import uuid4 import shortuuid @@ -161,15 +161,18 @@ async def get_account_by_username_or_email( ) +async def get_user_by_id( + user_id: str, conn: Optional[Connection] = None +) -> Optional[User]: + account = await get_account(user_id, conn) + if not account: + return None + return await get_user(account, conn) + + async def get_user( - account_or_id: Union[Account, str], conn: Optional[Connection] = None + account: Account, conn: Optional[Connection] = None ) -> Optional[User]: - if isinstance(account_or_id, str): - account = await get_account(account_or_id, conn) - if not account: - return None - else: - account = account_or_id extensions = await get_user_active_extensions_ids(account.id, conn) wallets = await get_wallets(account.id, False, conn=conn) return User( @@ -756,41 +759,6 @@ async def update_payment_details( ) -# TODO: should not be needed use update_payment instead -async def update_payment_extra( - payment_hash: str, - extra: dict, - outgoing: bool = False, - conn: Optional[Connection] = None, -) -> None: - """ - Only update the `extra` field for the payment. - Old values in the `extra` JSON object will be kept - unless the new `extra` overwrites them. - """ - - amount_clause = "AND amount < 0" if outgoing else "AND amount > 0" - - row: dict = await (conn or db).fetchone( - f""" - SELECT payment_hash, extra from apipayments - WHERE payment_hash = :hash {amount_clause} - """, - {"hash": payment_hash}, - ) - if not row: - return - db_extra = json.loads(row["extra"] if row["extra"] else "{}") - db_extra.update(extra) - - await (conn or db).execute( - f""" - UPDATE apipayments SET extra = :extra WHERE payment_hash = :hash {amount_clause} - """, - {"extra": json.dumps(db_extra), "hash": payment_hash}, - ) - - DateTrunc = Literal["hour", "day", "month"] sqlite_formats = { "hour": "%Y-%m-%d %H:00:00", diff --git a/lnbits/core/views/generic.py b/lnbits/core/views/generic.py index 85694cac3..6e8d91ab7 100644 --- a/lnbits/core/views/generic.py +++ b/lnbits/core/views/generic.py @@ -25,7 +25,7 @@ from ..crud import ( create_wallet, get_dbversions, get_installed_extensions, - get_user, + get_user_by_id, get_wallet, ) @@ -229,7 +229,7 @@ async def service_worker(request: Request): @generic_router.get("/manifest/{usr}.webmanifest") async def manifest(request: Request, usr: str): host = urlparse(str(request.url)).netloc - user = await get_user(usr) + user = await get_user_by_id(usr) if not user: raise HTTPException(status_code=HTTPStatus.NOT_FOUND) return {