basic invoice listeners.

This commit is contained in:
fiatjaf
2020-10-04 12:04:47 -03:00
parent e2f6c20e3b
commit 04222f1f01
12 changed files with 193 additions and 26 deletions
+40 -2
View File
@@ -1,9 +1,13 @@
import asyncio
from typing import Optional, Awaitable
from typing import Optional, List, Awaitable, Tuple, Callable
from quart import Quart, Request, g
from werkzeug.datastructures import Headers
from lnbits.db import open_db
from lnbits.db import open_db, open_ext_db
from lnbits.settings import WALLET
from .models import Payment
from .crud import get_standalone_payment
main_app: Optional[Quart] = None
@@ -31,3 +35,37 @@ def run_on_pseudo_request(awaitable: Awaitable):
loop = asyncio.get_event_loop()
loop.create_task(run(awaitable))
invoice_listeners: List[Tuple[str, Callable[[Payment], Awaitable[None]]]] = []
def register_invoice_listener(ext_name: str, callback: Callable[[Payment], Awaitable[None]]):
"""
A method intended for extensions to call when they want to be notified about
new invoice payments incoming.
"""
print("registering callback", callback)
invoice_listeners.append((ext_name, callback))
async def webhook_handler():
handler = getattr(WALLET, "webhook_listener", None)
if handler:
await handler()
async def invoice_listener(app):
run_on_pseudo_request(_invoice_listener())
async def _invoice_listener():
async for checking_id in WALLET.paid_invoices_stream():
# do this just so the g object is available
g.db = await open_db()
payment = await get_standalone_payment(checking_id)
if payment.is_in:
await payment.set_pending(False)
for ext_name, cb in invoice_listeners:
g.ext_db = await open_ext_db(ext_name)
cb(payment)