From 25dde9571c1b0e5c5b697de24caa0421b50aed83 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Tue, 7 Feb 2023 17:50:48 +0200 Subject: [PATCH] feat: save relay extra config --- crud.py | 10 ++++---- models.py | 25 ++++++++++++++++--- .../relay-details/relay-details.html | 4 +-- .../components/relay-details/relay-details.js | 10 -------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/crud.py b/crud.py index 629bf91..28c986d 100644 --- a/crud.py +++ b/crud.py @@ -11,10 +11,10 @@ from .models import NostrEvent, NostrFilter, NostrRelay async def create_relay(user_id: str, r: NostrRelay) -> NostrRelay: await db.execute( """ - INSERT INTO nostrrelay.relays (user_id, id, name, description, pubkey, contact) - VALUES (?, ?, ?, ?, ?, ?) + INSERT INTO nostrrelay.relays (user_id, id, name, description, pubkey, contact, meta) + VALUES (?, ?, ?, ?, ?, ?, ?) """, - (user_id, r.id, r.name, r.description, r.pubkey, r.contact,), + (user_id, r.id, r.name, r.description, r.pubkey, r.contact, json.dumps(dict(r.config))), ) relay = await get_relay(user_id, r.id) assert relay, "Created relay cannot be retrieved" @@ -24,10 +24,10 @@ async def update_relay(user_id: str, r: NostrRelay) -> NostrRelay: await db.execute( """ UPDATE nostrrelay.relays - SET (name, description, pubkey, contact, active) = (?, ?, ?, ?, ?) + SET (name, description, pubkey, contact, active, meta) = (?, ?, ?, ?, ?, ?) WHERE user_id = ? AND id = ? """, - (r.name, r.description, r.pubkey, r.contact, r.active, user_id, r.id), + (r.name, r.description, r.pubkey, r.contact, r.active, json.dumps(dict(r.config)), user_id, r.id), ) return r diff --git a/models.py b/models.py index 6f703fd..bb69edc 100644 --- a/models.py +++ b/models.py @@ -8,6 +8,19 @@ 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") + 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 NostrRelay(BaseModel): id: str name: str @@ -16,7 +29,14 @@ class NostrRelay(BaseModel): contact: Optional[str] active: bool = False - # meta: Optional[str] + config: "RelayConfig" = RelayConfig() + + + @classmethod + def from_row(cls, row: Row) -> "NostrRelay": + relay = cls(**dict(row)) + relay.config = RelayConfig(**json.loads(row["meta"])) + return relay @classmethod def info(cls,) -> dict: @@ -28,9 +48,6 @@ class NostrRelay(BaseModel): } - @classmethod - def from_row(cls, row: Row) -> "NostrRelay": - return cls(**dict(row)) diff --git a/static/components/relay-details/relay-details.html b/static/components/relay-details/relay-details.html index ed42511..ad36d51 100644 --- a/static/components/relay-details/relay-details.html +++ b/static/components/relay-details/relay-details.html @@ -162,14 +162,14 @@
Unlimited Filters diff --git a/static/components/relay-details/relay-details.js b/static/components/relay-details/relay-details.js index 8327373..2eaf8fe 100644 --- a/static/components/relay-details/relay-details.js +++ b/static/components/relay-details/relay-details.js @@ -56,16 +56,6 @@ async function relayDetails(path) { '/nostrrelay/api/v1/relay/' + this.relayId, this.inkey ) - data.config = { - isPaidRelay: true, - wallet: '', - costToJoin: 0, - freeStorage: 0, - storageCostPerKb: 0, - maxFilters: 0, - allowedPublicKeys: [], - blockedPublicKeys: [] - } this.relay = data console.log('### this.relay', this.relay)