feat: update to v1.0.0 (#30)
This commit is contained in:
parent
2bdbbb274d
commit
73054fd5ce
20 changed files with 2029 additions and 2132 deletions
223
views_api.py
223
views_api.py
|
|
@ -1,8 +1,7 @@
|
|||
from http import HTTPStatus
|
||||
from typing import List, Optional
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, WebSocket
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, WebSocket
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.core.models import WalletTypeInfo
|
||||
from lnbits.core.services import create_invoice
|
||||
|
|
@ -38,6 +37,7 @@ nostrrelay_api_router = APIRouter()
|
|||
|
||||
|
||||
@nostrrelay_api_router.websocket("/{relay_id}")
|
||||
@nostrrelay_api_router.websocket("/{relay_id}/")
|
||||
async def websocket_endpoint(relay_id: str, websocket: WebSocket):
|
||||
client = NostrClientConnection(relay_id=relay_id, websocket=websocket)
|
||||
client_accepted = await client_manager.add_client(client)
|
||||
|
|
@ -55,26 +55,19 @@ async def websocket_endpoint(relay_id: str, websocket: WebSocket):
|
|||
async def api_create_relay(
|
||||
data: NostrRelay,
|
||||
request: Request,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
key_info: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> NostrRelay:
|
||||
data.user_id = key_info.wallet.user
|
||||
if len(data.id):
|
||||
user = await get_user(wallet.wallet.user)
|
||||
user = await get_user(data.user_id)
|
||||
assert user, "User not found."
|
||||
assert user.admin, "Only admin users can set the relay ID"
|
||||
else:
|
||||
data.id = urlsafe_short_hash()[:8]
|
||||
|
||||
try:
|
||||
data.config.domain = extract_domain(str(request.url))
|
||||
relay = await create_relay(wallet.wallet.user, data)
|
||||
return relay
|
||||
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot create relay",
|
||||
) from ex
|
||||
data.meta.domain = extract_domain(str(request.url))
|
||||
relay = await create_relay(data)
|
||||
return relay
|
||||
|
||||
|
||||
@nostrrelay_api_router.patch("/api/v1/relay/{relay_id}")
|
||||
|
|
@ -87,79 +80,54 @@ async def api_update_relay(
|
|||
detail="Cannot change the relay id",
|
||||
)
|
||||
|
||||
try:
|
||||
relay = await get_relay(wallet.wallet.user, data.id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
updated_relay = NostrRelay.parse_obj({**dict(relay), **dict(data)})
|
||||
updated_relay = await update_relay(wallet.wallet.user, updated_relay)
|
||||
# activate & deactivate have their own endpoint
|
||||
updated_relay.active = relay.active
|
||||
|
||||
if updated_relay.active:
|
||||
await client_manager.enable_relay(relay_id, updated_relay.config)
|
||||
else:
|
||||
await client_manager.disable_relay(relay_id)
|
||||
|
||||
return updated_relay
|
||||
|
||||
except HTTPException as ex:
|
||||
raise ex
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
relay = await get_relay(wallet.wallet.user, data.id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot update relay",
|
||||
) from ex
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
|
||||
updated_relay = NostrRelay.parse_obj({**dict(relay), **dict(data)})
|
||||
updated_relay.user_id = wallet.wallet.user
|
||||
updated_relay = await update_relay(updated_relay)
|
||||
|
||||
# activate & deactivate have their own endpoint
|
||||
updated_relay.active = relay.active
|
||||
|
||||
if updated_relay.active:
|
||||
await client_manager.enable_relay(relay_id, updated_relay.meta)
|
||||
else:
|
||||
await client_manager.disable_relay(relay_id)
|
||||
|
||||
return updated_relay
|
||||
|
||||
|
||||
@nostrrelay_api_router.put("/api/v1/relay/{relay_id}")
|
||||
async def api_toggle_relay(
|
||||
relay_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||
) -> NostrRelay:
|
||||
|
||||
try:
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
relay.active = not relay.active
|
||||
updated_relay = await update_relay(wallet.wallet.user, relay)
|
||||
|
||||
if relay.active:
|
||||
await client_manager.enable_relay(relay_id, relay.config)
|
||||
else:
|
||||
await client_manager.disable_relay(relay_id)
|
||||
|
||||
return updated_relay
|
||||
|
||||
except HTTPException as ex:
|
||||
raise ex
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot update relay",
|
||||
) from ex
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
relay.active = not relay.active
|
||||
await update_relay(relay)
|
||||
|
||||
if relay.active:
|
||||
await client_manager.enable_relay(relay_id, relay.meta)
|
||||
else:
|
||||
await client_manager.disable_relay(relay_id)
|
||||
|
||||
return relay
|
||||
|
||||
|
||||
@nostrrelay_api_router.get("/api/v1/relay")
|
||||
async def api_get_relays(
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> List[NostrRelay]:
|
||||
try:
|
||||
return await get_relays(wallet.wallet.user)
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot fetch relays",
|
||||
) from ex
|
||||
) -> list[NostrRelay]:
|
||||
return await get_relays(wallet.wallet.user)
|
||||
|
||||
|
||||
@nostrrelay_api_router.get("/api/v1/relay-info")
|
||||
|
|
@ -171,14 +139,7 @@ async def api_get_relay_info() -> JSONResponse:
|
|||
async def api_get_relay(
|
||||
relay_id: str, wallet: WalletTypeInfo = Depends(require_invoice_key)
|
||||
) -> Optional[NostrRelay]:
|
||||
try:
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot fetch relay",
|
||||
) from ex
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
|
|
@ -191,39 +152,23 @@ async def api_get_relay(
|
|||
async def api_create_or_update_account(
|
||||
data: NostrPartialAccount,
|
||||
) -> NostrAccount:
|
||||
data.pubkey = normalize_public_key(data.pubkey)
|
||||
account = await get_account(data.relay_id, data.pubkey)
|
||||
if not account:
|
||||
account = NostrAccount(
|
||||
pubkey=data.pubkey,
|
||||
relay_id=data.relay_id,
|
||||
blocked=data.blocked or False,
|
||||
allowed=data.allowed or False,
|
||||
)
|
||||
return await create_account(account)
|
||||
|
||||
try:
|
||||
data.pubkey = normalize_public_key(data.pubkey)
|
||||
if data.blocked is not None:
|
||||
account.blocked = data.blocked
|
||||
if data.allowed is not None:
|
||||
account.allowed = data.allowed
|
||||
|
||||
account = await get_account(data.relay_id, data.pubkey)
|
||||
if not account:
|
||||
account = NostrAccount(
|
||||
pubkey=data.pubkey,
|
||||
blocked=data.blocked or False,
|
||||
allowed=data.allowed or False,
|
||||
)
|
||||
return await create_account(data.relay_id, account)
|
||||
|
||||
if data.blocked is not None:
|
||||
account.blocked = data.blocked
|
||||
if data.allowed is not None:
|
||||
account.allowed = data.allowed
|
||||
|
||||
return await update_account(data.relay_id, account)
|
||||
|
||||
except ValueError as ex:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=str(ex),
|
||||
) from ex
|
||||
except HTTPException as ex:
|
||||
raise ex
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot create account",
|
||||
) from ex
|
||||
return await update_account(account)
|
||||
|
||||
|
||||
@nostrrelay_api_router.delete(
|
||||
|
|
@ -249,30 +194,16 @@ async def api_get_accounts(
|
|||
allowed: bool = False,
|
||||
blocked: bool = True,
|
||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||
) -> List[NostrAccount]:
|
||||
try:
|
||||
# make sure the user has access to the relay
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
accounts = await get_accounts(relay.id, allowed, blocked)
|
||||
return accounts
|
||||
except ValueError as ex:
|
||||
) -> list[NostrAccount]:
|
||||
# make sure the user has access to the relay
|
||||
relay = await get_relay(wallet.wallet.user, relay_id)
|
||||
if not relay:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=str(ex),
|
||||
) from ex
|
||||
except HTTPException as ex:
|
||||
raise ex
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail="Cannot fetch accounts",
|
||||
) from ex
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Relay not found",
|
||||
)
|
||||
accounts = await get_accounts(relay.id, allowed, blocked)
|
||||
return accounts
|
||||
|
||||
|
||||
@nostrrelay_api_router.delete("/api/v1/relay/{relay_id}")
|
||||
|
|
@ -305,9 +236,9 @@ async def api_pay_to_join(data: BuyOrder):
|
|||
if data.action == "join":
|
||||
if relay.is_free_to_join:
|
||||
raise ValueError("Relay is free to join")
|
||||
amount = int(relay.config.cost_to_join)
|
||||
amount = int(relay.meta.cost_to_join)
|
||||
elif data.action == "storage":
|
||||
if relay.config.storage_cost_value == 0:
|
||||
if relay.meta.storage_cost_value == 0:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail="Relay storage cost is zero. Cannot buy!",
|
||||
|
|
@ -317,18 +248,18 @@ async def api_pay_to_join(data: BuyOrder):
|
|||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail="Must specify how much storage to buy!",
|
||||
)
|
||||
storage_to_buy = data.units_to_buy * relay.config.storage_cost_value * 1024
|
||||
if relay.config.storage_cost_unit == "MB":
|
||||
storage_to_buy = data.units_to_buy * relay.meta.storage_cost_value * 1024
|
||||
if relay.meta.storage_cost_unit == "MB":
|
||||
storage_to_buy *= 1024
|
||||
amount = data.units_to_buy * relay.config.storage_cost_value
|
||||
amount = data.units_to_buy * relay.meta.storage_cost_value
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=f"Unknown action: '{data.action}'",
|
||||
)
|
||||
|
||||
_, payment_request = await create_invoice(
|
||||
wallet_id=relay.config.wallet,
|
||||
payment = await create_invoice(
|
||||
wallet_id=relay.meta.wallet,
|
||||
amount=amount,
|
||||
memo=f"Pubkey '{data.pubkey}' wants to join {relay.id}",
|
||||
extra={
|
||||
|
|
@ -339,4 +270,4 @@ async def api_pay_to_join(data: BuyOrder):
|
|||
"storage_to_buy": storage_to_buy,
|
||||
},
|
||||
)
|
||||
return {"invoice": payment_request}
|
||||
return {"invoice": payment.bolt11}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue