lnbits-legend/lnbits/extensions/copilot/tasks.py

89 lines
2.9 KiB
Python
Raw Normal View History

2021-04-19 23:45:40 +01:00
import trio # type: ignore
import json
import httpx
2021-06-23 14:05:10 +01:00
from quart import g, jsonify, url_for, websocket
from http import HTTPStatus
2021-04-19 23:45:40 +01:00
from lnbits.core import db as core_db
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
2021-04-19 23:56:52 +01:00
from .crud import get_copilot
2021-04-20 00:37:51 +01:00
from .views import updater
2021-04-20 00:12:44 +01:00
import shortuuid
2021-04-19 23:45:40 +01:00
2021-04-20 08:50:53 +01:00
2021-04-19 23:45:40 +01:00
async def register_listeners():
invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2)
register_invoice_listener(invoice_paid_chan_send)
await wait_for_paid_invoices(invoice_paid_chan_recv)
async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel):
async for payment in invoice_paid_chan:
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
webhook = None
data = None
if "copilot" != payment.extra.get("tag"):
2021-04-20 08:50:53 +01:00
# not an copilot invoice
2021-04-19 23:45:40 +01:00
return
if payment.extra.get("wh_status"):
# this webhook has already been sent
return
copilot = await get_copilot(payment.extra.get("copilot", -1))
if not copilot:
return (
jsonify({"message": "Copilot link link does not exist."}),
HTTPStatus.NOT_FOUND,
)
if copilot.animation1threshold:
2021-04-20 17:47:59 +01:00
if int(payment.amount / 1000) >= copilot.animation1threshold:
data = copilot.animation1
2021-04-19 23:45:40 +01:00
webhook = copilot.animation1webhook
if copilot.animation2threshold:
2021-04-20 17:47:59 +01:00
if int(payment.amount / 1000) >= copilot.animation2threshold:
data = copilot.animation2
2021-04-19 23:45:40 +01:00
webhook = copilot.animation1webhook
if copilot.animation3threshold:
2021-04-20 17:47:59 +01:00
if int(payment.amount / 1000) >= copilot.animation3threshold:
data = copilot.animation3
webhook = copilot.animation1webhook
2021-04-19 23:45:40 +01:00
if webhook:
async with httpx.AsyncClient() as client:
try:
r = await client.post(
webhook,
json={
"payment_hash": payment.payment_hash,
"payment_request": payment.bolt11,
"amount": payment.amount,
"comment": payment.extra.get("comment"),
},
timeout=40,
)
await mark_webhook_sent(payment, r.status_code)
except (httpx.ConnectError, httpx.RequestError):
await mark_webhook_sent(payment, -1)
2021-04-20 17:26:38 +01:00
if payment.extra.get("comment"):
2021-06-24 00:51:26 +01:00
await updater(copilot.id, data, payment.extra.get("comment"))
2021-04-20 17:56:12 +01:00
else:
2021-06-24 00:51:26 +01:00
await updater(copilot.id, data, "none")
2021-04-19 23:45:40 +01:00
2021-06-24 01:26:49 +01:00
2021-04-19 23:45:40 +01:00
async def mark_webhook_sent(payment: Payment, status: int) -> None:
payment.extra["wh_status"] = status
await core_db.execute(
"""
UPDATE apipayments SET extra = ?
WHERE hash = ?
""",
(json.dumps(payment.extra), payment.payment_hash),
)