2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2022-10-04 09:51:47 +02:00
|
|
|
from typing import Dict
|
2020-09-28 04:12:55 +02:00
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
import httpx
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
2022-10-04 09:51:47 +02:00
|
|
|
from lnbits.tasks import SseListenersDict, register_invoice_listener
|
2021-04-17 23:27:15 +02:00
|
|
|
|
2020-12-24 13:38:35 +01:00
|
|
|
from . import db
|
2021-04-17 23:27:15 +02:00
|
|
|
from .crud import get_balance_notify
|
2020-12-24 13:38:35 +01:00
|
|
|
from .models import Payment
|
2020-09-28 04:12:55 +02:00
|
|
|
|
2022-10-04 09:51:47 +02:00
|
|
|
api_invoice_listeners: Dict[str, asyncio.Queue] = SseListenersDict(
|
|
|
|
"api_invoice_listeners"
|
|
|
|
)
|
2020-09-28 04:12:55 +02:00
|
|
|
|
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
async def register_task_listeners():
|
2022-10-04 09:51:47 +02:00
|
|
|
"""
|
|
|
|
Registers an invoice listener queue for the core tasks.
|
|
|
|
Incoming payaments in this queue will eventually trigger the signals sent to all other extensions
|
|
|
|
and fulfill other core tasks such as dispatching webhooks.
|
|
|
|
"""
|
2021-08-30 19:55:02 +02:00
|
|
|
invoice_paid_queue = asyncio.Queue(5)
|
2022-10-04 09:51:47 +02:00
|
|
|
# we register invoice_paid_queue to receive all incoming invoices
|
|
|
|
register_invoice_listener(invoice_paid_queue, "core/tasks.py")
|
|
|
|
# register a worker that will react to invoices
|
2021-08-30 19:55:02 +02:00
|
|
|
asyncio.create_task(wait_for_paid_invoices(invoice_paid_queue))
|
2020-10-06 06:50:55 +02:00
|
|
|
|
|
|
|
|
2021-08-30 19:55:02 +02:00
|
|
|
async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue):
|
2022-10-04 09:51:47 +02:00
|
|
|
"""
|
|
|
|
This worker dispatches events to all extensions, dispatches webhooks and balance notifys.
|
|
|
|
"""
|
2021-08-30 19:55:02 +02:00
|
|
|
while True:
|
|
|
|
payment = await invoice_paid_queue.get()
|
2022-10-04 09:51:47 +02:00
|
|
|
logger.trace("received invoice paid event")
|
2020-12-18 20:21:48 +01:00
|
|
|
# send information to sse channel
|
2022-10-04 09:51:47 +02:00
|
|
|
await dispatch_api_invoice_listeners(payment)
|
2020-12-18 20:21:48 +01:00
|
|
|
|
|
|
|
# dispatch webhook
|
2020-12-24 13:38:35 +01:00
|
|
|
if payment.webhook and not payment.webhook_status:
|
|
|
|
await dispatch_webhook(payment)
|
|
|
|
|
2021-04-17 23:27:15 +02:00
|
|
|
# dispatch balance_notify
|
|
|
|
url = await get_balance_notify(payment.wallet_id)
|
|
|
|
if url:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
2021-10-17 19:33:29 +02:00
|
|
|
r = await client.post(url, timeout=4)
|
2021-04-17 23:27:15 +02:00
|
|
|
await mark_webhook_sent(payment, r.status_code)
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
pass
|
|
|
|
|
2020-12-24 13:38:35 +01:00
|
|
|
|
2022-10-04 09:51:47 +02:00
|
|
|
async def dispatch_api_invoice_listeners(payment: Payment):
|
|
|
|
"""
|
|
|
|
Emits events to invoice listener subscribed from the API.
|
|
|
|
"""
|
|
|
|
for chan_name, send_channel in api_invoice_listeners.items():
|
2020-12-24 13:38:35 +01:00
|
|
|
try:
|
2022-10-04 09:51:47 +02:00
|
|
|
logger.debug(f"sending invoice paid event to {chan_name}")
|
2021-08-30 19:55:02 +02:00
|
|
|
send_channel.put_nowait(payment)
|
|
|
|
except asyncio.QueueFull:
|
2022-10-04 09:51:47 +02:00
|
|
|
logger.error(f"removing sse listener {send_channel}:{chan_name}")
|
|
|
|
api_invoice_listeners.pop(chan_name)
|
2020-12-24 13:38:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def dispatch_webhook(payment: Payment):
|
2022-10-04 09:51:47 +02:00
|
|
|
"""
|
|
|
|
Dispatches the webhook to the webhook url.
|
|
|
|
"""
|
2020-12-24 13:38:35 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
2022-01-05 10:48:26 +01:00
|
|
|
data = payment.dict()
|
2020-12-24 13:38:35 +01:00
|
|
|
try:
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.debug("sending webhook", payment.webhook)
|
2022-07-27 09:39:52 +02:00
|
|
|
r = await client.post(payment.webhook, json=data, timeout=40) # type: ignore
|
2020-12-24 13:38:35 +01:00
|
|
|
await mark_webhook_sent(payment, r.status_code)
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
await mark_webhook_sent(payment, -1)
|
|
|
|
|
|
|
|
|
|
|
|
async def mark_webhook_sent(payment: Payment, status: int) -> None:
|
|
|
|
await db.execute(
|
|
|
|
"""
|
2021-07-03 20:39:58 +02:00
|
|
|
UPDATE apipayments SET webhook_status = ?
|
2020-12-24 13:38:35 +01:00
|
|
|
WHERE hash = ?
|
|
|
|
""",
|
|
|
|
(status, payment.payment_hash),
|
|
|
|
)
|