refactoring: small

This commit is contained in:
Vlad Stan 2023-02-08 10:47:43 +02:00
parent 30dfb03d2d
commit e1fe19b115
2 changed files with 20 additions and 12 deletions

View file

@ -25,7 +25,7 @@ class NostrClientManager:
if not self._is_ready:
await self.init_relays()
allow_connect = await self._allow_client(client.relay_id, client.websocket)
allow_connect = await self._allow_client(client)
if not allow_connect:
return False
setattr(client, "broadcast_event", self.broadcast_event)
@ -64,10 +64,11 @@ class NostrClientManager:
self._clients[relay_id] = []
return self._clients[relay_id]
async def _allow_client(self, relay_id:str, websocket: WebSocket) -> bool:
if relay_id not in self._active_relays:
await websocket.close(reason=f"Relay '{relay_id}' is not active")
async def _allow_client(self, c: "NostrClientConnection") -> bool:
if c.relay_id not in self._active_relays:
await c.stop(reason=f"Relay '{c.relay_id}' is not active")
return False
#todo: NIP-42: AUTH
return True
class NostrClientConnection:
@ -93,10 +94,14 @@ class NostrClientConnection:
logger.warning(e)
async def stop(self, reason: Optional[str]):
message = reason if reason else "Server closed webocket"
try:
message = reason if reason else "Server closed webocket"
await self.websocket.send_text(json.dumps(["NOTICE", message]))
await self.websocket.close()
except:
pass
try:
await self.websocket.close(reason=reason)
except:
pass

View file

@ -8,18 +8,21 @@ from pydantic import BaseModel, Field
from secp256k1 import PublicKey
class RelayConfig(BaseModel):
is_paid_relay = Field(False, alias="isPaidRelay")
wallet = Field("")
cost_to_join = Field(0, alias="costToJoin")
free_storage = Field(0, alias="freeStorage")
storage_cost_per_kb = Field(0, alias="storageCostPerKb")
class ClientConfig(BaseModel):
max_client_filters = Field(0, alias="maxClientFilters")
allowed_public_keys = Field([], alias="allowedPublicKeys")
blocked_public_keys = Field([], alias="blockedPublicKeys")
class Config:
allow_population_by_field_name = True
class RelayConfig(ClientConfig):
is_paid_relay = Field(False, alias="isPaidRelay")
wallet = Field("")
cost_to_join = Field(0, alias="costToJoin")
free_storage = Field(0, alias="freeStorage")
storage_cost_per_kb = Field(0, alias="storageCostPerKb")
class NostrRelay(BaseModel):
id: str