From f876f0659f6387f57597a17484c9b11126515490 Mon Sep 17 00:00:00 2001 From: ben Date: Thu, 24 Nov 2022 00:21:39 +0000 Subject: [PATCH] Moved into correct files, and added payment example --- lnbits/core/services.py | 24 +++++++++++++++++++++++- lnbits/core/tasks.py | 4 +++- lnbits/core/views/api.py | 27 ++------------------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 5d993b4c5..72a0c84b7 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -6,7 +6,7 @@ from typing import Dict, Optional, Tuple from urllib.parse import parse_qs, urlparse import httpx -from fastapi import Depends +from fastapi import Depends, WebSocket, WebSocketDisconnect from lnurl import LnurlErrorResponse from lnurl import decode as decode_lnurl # type: ignore 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 def fee_reserve(amount_msat: int) -> int: 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) \ No newline at end of file diff --git a/lnbits/core/tasks.py b/lnbits/core/tasks.py index b57e26257..01d04a606 100644 --- a/lnbits/core/tasks.py +++ b/lnbits/core/tasks.py @@ -10,6 +10,7 @@ from lnbits.tasks import SseListenersDict, register_invoice_listener from . import db from .crud import get_balance_notify from .models import Payment +from .services import websocketUpdater api_invoice_listeners: Dict[str, asyncio.Queue] = SseListenersDict( "api_invoice_listeners" @@ -38,6 +39,7 @@ async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue): logger.trace("received invoice paid event") # send information to sse channel await dispatch_api_invoice_listeners(payment) + await websocketUpdater(payment.wallet_id, payment) # dispatch webhook if payment.webhook and not payment.webhook_status: @@ -88,4 +90,4 @@ async def mark_webhook_sent(payment: Payment, status: int) -> None: WHERE hash = ? """, (status, payment.payment_hash), - ) + ) \ No newline at end of file diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index e65206593..3465ef248 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -56,6 +56,8 @@ from ..services import ( create_invoice, pay_invoice, perform_lnurlauth, + websocketManager, + websocketUpdater ) from ..tasks import api_invoice_listeners @@ -702,27 +704,6 @@ async def api_auditor(wallet: WalletTypeInfo = Depends(get_key_type)): ##################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}") async def websocket_connect(websocket: WebSocket, item_id: str): await websocketManager.connect(websocket, item_id) @@ -749,7 +730,3 @@ async def websocket_update(item_id: str, data: str): return {"sent": True, "data": data} except: return {"sent": False, "data": data} - - -async def websocketUpdater(item_id, data): - return await websocketManager.send_data(f"{data}", item_id)