refactor: class renamings
This commit is contained in:
parent
0c72f868ed
commit
d5aff47717
5 changed files with 37 additions and 32 deletions
|
|
@ -16,7 +16,7 @@ from .crud import (
|
||||||
mark_events_deleted,
|
mark_events_deleted,
|
||||||
prune_old_events,
|
prune_old_events,
|
||||||
)
|
)
|
||||||
from .models import ClientConfig, NostrEvent, NostrEventType, NostrFilter, RelayConfig
|
from .models import NostrEvent, NostrEventType, NostrFilter, RelaySpec
|
||||||
|
|
||||||
|
|
||||||
class NostrClientManager:
|
class NostrClientManager:
|
||||||
|
|
@ -48,7 +48,7 @@ class NostrClientManager:
|
||||||
self._active_relays = await get_config_for_all_active_relays()
|
self._active_relays = await get_config_for_all_active_relays()
|
||||||
self._is_ready = True
|
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._is_ready = True
|
||||||
self._active_relays[relay_id] = config
|
self._active_relays[relay_id] = config
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ class NostrClientManager:
|
||||||
if relay_id in self._active_relays:
|
if relay_id in self._active_relays:
|
||||||
del self._active_relays[relay_id]
|
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]
|
return self._active_relays[relay_id]
|
||||||
|
|
||||||
def clients(self, relay_id: str) -> List["NostrClientConnection"]:
|
def clients(self, relay_id: str) -> List["NostrClientConnection"]:
|
||||||
|
|
@ -80,7 +80,7 @@ class NostrClientManager:
|
||||||
def _set_client_callbacks(self, client):
|
def _set_client_callbacks(self, client):
|
||||||
setattr(client, "broadcast_event", self.broadcast_event)
|
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)
|
return self.get_relay_config(client.relay_id)
|
||||||
|
|
||||||
setattr(client, "get_client_config", get_client_config)
|
setattr(client, "get_client_config", get_client_config)
|
||||||
|
|
@ -94,7 +94,7 @@ class NostrClientConnection:
|
||||||
self.broadcast_event: Optional[
|
self.broadcast_event: Optional[
|
||||||
Callable[[NostrClientConnection, NostrEvent], Awaitable[None]]
|
Callable[[NostrClientConnection, NostrEvent], Awaitable[None]]
|
||||||
] = 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._last_event_timestamp = 0 # in seconds
|
||||||
self._event_count_per_timestamp = 0
|
self._event_count_per_timestamp = 0
|
||||||
|
|
@ -189,7 +189,7 @@ class NostrClientConnection:
|
||||||
await self._send_msg(resp_nip20)
|
await self._send_msg(resp_nip20)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def client_config(self) -> ClientConfig:
|
def client_config(self) -> RelaySpec:
|
||||||
if not self.get_client_config:
|
if not self.get_client_config:
|
||||||
raise Exception("Client not ready!")
|
raise Exception("Client not ready!")
|
||||||
return self.get_client_config()
|
return self.get_client_config()
|
||||||
|
|
|
||||||
4
crud.py
4
crud.py
|
|
@ -2,7 +2,7 @@ import json
|
||||||
from typing import Any, List, Optional, Tuple
|
from typing import Any, List, Optional, Tuple
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import NostrEvent, NostrFilter, NostrRelay, RelayConfig
|
from .models import NostrEvent, NostrFilter, NostrRelay, RelaySpec
|
||||||
|
|
||||||
########################## RELAYS ####################
|
########################## RELAYS ####################
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ async def get_config_for_all_active_relays() -> dict:
|
||||||
)
|
)
|
||||||
active_relay_configs = {}
|
active_relay_configs = {}
|
||||||
for r in rows:
|
for r in rows:
|
||||||
active_relay_configs[r["id"]] = RelayConfig(
|
active_relay_configs[r["id"]] = RelaySpec(
|
||||||
**json.loads(r["meta"])
|
**json.loads(r["meta"])
|
||||||
) # todo: from_json
|
) # todo: from_json
|
||||||
|
|
||||||
|
|
|
||||||
47
models.py
47
models.py
|
|
@ -8,9 +8,11 @@ from pydantic import BaseModel, Field
|
||||||
from secp256k1 import PublicKey
|
from secp256k1 import PublicKey
|
||||||
|
|
||||||
|
|
||||||
class ClientConfig(BaseModel):
|
class FilterSpec(BaseModel):
|
||||||
max_client_filters = Field(0, alias="maxClientFilters")
|
max_client_filters = Field(0, alias="maxClientFilters")
|
||||||
limit_per_filter = Field(1000, alias="limitPerFilter")
|
limit_per_filter = Field(1000, alias="limitPerFilter")
|
||||||
|
|
||||||
|
class EventSpec(BaseModel):
|
||||||
max_events_per_second = Field(0, alias="maxEventsPerSecond")
|
max_events_per_second = Field(0, alias="maxEventsPerSecond")
|
||||||
|
|
||||||
created_at_days_past = Field(0, alias="createdAtDaysPast")
|
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_minutes_future = Field(0, alias="createdAtMinutesFuture")
|
||||||
created_at_seconds_future = Field(0, alias="createdAtSecondsFuture")
|
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
|
@property
|
||||||
def created_at_in_past(self) -> int:
|
def created_at_in_past(self) -> int:
|
||||||
|
|
@ -57,6 +44,11 @@ class ClientConfig(BaseModel):
|
||||||
+ self.created_at_seconds_future
|
+ 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
|
@property
|
||||||
def free_storage_bytes_value(self):
|
def free_storage_bytes_value(self):
|
||||||
value = self.free_storage_value * 1024
|
value = self.free_storage_value * 1024
|
||||||
|
|
@ -64,11 +56,20 @@ class ClientConfig(BaseModel):
|
||||||
value *= 1024
|
value *= 1024
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class Config:
|
class AuthorSpec(BaseModel):
|
||||||
allow_population_by_field_name = True
|
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("")
|
wallet = Field("")
|
||||||
cost_to_join = Field(0, alias="costToJoin")
|
cost_to_join = Field(0, alias="costToJoin")
|
||||||
free_storage = Field(0, alias="freeStorage")
|
free_storage = Field(0, alias="freeStorage")
|
||||||
|
|
@ -76,6 +77,10 @@ class RelayConfig(ClientConfig):
|
||||||
storage_cost_value = Field(0, alias="storageCostValue")
|
storage_cost_value = Field(0, alias="storageCostValue")
|
||||||
storage_cost_unit = Field("MB", alias="storageCostUnit")
|
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):
|
class NostrRelay(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
|
|
@ -85,12 +90,12 @@ class NostrRelay(BaseModel):
|
||||||
contact: Optional[str]
|
contact: Optional[str]
|
||||||
active: bool = False
|
active: bool = False
|
||||||
|
|
||||||
config: "RelayConfig" = RelayConfig()
|
config: "RelaySpec" = RelaySpec()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_row(cls, row: Row) -> "NostrRelay":
|
def from_row(cls, row: Row) -> "NostrRelay":
|
||||||
relay = cls(**dict(row))
|
relay = cls(**dict(row))
|
||||||
relay.config = RelayConfig(**json.loads(row["meta"]))
|
relay.config = RelaySpec(**json.loads(row["meta"]))
|
||||||
return relay
|
return relay
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{% extends "public.html" %} {% block toolbar_title %} {{ nostrrelay.name }}
|
{% extends "public.html" %} {% block toolbar_title %} {{ relay.name }}
|
||||||
<q-btn
|
<q-btn
|
||||||
flat
|
flat
|
||||||
dense
|
dense
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ from lnbits.extensions.nostrrelay.client_manager import (
|
||||||
NostrClientConnection,
|
NostrClientConnection,
|
||||||
NostrClientManager,
|
NostrClientManager,
|
||||||
)
|
)
|
||||||
from lnbits.extensions.nostrrelay.models import RelayConfig
|
from lnbits.extensions.nostrrelay.models import RelaySpec
|
||||||
|
|
||||||
from .helpers import get_fixtures
|
from .helpers import get_fixtures
|
||||||
|
|
||||||
|
|
@ -71,7 +71,7 @@ async def test_alice_and_bob():
|
||||||
|
|
||||||
async def init_clients():
|
async def init_clients():
|
||||||
client_manager = NostrClientManager()
|
client_manager = NostrClientManager()
|
||||||
await client_manager.enable_relay(RELAY_ID, RelayConfig())
|
await client_manager.enable_relay(RELAY_ID, RelaySpec())
|
||||||
|
|
||||||
ws_alice = MockWebSocket()
|
ws_alice = MockWebSocket()
|
||||||
client_alice = NostrClientConnection(relay_id=RELAY_ID, websocket=ws_alice)
|
client_alice = NostrClientConnection(relay_id=RELAY_ID, websocket=ws_alice)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue