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

213 lines
6 KiB
Python
Raw Normal View History

from quart import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user
from lnbits.core.services import create_invoice, check_invoice_status
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from . import subdomains_ext
from .crud import (
create_subdomain,
get_subdomain,
get_subdomains,
delete_subdomain,
create_domain,
update_domain,
get_domain,
get_domains,
delete_domain,
get_subdomainBySubdomain,
)
from .cloudflare import cloudflare_create_subdomain, cloudflare_deletesubdomain
# domainS
2021-08-21 00:34:48 +01:00
@subdomains_ext.get("/api/v1/domains")
@api_check_wallet_key("invoice")
async def api_domains():
wallet_ids = [g.wallet.id]
if "all_wallets" in request.args:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return (
2021-08-21 00:34:48 +01:00
[domain._asdict() for domain in await get_domains(wallet_ids)],
HTTPStatus.OK,
)
2021-08-21 00:34:48 +01:00
class CreateDomainsData(BaseModel):
wallet: str
domain: str
cf_token: str
cf_zone_id: str
webhook: optional[str]
description: str
cost: int
allowed_record_types: str
@subdomains_ext.post("/api/v1/domains")
@subdomains_ext.put("/api/v1/domains/<domain_id>")
@api_check_wallet_key("invoice")
2021-08-21 00:34:48 +01:00
async def api_domain_create(data: CreateDomainsData, domain_id=None):
if domain_id:
domain = await get_domain(domain_id)
if not domain:
2021-08-21 00:34:48 +01:00
return {"message": "domain does not exist."}, HTTPStatus.NOT_FOUND
if domain.wallet != g.wallet.id:
2021-08-21 00:34:48 +01:00
return {"message": "Not your domain."}, HTTPStatus.FORBIDDEN
2021-08-21 00:34:48 +01:00
domain = await update_domain(domain_id, **data)
else:
2021-08-21 00:34:48 +01:00
domain = await create_domain(**data)
return jsonify(domain._asdict()), HTTPStatus.CREATED
2021-08-21 00:34:48 +01:00
@subdomains_ext.delete("/api/v1/domains/<domain_id>")
@api_check_wallet_key("invoice")
async def api_domain_delete(domain_id):
domain = await get_domain(domain_id)
if not domain:
2021-08-21 00:34:48 +01:00
return {"message": "domain does not exist."}, HTTPStatus.NOT_FOUND
if domain.wallet != g.wallet.id:
2021-08-21 00:34:48 +01:00
return {"message": "Not your domain."}, HTTPStatus.FORBIDDEN
await delete_domain(domain_id)
return "", HTTPStatus.NO_CONTENT
#########subdomains##########
2021-08-21 00:34:48 +01:00
@subdomains_ext.get("/api/v1/subdomains")
@api_check_wallet_key("invoice")
async def api_subdomains():
wallet_ids = [g.wallet.id]
if "all_wallets" in request.args:
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return (
2021-08-21 00:34:48 +01:00
[domain._asdict() for domain in await get_subdomains(wallet_ids)],
HTTPStatus.OK,
)
2021-08-21 00:34:48 +01:00
class CreateDomainsData(BaseModel):
domain: str
subdomain: str
email: str
ip: str
sats: int = Query(ge=0)
duration: int
record_type: str
2021-08-21 00:34:48 +01:00
@subdomains_ext.post("/api/v1/subdomains/<domain_id>")
async def api_subdomain_make_subdomain(data: CreateDomainsData, domain_id):
domain = await get_domain(domain_id)
# If the request is coming for the non-existant domain
if not domain:
return jsonify({"message": "LNsubdomain does not exist."}), HTTPStatus.NOT_FOUND
## If record_type is not one of the allowed ones reject the request
2021-08-21 00:34:48 +01:00
if data.record_type not in domain.allowed_record_types:
return ({"message": data.record_type + "Not a valid record"},
HTTPStatus.BAD_REQUEST,
)
## If domain already exist in our database reject it
2021-08-21 00:34:48 +01:00
if await get_subdomainBySubdomain(data.subdomain) is not None:
return (
{
2021-08-21 00:34:48 +01:00
"message": data.subdomain
+ "."
+ domain.domain
+ " domain already taken"
2021-08-21 00:34:48 +01:00
},
HTTPStatus.BAD_REQUEST,
)
## Dry run cloudflare... (create and if create is sucessful delete it)
cf_response = await cloudflare_create_subdomain(
domain=domain,
2021-08-21 00:34:48 +01:00
subdomain=data.subdomain,
record_type=data.record_type,
ip=data.ip,
)
if cf_response["success"] == True:
cloudflare_deletesubdomain(domain=domain, domain_id=cf_response["result"]["id"])
else:
return (
{
"message": "Problem with cloudflare: "
+ cf_response["errors"][0]["message"]
2021-08-21 00:34:48 +01:00
},
HTTPStatus.BAD_REQUEST,
)
## ALL OK - create an invoice and return it to the user
2021-08-21 00:34:48 +01:00
sats = data.sats
try:
payment_hash, payment_request = await create_invoice(
wallet_id=domain.wallet,
amount=sats,
2021-08-21 00:34:48 +01:00
memo=f"subdomain {data.subdomain}.{domain.domain} for {sats} sats for {data.duration} days",
extra={"tag": "lnsubdomain"},
)
except Exception as e:
2021-08-21 00:34:48 +01:00
return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
subdomain = await create_subdomain(
2021-08-21 00:34:48 +01:00
payment_hash=payment_hash, wallet=domain.wallet, **data
)
if not subdomain:
return (
2021-08-21 00:34:48 +01:00
{"message": "LNsubdomain could not be fetched."},
HTTPStatus.NOT_FOUND,
)
return (
2021-08-21 00:34:48 +01:00
{"payment_hash": payment_hash, "payment_request": payment_request},
HTTPStatus.OK,
)
2021-08-21 00:34:48 +01:00
@subdomains_ext.get("/api/v1/subdomains/<payment_hash>")
async def api_subdomain_send_subdomain(payment_hash):
subdomain = await get_subdomain(payment_hash)
try:
status = await check_invoice_status(subdomain.wallet, payment_hash)
is_paid = not status.pending
except Exception:
2021-08-21 00:34:48 +01:00
return {"paid": False}, HTTPStatus.OK
if is_paid:
2021-08-21 00:34:48 +01:00
return {"paid": True}, HTTPStatus.OK
2021-08-21 00:34:48 +01:00
return {"paid": False}, HTTPStatus.OK
2021-08-21 00:34:48 +01:00
@subdomains_ext.delete("/api/v1/subdomains/<subdomain_id>")
@api_check_wallet_key("invoice")
async def api_subdomain_delete(subdomain_id):
subdomain = await get_subdomain(subdomain_id)
if not subdomain:
2021-08-21 00:34:48 +01:00
return {"message": "Paywall does not exist."}, HTTPStatus.NOT_FOUND
if subdomain.wallet != g.wallet.id:
2021-08-21 00:34:48 +01:00
return {"message": "Not your subdomain."}, HTTPStatus.FORBIDDEN
await delete_subdomain(subdomain_id)
return "", HTTPStatus.NO_CONTENT