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

37 lines
1 KiB
Python
Raw Normal View History

2023-01-09 09:41:19 +01:00
import asyncio
from loguru import logger
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
2023-01-11 19:21:13 +01:00
from .crud import get_email_by_payment_hash, get_emailaddress, set_email_paid
2023-01-09 09:41:19 +01:00
from .smtp import send_mail
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
register_invoice_listener(invoice_queue)
while True:
payment = await invoice_queue.get()
await on_invoice_paid(payment)
async def on_invoice_paid(payment: Payment) -> None:
2023-01-09 09:54:17 +01:00
if payment.extra.get("tag") != "smtp":
2023-01-09 09:41:19 +01:00
return
2023-01-11 19:21:13 +01:00
email = await get_email_by_payment_hash(payment.checking_id)
2023-01-09 09:41:19 +01:00
if not email:
logger.error("SMTP: email can not by fetched")
return
emailaddress = await get_emailaddress(email.emailaddress_id)
if not emailaddress:
logger.error("SMTP: emailaddress can not by fetched")
return
await payment.set_pending(False)
await send_mail(emailaddress, email)
await set_email_paid(payment_hash=payment.payment_hash)