migrate to sqlalchemy-aio.
a big refactor that:
- fixes some issues that might have happened (or not) with asynchronous
reactions to payments;
- paves the way to https://github.com/lnbits/lnbits/issues/121;
- uses more async/await notation which just looks nice; and
- makes it simple(r?) for one extension to modify stuff from other extensions.
This commit is contained in:
@@ -6,10 +6,10 @@ from lnbits.core.crud import get_user, get_wallet
|
||||
from lnbits.core.services import create_invoice, check_invoice_status
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
|
||||
from lnbits.extensions.lnticket import lnticket_ext
|
||||
from . import lnticket_ext
|
||||
from .crud import (
|
||||
create_ticket,
|
||||
update_ticket,
|
||||
set_ticket_paid,
|
||||
get_ticket,
|
||||
get_tickets,
|
||||
delete_ticket,
|
||||
@@ -21,7 +21,7 @@ from .crud import (
|
||||
)
|
||||
|
||||
|
||||
#########FORMS##########
|
||||
# FORMS
|
||||
|
||||
|
||||
@lnticket_ext.route("/api/v1/forms", methods=["GET"])
|
||||
@@ -30,9 +30,9 @@ async def api_forms():
|
||||
wallet_ids = [g.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
wallet_ids = get_user(g.wallet.user).wallet_ids
|
||||
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
||||
|
||||
return jsonify([form._asdict() for form in get_forms(wallet_ids)]), HTTPStatus.OK
|
||||
return jsonify([form._asdict() for form in await get_forms(wallet_ids)]), HTTPStatus.OK
|
||||
|
||||
|
||||
@lnticket_ext.route("/api/v1/forms", methods=["POST"])
|
||||
@@ -48,7 +48,7 @@ async def api_forms():
|
||||
)
|
||||
async def api_form_create(form_id=None):
|
||||
if form_id:
|
||||
form = get_form(form_id)
|
||||
form = await get_form(form_id)
|
||||
|
||||
if not form:
|
||||
return jsonify({"message": "Form does not exist."}), HTTPStatus.NOT_FOUND
|
||||
@@ -56,16 +56,16 @@ async def api_form_create(form_id=None):
|
||||
if form.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your form."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
form = update_form(form_id, **g.data)
|
||||
form = await update_form(form_id, **g.data)
|
||||
else:
|
||||
form = create_form(**g.data)
|
||||
form = await create_form(**g.data)
|
||||
return jsonify(form._asdict()), HTTPStatus.CREATED
|
||||
|
||||
|
||||
@lnticket_ext.route("/api/v1/forms/<form_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_form_delete(form_id):
|
||||
form = get_form(form_id)
|
||||
form = await get_form(form_id)
|
||||
|
||||
if not form:
|
||||
return jsonify({"message": "Form does not exist."}), HTTPStatus.NOT_FOUND
|
||||
@@ -73,7 +73,7 @@ async def api_form_delete(form_id):
|
||||
if form.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your form."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
delete_form(form_id)
|
||||
await delete_form(form_id)
|
||||
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
@@ -87,9 +87,9 @@ async def api_tickets():
|
||||
wallet_ids = [g.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
wallet_ids = get_user(g.wallet.user).wallet_ids
|
||||
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
||||
|
||||
return jsonify([form._asdict() for form in get_tickets(wallet_ids)]), HTTPStatus.OK
|
||||
return jsonify([form._asdict() for form in await get_tickets(wallet_ids)]), HTTPStatus.OK
|
||||
|
||||
|
||||
@lnticket_ext.route("/api/v1/tickets/<form_id>", methods=["POST"])
|
||||
@@ -102,22 +102,17 @@ async def api_tickets():
|
||||
}
|
||||
)
|
||||
async def api_ticket_make_ticket(form_id):
|
||||
form = get_form(form_id)
|
||||
form = await get_form(form_id)
|
||||
if not form:
|
||||
return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
|
||||
try:
|
||||
nwords = len(re.split(r"\s+", g.data["ltext"]))
|
||||
sats = nwords * form.costpword
|
||||
payment_hash, payment_request = create_invoice(
|
||||
wallet_id=form.wallet,
|
||||
amount=sats,
|
||||
memo=f"ticket with {nwords} words on {form_id}",
|
||||
extra={"tag": "lnticket"},
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
ticket = create_ticket(payment_hash=payment_hash, wallet=form.wallet, sats=sats, **g.data)
|
||||
nwords = len(re.split(r"\s+", g.data["ltext"]))
|
||||
sats = nwords * form.costpword
|
||||
payment_hash, payment_request = await create_invoice(
|
||||
wallet_id=form.wallet, amount=sats, memo=f"ticket with {nwords} words on {form_id}", extra={"tag": "lnticket"},
|
||||
)
|
||||
|
||||
ticket = await create_ticket(payment_hash=payment_hash, wallet=form.wallet, sats=sats, **g.data)
|
||||
|
||||
if not ticket:
|
||||
return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND
|
||||
@@ -127,17 +122,18 @@ async def api_ticket_make_ticket(form_id):
|
||||
|
||||
@lnticket_ext.route("/api/v1/tickets/<payment_hash>", methods=["GET"])
|
||||
async def api_ticket_send_ticket(payment_hash):
|
||||
ticket = get_ticket(payment_hash)
|
||||
ticket = await get_ticket(payment_hash)
|
||||
try:
|
||||
is_paid = not check_invoice_status(ticket.wallet, payment_hash).pending
|
||||
status = await check_invoice_status(ticket.wallet, payment_hash)
|
||||
is_paid = not status.pending
|
||||
except Exception:
|
||||
return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
if is_paid:
|
||||
wallet = get_wallet(ticket.wallet)
|
||||
payment = wallet.get_payment(payment_hash)
|
||||
payment.set_pending(False)
|
||||
ticket = update_ticket(paid=True, payment_hash=payment_hash)
|
||||
wallet = await get_wallet(ticket.wallet)
|
||||
payment = await wallet.get_payment(payment_hash)
|
||||
await payment.set_pending(False)
|
||||
ticket = await set_ticket_paid(payment_hash=payment_hash)
|
||||
return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK
|
||||
|
||||
return jsonify({"paid": False}), HTTPStatus.OK
|
||||
@@ -146,7 +142,7 @@ async def api_ticket_send_ticket(payment_hash):
|
||||
@lnticket_ext.route("/api/v1/tickets/<ticket_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_ticket_delete(ticket_id):
|
||||
ticket = get_ticket(ticket_id)
|
||||
ticket = await get_ticket(ticket_id)
|
||||
|
||||
if not ticket:
|
||||
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
|
||||
@@ -154,6 +150,6 @@ async def api_ticket_delete(ticket_id):
|
||||
if ticket.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your ticket."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
delete_ticket(ticket_id)
|
||||
await delete_ticket(ticket_id)
|
||||
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
Reference in New Issue
Block a user