diff --git a/client_manager.py b/client_manager.py index 1c4cc53..8b4ac2c 100644 --- a/client_manager.py +++ b/client_manager.py @@ -16,7 +16,7 @@ from .crud import ( mark_events_deleted, prune_old_events, ) -from .models import ClientConfig, NostrEvent, NostrEventType, NostrFilter, RelayConfig +from .models import NostrEvent, NostrEventType, NostrFilter, RelaySpec class NostrClientManager: @@ -48,7 +48,7 @@ class NostrClientManager: self._active_relays = await get_config_for_all_active_relays() self._is_ready = True - async def enable_relay(self, relay_id: str, config: RelayConfig): + async def enable_relay(self, relay_id: str, config: RelaySpec): self._is_ready = True self._active_relays[relay_id] = config @@ -57,7 +57,7 @@ class NostrClientManager: if relay_id in self._active_relays: del self._active_relays[relay_id] - def get_relay_config(self, relay_id: str) -> RelayConfig: + def get_relay_config(self, relay_id: str) -> RelaySpec: return self._active_relays[relay_id] def clients(self, relay_id: str) -> List["NostrClientConnection"]: @@ -80,7 +80,7 @@ class NostrClientManager: def _set_client_callbacks(self, client): setattr(client, "broadcast_event", self.broadcast_event) - def get_client_config() -> ClientConfig: + def get_client_config() -> RelaySpec: return self.get_relay_config(client.relay_id) setattr(client, "get_client_config", get_client_config) @@ -94,7 +94,7 @@ class NostrClientConnection: self.broadcast_event: Optional[ Callable[[NostrClientConnection, NostrEvent], Awaitable[None]] ] = None - self.get_client_config: Optional[Callable[[], ClientConfig]] = None + self.get_client_config: Optional[Callable[[], RelaySpec]] = None self._last_event_timestamp = 0 # in seconds self._event_count_per_timestamp = 0 @@ -189,7 +189,7 @@ class NostrClientConnection: await self._send_msg(resp_nip20) @property - def client_config(self) -> ClientConfig: + def client_config(self) -> RelaySpec: if not self.get_client_config: raise Exception("Client not ready!") return self.get_client_config() diff --git a/crud.py b/crud.py index 70d5dfa..8689a12 100644 --- a/crud.py +++ b/crud.py @@ -2,7 +2,7 @@ import json from typing import Any, List, Optional, Tuple from . import db -from .models import NostrEvent, NostrFilter, NostrRelay, RelayConfig +from .models import NostrEvent, NostrFilter, NostrRelay, RelaySpec ########################## RELAYS #################### @@ -77,7 +77,7 @@ async def get_config_for_all_active_relays() -> dict: ) active_relay_configs = {} for r in rows: - active_relay_configs[r["id"]] = RelayConfig( + active_relay_configs[r["id"]] = RelaySpec( **json.loads(r["meta"]) ) # todo: from_json diff --git a/models.py b/models.py index 0eb4404..51144b3 100644 --- a/models.py +++ b/models.py @@ -8,9 +8,11 @@ from pydantic import BaseModel, Field from secp256k1 import PublicKey -class ClientConfig(BaseModel): +class FilterSpec(BaseModel): max_client_filters = Field(0, alias="maxClientFilters") limit_per_filter = Field(1000, alias="limitPerFilter") + +class EventSpec(BaseModel): max_events_per_second = Field(0, alias="maxEventsPerSecond") created_at_days_past = Field(0, alias="createdAtDaysPast") @@ -23,21 +25,6 @@ class ClientConfig(BaseModel): created_at_minutes_future = Field(0, alias="createdAtMinutesFuture") created_at_seconds_future = Field(0, alias="createdAtSecondsFuture") - is_paid_relay = Field(False, alias="isPaidRelay") - free_storage_value = Field(1, alias="freeStorageValue") - free_storage_unit = Field("MB", alias="freeStorageUnit") - full_storage_action = Field("prune", alias="fullStorageAction") - - allowed_public_keys = Field([], alias="allowedPublicKeys") - blocked_public_keys = Field([], alias="blockedPublicKeys") - - def is_author_allowed(self, p: str) -> bool: - if p in self.blocked_public_keys: - return False - if len(self.allowed_public_keys) == 0: - return True - # todo: check payment - return p in self.allowed_public_keys @property def created_at_in_past(self) -> int: @@ -57,6 +44,11 @@ class ClientConfig(BaseModel): + self.created_at_seconds_future ) +class StorageSpec(BaseModel): + free_storage_value = Field(1, alias="freeStorageValue") + free_storage_unit = Field("MB", alias="freeStorageUnit") + full_storage_action = Field("prune", alias="fullStorageAction") + @property def free_storage_bytes_value(self): value = self.free_storage_value * 1024 @@ -64,11 +56,20 @@ class ClientConfig(BaseModel): value *= 1024 return value - class Config: - allow_population_by_field_name = True +class AuthorSpec(BaseModel): + allowed_public_keys = Field([], alias="allowedPublicKeys") + blocked_public_keys = Field([], alias="blockedPublicKeys") + def is_author_allowed(self, p: str) -> bool: + if p in self.blocked_public_keys: + return False + if len(self.allowed_public_keys) == 0: + return True + # todo: check payment + return p in self.allowed_public_keys -class RelayConfig(ClientConfig): +class PaymentSpec(BaseModel): + is_paid_relay = Field(False, alias="isPaidRelay") wallet = Field("") cost_to_join = Field(0, alias="costToJoin") free_storage = Field(0, alias="freeStorage") @@ -76,6 +77,10 @@ class RelayConfig(ClientConfig): storage_cost_value = Field(0, alias="storageCostValue") storage_cost_unit = Field("MB", alias="storageCostUnit") +class RelaySpec(FilterSpec, EventSpec, StorageSpec, AuthorSpec, PaymentSpec): + class Config: + allow_population_by_field_name = True + class NostrRelay(BaseModel): id: str @@ -85,12 +90,12 @@ class NostrRelay(BaseModel): contact: Optional[str] active: bool = False - config: "RelayConfig" = RelayConfig() + config: "RelaySpec" = RelaySpec() @classmethod def from_row(cls, row: Row) -> "NostrRelay": relay = cls(**dict(row)) - relay.config = RelayConfig(**json.loads(row["meta"])) + relay.config = RelaySpec(**json.loads(row["meta"])) return relay @classmethod diff --git a/templates/nostrrelay/public.html b/templates/nostrrelay/public.html index 5e349ee..e5e5cea 100644 --- a/templates/nostrrelay/public.html +++ b/templates/nostrrelay/public.html @@ -1,4 +1,4 @@ -{% extends "public.html" %} {% block toolbar_title %} {{ nostrrelay.name }} +{% extends "public.html" %} {% block toolbar_title %} {{ relay.name }}