Replaces the previous "one row, N seats via extra.quantity" model
with proper one-row-per-attendee semantics. Each attendee gets a
unique scannable id; the door PUT /register/{ticket_id} marks
them registered independently — so a buyer can purchase 3 tickets,
hand 2 QRs to friends arriving separately, and each attendee can
enter on their own schedule.
Schema (migrations_fork.py m002):
- ticket.payment_hash: new TEXT column shared across all rows of
a multi-ticket purchase. Backfilled `payment_hash = id` for
pre-migration rows (id WAS the payment_hash by invariant).
Wire:
- TicketPaymentRequest grows `ticket_ids: list[str]` so the
webapp gets every scannable id back in the create response.
- POST /tickets/{event_id}/{payment_hash} polling endpoint now
reports `ticket_ids` (every row) + keeps `ticket_id` for
back-compat.
- api_ticket_create loops quantity times; the first row reuses
payment_hash as id (preserves legacy `id == payment_hash`
invariant for single-ticket purchases), the rest get
urlsafe_short_hash() uuids.
Payment flow:
- on_invoice_paid fetches all rows by payment_hash and marks each
paid via set_ticket_paid, which now increments event.sold by 1
per row (was N per row via extra.quantity — simpler now). The
per-event asyncio lock still serializes counter + republish so
concurrent multi-ticket purchases for the same event don't
reorder the published Nostr state.
- Each paid row triggers its own send_ticket_notification_in_
background call — no-op for buyers without nostr_identifier /
email, useful when the buyer set those on the row.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
68 lines
2.5 KiB
Python
68 lines
2.5 KiB
Python
import asyncio
|
|
|
|
from lnbits.core.models import Payment
|
|
from lnbits.tasks import register_invoice_listener
|
|
from loguru import logger
|
|
|
|
from .crud import get_ticket, get_tickets_by_payment_hash
|
|
from .models import Ticket
|
|
from .services import send_ticket_notification_in_background, set_ticket_paid
|
|
|
|
payment_listeners: dict[str, list[asyncio.Queue[Ticket]]] = {}
|
|
|
|
|
|
def register_payment_listener(payment_hash, queue: asyncio.Queue[Ticket]) -> None:
|
|
if payment_hash not in payment_listeners:
|
|
payment_listeners[payment_hash] = []
|
|
payment_listeners[payment_hash].append(queue)
|
|
|
|
|
|
def deregister_payment_listener(payment_hash, queue: asyncio.Queue[Ticket]) -> None:
|
|
if payment_hash in payment_listeners:
|
|
payment_listeners[payment_hash].remove(queue)
|
|
if not payment_listeners[payment_hash]:
|
|
del payment_listeners[payment_hash]
|
|
|
|
|
|
async def wait_for_paid_invoices():
|
|
invoice_queue = asyncio.Queue()
|
|
register_invoice_listener(invoice_queue, "ext_events")
|
|
|
|
while True:
|
|
payment = await invoice_queue.get()
|
|
await on_invoice_paid(payment)
|
|
|
|
|
|
async def on_invoice_paid(payment: Payment) -> None:
|
|
if not payment.extra or "events" != payment.extra.get("tag"):
|
|
return
|
|
|
|
# Multi-ticket purchases land as N rows sharing this payment_hash;
|
|
# each one needs to be marked paid + counted against capacity, and
|
|
# each gets its own buyer notification (mostly a no-op when all
|
|
# rows are owned by the same buyer, but cheap and consistent).
|
|
tickets = await get_tickets_by_payment_hash(payment.payment_hash)
|
|
if not tickets:
|
|
# Backstop for any legacy row created before the payment_hash
|
|
# column was populated by the migration backfill.
|
|
legacy = await get_ticket(payment.payment_hash)
|
|
if legacy:
|
|
tickets = [legacy]
|
|
|
|
if not tickets:
|
|
logger.warning(f"No tickets for payment {payment.payment_hash}.")
|
|
return
|
|
|
|
paid_tickets: list[Ticket] = []
|
|
for ticket in tickets:
|
|
paid_tickets.append(await set_ticket_paid(ticket))
|
|
|
|
for paid_ticket in paid_tickets:
|
|
send_ticket_notification_in_background(paid_ticket)
|
|
|
|
# Wake up the WebSocket / poll listeners. Forward the first paid
|
|
# ticket so the existing single-ticket subscribers still work; the
|
|
# webapp re-fetches all ids via the polling endpoint anyway.
|
|
if payment_listeners.get(payment.payment_hash):
|
|
for paid_ticket_queue in payment_listeners[payment.payment_hash]:
|
|
paid_ticket_queue.put_nowait(paid_tickets[0])
|