refactoring some stuff
This commit is contained in:
parent
2b65682960
commit
9bace16a45
5 changed files with 23 additions and 29 deletions
|
|
@ -95,6 +95,15 @@ async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[Withdraw
|
||||||
return [WithdrawLink(**row) for row in rows]
|
return [WithdrawLink(**row) for row in rows]
|
||||||
|
|
||||||
|
|
||||||
|
async def remove_unique_withdraw_link(link: WithdrawLink, unique_hash: str) -> None:
|
||||||
|
unique_links = link.usescsv.split(",")
|
||||||
|
unique_links.remove(unique_hash)
|
||||||
|
await update_withdraw_link(
|
||||||
|
link.id,
|
||||||
|
usescsv=",".join(unique_links),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def increment_withdraw_link(link: WithdrawLink) -> None:
|
async def increment_withdraw_link(link: WithdrawLink) -> None:
|
||||||
await update_withdraw_link(
|
await update_withdraw_link(
|
||||||
link.id,
|
link.id,
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,20 @@ from http import HTTPStatus
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import shortuuid
|
import shortuuid
|
||||||
|
from fastapi import HTTPException, Response, Request, Query
|
||||||
from fastapi import HTTPException
|
|
||||||
from fastapi.param_functions import Query
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from starlette.requests import Request
|
|
||||||
from starlette.responses import HTMLResponse
|
|
||||||
|
|
||||||
from lnbits.core.crud import update_payment_extra
|
from lnbits.core.crud import update_payment_extra
|
||||||
from lnbits.core.services import pay_invoice
|
from lnbits.core.services import pay_invoice
|
||||||
|
|
||||||
from . import withdraw_ext
|
from . import withdraw_ext
|
||||||
from .crud import get_withdraw_link_by_hash, increment_withdraw_link
|
from .crud import get_withdraw_link_by_hash, remove_unique_withdraw_link, increment_withdraw_link
|
||||||
from .models import WithdrawLink
|
from .models import WithdrawLink
|
||||||
|
|
||||||
|
|
||||||
@withdraw_ext.get(
|
@withdraw_ext.get(
|
||||||
"/api/v1/lnurl/{unique_hash}",
|
"/api/v1/lnurl/{unique_hash}",
|
||||||
response_class=HTMLResponse,
|
response_class=Response,
|
||||||
name="withdraw.api_lnurl_response",
|
name="withdraw.api_lnurl_response",
|
||||||
)
|
)
|
||||||
async def api_lnurl_response(request: Request, unique_hash):
|
async def api_lnurl_response(request: Request, unique_hash):
|
||||||
|
|
@ -97,8 +93,7 @@ async def api_lnurl_callback(
|
||||||
|
|
||||||
if id_unique_hash:
|
if id_unique_hash:
|
||||||
if check_unique_link(link, id_unique_hash):
|
if check_unique_link(link, id_unique_hash):
|
||||||
# remove it from usescsv list
|
await remove_unique_withdraw_link(link, id_unique_hash)
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="withdraw not found."
|
status_code=HTTPStatus.NOT_FOUND, detail="withdraw not found."
|
||||||
|
|
@ -122,8 +117,10 @@ async def api_lnurl_callback(
|
||||||
|
|
||||||
|
|
||||||
def check_unique_link(link: WithdrawLink, unique_hash: str) -> bool:
|
def check_unique_link(link: WithdrawLink, unique_hash: str) -> bool:
|
||||||
unique_links = link.usescsv.split(",")
|
return any(
|
||||||
return any(unique_hash == shortuuid.uuid(name=link.id + link.unique_hash + x.strip()) for x in unique_links)
|
unique_hash == shortuuid.uuid(name=link.id + link.unique_hash + x.strip())
|
||||||
|
for x in link.usescsv.split(",")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def dispatch_webhook(
|
async def dispatch_webhook(
|
||||||
|
|
@ -166,7 +163,7 @@ async def dispatch_webhook(
|
||||||
# FOR LNURLs WHICH ARE UNIQUE
|
# FOR LNURLs WHICH ARE UNIQUE
|
||||||
@withdraw_ext.get(
|
@withdraw_ext.get(
|
||||||
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
|
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
|
||||||
response_class=HTMLResponse,
|
response_class=Response,
|
||||||
name="withdraw.api_lnurl_multi_response",
|
name="withdraw.api_lnurl_multi_response",
|
||||||
)
|
)
|
||||||
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
|
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
|
||||||
|
|
@ -182,14 +179,7 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent."
|
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent."
|
||||||
)
|
)
|
||||||
|
|
||||||
useslist = link.usescsv.split(",")
|
if not check_unique_link(link, id_unique_hash):
|
||||||
found = False
|
|
||||||
for x in useslist:
|
|
||||||
tohash = link.id + link.unique_hash + str(x)
|
|
||||||
if id_unique_hash == shortuuid.uuid(name=tohash):
|
|
||||||
found = True
|
|
||||||
|
|
||||||
if not found:
|
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import shortuuid
|
import shortuuid
|
||||||
from fastapi import Query
|
from fastapi import Query
|
||||||
from lnurl import Lnurl, LnurlWithdrawResponse
|
from lnurl import Lnurl, LnurlWithdrawResponse
|
||||||
from lnurl.models import ClearnetUrl, MilliSatoshi
|
|
||||||
from lnurl import encode as lnurl_encode
|
from lnurl import encode as lnurl_encode
|
||||||
|
from lnurl.models import ClearnetUrl, MilliSatoshi
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,8 @@ from http import HTTPStatus
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
import pyqrcode
|
import pyqrcode
|
||||||
from fastapi import Request, Depends
|
from fastapi import Depends, HTTPException, Request
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from starlette.exceptions import HTTPException
|
|
||||||
from starlette.responses import HTMLResponse, StreamingResponse
|
from starlette.responses import HTMLResponse, StreamingResponse
|
||||||
|
|
||||||
from lnbits.core.models import User
|
from lnbits.core.models import User
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
from typing import Optional
|
from fastapi import Depends, HTTPException, Query, Request
|
||||||
|
|
||||||
from fastapi import Query, Depends
|
|
||||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
|
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
|
||||||
from starlette.exceptions import HTTPException
|
|
||||||
from starlette.requests import Request
|
|
||||||
|
|
||||||
from lnbits.core.crud import get_user
|
from lnbits.core.crud import get_user
|
||||||
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
||||||
|
|
@ -70,7 +66,7 @@ async def api_link_retrieve(
|
||||||
async def api_link_create_or_update(
|
async def api_link_create_or_update(
|
||||||
req: Request,
|
req: Request,
|
||||||
data: CreateWithdrawData,
|
data: CreateWithdrawData,
|
||||||
link_id: Optional[str] = Query(),
|
link_id: str = Query(None),
|
||||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||||
):
|
):
|
||||||
if data.uses > 250:
|
if data.uses > 250:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue