lnbits-legend/lnbits/extensions/lnticket/views_api.py

197 lines
5.7 KiB
Python
Raw Normal View History

import re
from quart import g, jsonify, request
from http import HTTPStatus
2021-08-22 11:31:04 +01:00
from fastapi import FastAPI, Query
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from pydantic import BaseModel
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 . import lnticket_ext
from .crud import (
create_ticket,
set_ticket_paid,
get_ticket,
get_tickets,
delete_ticket,
create_form,
update_form,
get_form,
get_forms,
delete_form,
)
# FORMS
2021-08-22 11:31:04 +01:00
@lnticket_ext.get("/api/v1/forms")
@api_check_wallet_key("invoice")
async def api_forms():
wallet_ids = [g.wallet.id]
if "all_wallets" in request.args:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return (
2021-08-22 11:31:04 +01:00
[form._asdict() for form in await get_forms(wallet_ids)],
HTTPStatus.OK,
)
2021-08-22 11:31:04 +01:00
class CreateData(BaseModel):
wallet: str = Query(...)
name: str = Query(...)
webhook: str = Query(None)
description: str = Query(..., min_length=0)
amount: int = Query(..., ge=0)
flatrate: int = Query(...)
2021-08-22 11:31:04 +01:00
@lnticket_ext.post("/api/v1/forms")
@lnticket_ext.put("/api/v1/forms/{form_id}")
@api_check_wallet_key("invoice")
2021-08-22 11:31:04 +01:00
# @api_validate_post_request(
# schema={
# "wallet": {"type": "string", "empty": False, "required": True},
# "name": {"type": "string", "empty": False, "required": True},
# "webhook": {"type": "string", "required": False},
# "description": {"type": "string", "min": 0, "required": True},
# "amount": {"type": "integer", "min": 0, "required": True},
# "flatrate": {"type": "integer", "required": True},
# }
# )
async def api_form_create(data: CreateData, form_id=None):
if form_id:
form = await get_form(form_id)
if not form:
2021-08-22 11:31:04 +01:00
return {"message": "Form does not exist."}, HTTPStatus.NOT_FOUND
if form.wallet != g.wallet.id:
2021-08-22 11:31:04 +01:00
return jsonify{"message": "Not your form."}, HTTPStatus.FORBIDDEN
2021-08-22 11:31:04 +01:00
form = await update_form(form_id, **data)
else:
2021-08-22 11:31:04 +01:00
form = await create_form(**data)
return form._asdict(), HTTPStatus.CREATED
2021-08-22 11:31:04 +01:00
@lnticket_ext.delete("/api/v1/forms/{form_id}")
@api_check_wallet_key("invoice")
async def api_form_delete(form_id):
form = await get_form(form_id)
if not form:
2021-08-22 11:31:04 +01:00
return {"message": "Form does not exist."}, HTTPStatus.NOT_FOUND
if form.wallet != g.wallet.id:
2021-08-22 11:31:04 +01:00
return {"message": "Not your form."}, HTTPStatus.FORBIDDEN
await delete_form(form_id)
return "", HTTPStatus.NO_CONTENT
#########tickets##########
2021-08-22 11:31:04 +01:00
@lnticket_ext.get("/api/v1/tickets")
@api_check_wallet_key("invoice")
2021-08-22 11:31:04 +01:00
async def api_tickets(all_wallets: bool = Query(None)):
wallet_ids = [g.wallet.id]
2021-08-22 11:31:04 +01:00
if all_wallets:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return (
2021-08-22 11:31:04 +01:00
[form._asdict() for form in await get_tickets(wallet_ids)],
HTTPStatus.OK,
)
2021-08-22 11:31:04 +01:00
class CreateTicketData(BaseModel):
form: str = Query(...)
name: str = Query(...)
email: str = Query("")
ltext: str = Query(...)
sats: int = Query(..., ge=0)
@lnticket_ext.post("/api/v1/tickets/{form_id}")
# @api_validate_post_request(
# schema={
# "form": {"type": "string", "empty": False, "required": True},
# "name": {"type": "string", "empty": False, "required": True},
# "email": {"type": "string", "empty": True, "required": True},
# "ltext": {"type": "string", "empty": False, "required": True},
# "sats": {"type": "integer", "min": 0, "required": True},
# }
# )
async def api_ticket_make_ticket(data: CreateTicketData, form_id):
form = await get_form(form_id)
if not form:
2021-08-22 11:31:04 +01:00
return {"message": "LNTicket does not exist."}, HTTPStatus.NOT_FOUND
2021-08-22 11:31:04 +01:00
nwords = len(re.split(r"\s+", data["ltext"]))
sats = data["sats"]
try:
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"},
)
except Exception as e:
2021-08-22 11:31:04 +01:00
return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
ticket = await create_ticket(
2021-08-22 11:31:04 +01:00
payment_hash=payment_hash, wallet=form.wallet, **data
)
if not ticket:
return (
2021-08-22 11:31:04 +01:00
{"message": "LNTicket could not be fetched."},
HTTPStatus.NOT_FOUND,
)
2021-08-22 11:31:04 +01:00
return
{"payment_hash": payment_hash, "payment_request": payment_request},
HTTPStatus.OK
2021-08-22 11:31:04 +01:00
@lnticket_ext.get("/api/v1/tickets/{payment_hash}")
async def api_ticket_send_ticket(payment_hash):
ticket = await get_ticket(payment_hash)
try:
status = await check_invoice_status(ticket.wallet, payment_hash)
is_paid = not status.pending
except Exception:
2021-08-22 11:31:04 +01:00
return {"paid": False}, HTTPStatus.OK
if is_paid:
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)
2021-08-22 11:31:04 +01:00
return {"paid": True}, HTTPStatus.OK
2021-08-22 11:31:04 +01:00
return {"paid": False}, HTTPStatus.OK
2021-08-22 11:31:04 +01:00
@lnticket_ext.delete("/api/v1/tickets/{ticket_id}")
@api_check_wallet_key("invoice")
async def api_ticket_delete(ticket_id):
ticket = await get_ticket(ticket_id)
if not ticket:
2021-08-22 11:31:04 +01:00
return {"message": "Paywall does not exist."}, HTTPStatus.NOT_FOUND
if ticket.wallet != g.wallet.id:
2021-08-22 11:31:04 +01:00
return {"message": "Not your ticket."}, HTTPStatus.FORBIDDEN
await delete_ticket(ticket_id)
return "", HTTPStatus.NO_CONTENT