mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-29 05:12:39 +02:00
Moved into correct files, and added payment example
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import Dict, Optional, Tuple
|
|||||||
from urllib.parse import parse_qs, urlparse
|
from urllib.parse import parse_qs, urlparse
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from fastapi import Depends
|
from fastapi import Depends, WebSocket, WebSocketDisconnect
|
||||||
from lnurl import LnurlErrorResponse
|
from lnurl import LnurlErrorResponse
|
||||||
from lnurl import decode as decode_lnurl # type: ignore
|
from lnurl import decode as decode_lnurl # type: ignore
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -382,3 +382,25 @@ async def check_transaction_status(
|
|||||||
# WARN: this same value must be used for balance check and passed to WALLET.pay_invoice(), it may cause a vulnerability if the values differ
|
# WARN: this same value must be used for balance check and passed to WALLET.pay_invoice(), it may cause a vulnerability if the values differ
|
||||||
def fee_reserve(amount_msat: int) -> int:
|
def fee_reserve(amount_msat: int) -> int:
|
||||||
return max(int(RESERVE_FEE_MIN), int(amount_msat * RESERVE_FEE_PERCENT / 100.0))
|
return max(int(RESERVE_FEE_MIN), int(amount_msat * RESERVE_FEE_PERCENT / 100.0))
|
||||||
|
|
||||||
|
class websocketConnectionManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.active_connections: List[WebSocket] = []
|
||||||
|
|
||||||
|
async def connect(self, websocket: WebSocket, item_id: str):
|
||||||
|
await websocket.accept()
|
||||||
|
websocket.id = item_id
|
||||||
|
self.active_connections.append(websocket)
|
||||||
|
|
||||||
|
def disconnect(self, websocket: WebSocket):
|
||||||
|
self.active_connections.remove(websocket)
|
||||||
|
|
||||||
|
async def send_data(self, message: str, item_id: str):
|
||||||
|
for connection in self.active_connections:
|
||||||
|
if connection.id == item_id:
|
||||||
|
await connection.send_text(message)
|
||||||
|
|
||||||
|
websocketManager = websocketConnectionManager()
|
||||||
|
|
||||||
|
async def websocketUpdater(item_id, data):
|
||||||
|
return await websocketManager.send_data(f"{data}", item_id)
|
@@ -10,6 +10,7 @@ from lnbits.tasks import SseListenersDict, register_invoice_listener
|
|||||||
from . import db
|
from . import db
|
||||||
from .crud import get_balance_notify
|
from .crud import get_balance_notify
|
||||||
from .models import Payment
|
from .models import Payment
|
||||||
|
from .services import websocketUpdater
|
||||||
|
|
||||||
api_invoice_listeners: Dict[str, asyncio.Queue] = SseListenersDict(
|
api_invoice_listeners: Dict[str, asyncio.Queue] = SseListenersDict(
|
||||||
"api_invoice_listeners"
|
"api_invoice_listeners"
|
||||||
@@ -38,6 +39,7 @@ async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue):
|
|||||||
logger.trace("received invoice paid event")
|
logger.trace("received invoice paid event")
|
||||||
# send information to sse channel
|
# send information to sse channel
|
||||||
await dispatch_api_invoice_listeners(payment)
|
await dispatch_api_invoice_listeners(payment)
|
||||||
|
await websocketUpdater(payment.wallet_id, payment)
|
||||||
|
|
||||||
# dispatch webhook
|
# dispatch webhook
|
||||||
if payment.webhook and not payment.webhook_status:
|
if payment.webhook and not payment.webhook_status:
|
||||||
@@ -88,4 +90,4 @@ async def mark_webhook_sent(payment: Payment, status: int) -> None:
|
|||||||
WHERE hash = ?
|
WHERE hash = ?
|
||||||
""",
|
""",
|
||||||
(status, payment.payment_hash),
|
(status, payment.payment_hash),
|
||||||
)
|
)
|
@@ -56,6 +56,8 @@ from ..services import (
|
|||||||
create_invoice,
|
create_invoice,
|
||||||
pay_invoice,
|
pay_invoice,
|
||||||
perform_lnurlauth,
|
perform_lnurlauth,
|
||||||
|
websocketManager,
|
||||||
|
websocketUpdater
|
||||||
)
|
)
|
||||||
from ..tasks import api_invoice_listeners
|
from ..tasks import api_invoice_listeners
|
||||||
|
|
||||||
@@ -702,27 +704,6 @@ async def api_auditor(wallet: WalletTypeInfo = Depends(get_key_type)):
|
|||||||
##################UNIVERSAL WEBSOCKET MANAGER########################
|
##################UNIVERSAL WEBSOCKET MANAGER########################
|
||||||
|
|
||||||
|
|
||||||
class websocketConnectionManager:
|
|
||||||
def __init__(self):
|
|
||||||
self.active_connections: List[WebSocket] = []
|
|
||||||
|
|
||||||
async def connect(self, websocket: WebSocket, item_id: str):
|
|
||||||
await websocket.accept()
|
|
||||||
websocket.id = item_id
|
|
||||||
self.active_connections.append(websocket)
|
|
||||||
|
|
||||||
def disconnect(self, websocket: WebSocket):
|
|
||||||
self.active_connections.remove(websocket)
|
|
||||||
|
|
||||||
async def send_data(self, message: str, item_id: str):
|
|
||||||
for connection in self.active_connections:
|
|
||||||
if connection.id == item_id:
|
|
||||||
await connection.send_text(message)
|
|
||||||
|
|
||||||
|
|
||||||
websocketManager = websocketConnectionManager()
|
|
||||||
|
|
||||||
|
|
||||||
@core_app.websocket("/api/v1/ws/{item_id}")
|
@core_app.websocket("/api/v1/ws/{item_id}")
|
||||||
async def websocket_connect(websocket: WebSocket, item_id: str):
|
async def websocket_connect(websocket: WebSocket, item_id: str):
|
||||||
await websocketManager.connect(websocket, item_id)
|
await websocketManager.connect(websocket, item_id)
|
||||||
@@ -749,7 +730,3 @@ async def websocket_update(item_id: str, data: str):
|
|||||||
return {"sent": True, "data": data}
|
return {"sent": True, "data": data}
|
||||||
except:
|
except:
|
||||||
return {"sent": False, "data": data}
|
return {"sent": False, "data": data}
|
||||||
|
|
||||||
|
|
||||||
async def websocketUpdater(item_id, data):
|
|
||||||
return await websocketManager.send_data(f"{data}", item_id)
|
|
||||||
|
Reference in New Issue
Block a user