From c626e12fa310e9b5eb5fcee2e49d2477055c8471 Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Thu, 17 Feb 2022 13:10:34 +0100 Subject: [PATCH] LNDHhub for fastapi with LIMIT (#499) * add limit to get_payments * add limit to lndhub * fix defaults * default memo * add offset cluase * offset for the api --- lnbits/core/crud.py | 17 +++++++++++++-- lnbits/extensions/lndhub/views_api.py | 31 ++++++++++++++++++++------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index da1340e0c..a63f52c4d 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -61,7 +61,9 @@ async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[ email=user["email"], extensions=[e[0] for e in extensions], wallets=[Wallet(**w) for w in wallets], - admin=user["id"] in [x.strip() for x in LNBITS_ADMIN_USERS] if LNBITS_ADMIN_USERS else False + admin=user["id"] in [x.strip() for x in LNBITS_ADMIN_USERS] + if LNBITS_ADMIN_USERS + else False, ) @@ -217,6 +219,8 @@ async def get_payments( incoming: bool = False, since: Optional[int] = None, exclude_uncheckable: bool = False, + limit: Optional[int] = None, + offset: Optional[int] = None, conn: Optional[Connection] = None, ) -> List[Payment]: """ @@ -261,6 +265,15 @@ async def get_payments( clause.append("checking_id NOT LIKE 'temp_%'") clause.append("checking_id NOT LIKE 'internal_%'") + limit_clause = f"LIMIT {limit}" if type(limit) == int and limit > 0 else "" + offset_clause = f"OFFSET {offset}" if type(offset) == int and offset > 0 else "" + # combine limit and offset clauses + limit_offset_clause = ( + f"{limit_clause} {offset_clause}" + if limit_clause and offset_clause + else limit_clause or offset_clause + ) + where = "" if clause: where = f"WHERE {' AND '.join(clause)}" @@ -271,10 +284,10 @@ async def get_payments( FROM apipayments {where} ORDER BY time DESC + {limit_offset_clause} """, tuple(args), ) - return [Payment.from_row(row) for row in rows] diff --git a/lnbits/extensions/lndhub/views_api.py b/lnbits/extensions/lndhub/views_api.py index fdda13562..8cbf5b01c 100644 --- a/lnbits/extensions/lndhub/views_api.py +++ b/lnbits/extensions/lndhub/views_api.py @@ -1,4 +1,6 @@ import time +import asyncio + from base64 import urlsafe_b64encode from http import HTTPStatus @@ -11,7 +13,7 @@ from lnbits import bolt11 from lnbits.core.crud import delete_expired_invoices, get_payments from lnbits.core.services import create_invoice, pay_invoice from lnbits.decorators import WalletTypeInfo -from lnbits.settings import WALLET +from lnbits.settings import WALLET, LNBITS_SITE_TITLE from . import lndhub_ext from .decorators import check_wallet, require_admin_key @@ -55,14 +57,13 @@ async def lndhub_addinvoice( _, pr = await create_invoice( wallet_id=wallet.wallet.id, amount=int(data.amt), - memo=data.memo or "received sats", + memo=data.memo or LNBITS_SITE_TITLE, extra={"tag": "lndhub"}, ) except: raise HTTPException( status_code=HTTPStatus.NOT_FOUND, detail="Failed to create invoice" ) - invoice = bolt11.decode(pr) return { "pay_req": pr, @@ -116,7 +117,9 @@ async def lndhub_balance( @lndhub_ext.get("/ext/gettxs") async def lndhub_gettxs( - wallet: WalletTypeInfo = Depends(check_wallet), limit: int = Query(0, ge=0, lt=200) + wallet: WalletTypeInfo = Depends(check_wallet), + limit: int = Query(20, ge=1, le=20), + offset: int = Query(0, ge=0), ): for payment in await get_payments( wallet_id=wallet.wallet.id, @@ -124,11 +127,15 @@ async def lndhub_gettxs( pending=True, outgoing=True, incoming=False, + limit=limit, + offset=offset, exclude_uncheckable=True, ): await payment.set_pending( (await WALLET.get_payment_status(payment.checking_id)).pending ) + await asyncio.sleep(0.1) + return [ { "payment_preimage": payment.preimage, @@ -148,28 +155,34 @@ async def lndhub_gettxs( complete=True, outgoing=True, incoming=False, + limit=limit, + offset=offset, ) - )[:limit] + ) ) ] @lndhub_ext.get("/ext/getuserinvoices") async def lndhub_getuserinvoices( - wallet: WalletTypeInfo = Depends(check_wallet), limit: int = Query(0, ge=0, lt=200) + wallet: WalletTypeInfo = Depends(check_wallet), + limit: int = Query(20, ge=1, le=20), + offset: int = Query(0, ge=0), ): - await delete_expired_invoices() for invoice in await get_payments( wallet_id=wallet.wallet.id, complete=False, pending=True, outgoing=False, incoming=True, + limit=limit, + offset=offset, exclude_uncheckable=True, ): await invoice.set_pending( (await WALLET.get_invoice_status(invoice.checking_id)).pending ) + await asyncio.sleep(0.1) return [ { @@ -192,8 +205,10 @@ async def lndhub_getuserinvoices( complete=True, incoming=True, outgoing=False, + limit=limit, + offset=offset, ) - )[:limit] + ) ) ]