feat: block access to deactivated client

This commit is contained in:
Vlad Stan 2023-02-06 17:42:27 +02:00
parent dedcf823bd
commit f56e9e2e56
3 changed files with 36 additions and 6 deletions

View file

@ -1,7 +1,7 @@
from http import HTTPStatus
from typing import List, Optional
from fastapi import Depends, Query, WebSocket
from fastapi import Depends, WebSocket
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
from loguru import logger
@ -28,12 +28,13 @@ from .crud import (
from .models import NostrRelay
client_manager = NostrClientManager()
active_relays: List[str] = []
@nostrrelay_ext.websocket("/{relay_id}")
async def websocket_endpoint(relay_id: str, websocket: WebSocket):
client = NostrClientConnection(relay_id=relay_id, websocket=websocket)
client_manager.add_client(client)
if not (await client_manager.add_client(client)):
return
try:
await client.start()
except Exception as e:
@ -41,6 +42,7 @@ async def websocket_endpoint(relay_id: str, websocket: WebSocket):
client_manager.remove_client(client)
@nostrrelay_ext.get("/{relay_id}", status_code=HTTPStatus.OK)
async def api_nostrrelay_info(relay_id: str):
relay = await get_public_relay(relay_id)
@ -93,6 +95,7 @@ async def api_update_relay(relay_id: str, data: NostrRelay, wallet: WalletTypeIn
)
updated_relay = NostrRelay.parse_obj({**dict(relay), **dict(data)})
updated_relay = await update_relay(wallet.wallet.user, updated_relay)
await client_manager.toggle_relay(relay_id, updated_relay.active)
return updated_relay
except HTTPException as ex: