Skip to content

Commit

Permalink
fix: config persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
motorina0 committed Oct 31, 2024
1 parent abcc435 commit dcf9086
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
41 changes: 18 additions & 23 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import json
from typing import Optional

from lnbits.db import Database

from .models import Config, Relay
from .models import Config, Relay, UserConfig

db = Database("ext_nostrclient")

Expand All @@ -29,28 +28,24 @@ async def delete_relay(relay: Relay) -> None:


######################CONFIG#######################
async def create_config() -> Config:
config = Config()
await db.execute(
"""
INSERT INTO nostrclient.config (json_data)
VALUES (?)
""",
(json.dumps(config.dict()),),
)
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d))
async def create_config(owner_id: str) -> UserConfig:
admin_config = UserConfig(owner_id=owner_id)
await db.insert("nostrclient.config", admin_config)
return admin_config


async def update_config(config: Config) -> Optional[Config]:
await db.execute(
"""UPDATE nostrclient.config SET json_data = ?""",
(json.dumps(config.dict()),),
)
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d))
async def update_config(owner_id: str, config: Config) -> UserConfig:
user_config = UserConfig(owner_id=owner_id, extra=config)
await db.update("nostrclient.config", user_config, "WHERE owner_id = :owner_id")
return user_config


async def get_config() -> Optional[Config]:
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
return json.loads(row[0], object_hook=lambda d: Config(**d)) if row else None
async def get_config(owner_id: str) -> Optional[UserConfig]:
return await db.fetchone(
"""
SELECT * FROM nostrclient.config
WHERE owner_id = :owner_id
""",
{"owner_id": owner_id},
model=UserConfig,
)
7 changes: 7 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ async def m002_create_config_table(db):
json_data TEXT NOT NULL
);"""
)


async def m003_update_config_table(db):
await db.execute("ALTER TABLE nostrclient.config RENAME COLUMN json_data TO extra")
await db.execute(
"ALTER TABLE nostrclient.config ADD COLUMN owner_id TEXT DEFAULT 'admin'"
)
5 changes: 5 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ class TestMessageResponse(BaseModel):
class Config(BaseModel):
private_ws: bool = True
public_ws: bool = False


class UserConfig(BaseModel):
owner_id: str
extra: Config = Config()
14 changes: 7 additions & 7 deletions views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ async def ws_relay(ws_id: str, websocket: WebSocket) -> None:

logger.info("New websocket connection at: '/api/v1/relay'")
try:
# config = await get_config()
# assert config, "Failed to get config"
config = Config()
config = await get_config(owner_id="admin")
assert config, "Failed to get config"

config.public_ws = True

if not config.private_ws and not config.public_ws:
Expand Down Expand Up @@ -167,15 +167,15 @@ async def ws_relay(ws_id: str, websocket: WebSocket) -> None:

@nostrclient_api_router.get("/api/v1/config", dependencies=[Depends(check_admin)])
async def api_get_config() -> Config:
config = await get_config()
config = await get_config(owner_id="admin")
if not config:
config = await create_config()
config = await create_config(owner_id="admin")
assert config, "Failed to create config"
return config


@nostrclient_api_router.put("/api/v1/config", dependencies=[Depends(check_admin)])
async def api_update_config(data: Config):
config = await update_config(data)
config = await update_config(owner_id="admin", config=data)
assert config
return config.dict()
return config.extra.dict()

0 comments on commit dcf9086

Please sign in to comment.