Skip to content

Commit

Permalink
v1 in the middle (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
dni authored Oct 31, 2024
1 parent a8eb139 commit db20915
Show file tree
Hide file tree
Showing 14 changed files with 1,256 additions and 1,124 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from loguru import logger

from .crud import db
from .nostr_client import all_routers, nostr_client
from .router import all_routers, nostr_client
from .tasks import check_relays, init_relays, subscribe_events
from .views import nostrclient_generic_router
from .views_api import nostrclient_api_router
Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "Nostr Client",
"short_description": "Nostr client for extensions",
"tile": "/nostrclient/static/images/nostr-bitcoin.png",
"contributors": ["calle", "motorina0"],
"min_lnbits_version": "0.12.0"
"contributors": ["calle", "motorina0", "dni"],
"min_lnbits_version": "1.0.0"
}
71 changes: 33 additions & 38 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
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")


async def get_relays() -> list[Relay]:
rows = await db.fetchall("SELECT * FROM nostrclient.relays")
return [Relay.from_row(r) for r in rows]
return await db.fetchall(
"SELECT * FROM nostrclient.relays",
model=Relay,
)


async def add_relay(relay: Relay) -> None:
await db.execute(
"""
INSERT INTO nostrclient.relays (
id,
url,
active
)
VALUES (?, ?, ?)
""",
(relay.id, relay.url, relay.active),
)
async def add_relay(relay: Relay) -> Relay:
await db.insert("nostrclient.relays", relay)
return relay


async def delete_relay(relay: Relay) -> None:
await db.execute("DELETE FROM nostrclient.relays WHERE url = ?", (relay.url,))
if not relay.url:
return
await db.execute(
"DELETE FROM nostrclient.relays WHERE url = :url", {"url": relay.url}
)


######################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) -> Config:
admin_config = UserConfig(owner_id=owner_id)
await db.insert("nostrclient.config", admin_config)
return admin_config.extra


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) -> Config:
user_config = UserConfig(owner_id=owner_id, extra=config)
await db.update("nostrclient.config", user_config, "WHERE owner_id = :owner_id")
return user_config.extra


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[Config]:
user_config: UserConfig = await db.fetchone(
"""
SELECT * FROM nostrclient.config
WHERE owner_id = :owner_id
""",
{"owner_id": owner_id},
model=UserConfig,
)
if user_config:
return user_config.extra
return None
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'"
)
32 changes: 20 additions & 12 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
from sqlite3 import Row
from typing import List, Optional
from typing import Optional

from lnbits.helpers import urlsafe_short_hash
from pydantic import BaseModel
from pydantic import BaseModel, Field


class RelayStatus(BaseModel):
num_sent_events: Optional[int] = 0
num_received_events: Optional[int] = 0
error_counter: Optional[int] = 0
error_list: Optional[List] = []
notice_list: Optional[List] = []
error_list: Optional[list] = []
notice_list: Optional[list] = []


class Relay(BaseModel):
id: Optional[str] = None
url: Optional[str] = None
connected: Optional[bool] = None
connected_string: Optional[str] = None
status: Optional[RelayStatus] = None
active: Optional[bool] = None
ping: Optional[int] = None

connected: Optional[bool] = Field(default=None, no_database=True)
connected_string: Optional[str] = Field(default=None, no_database=True)
status: Optional[RelayStatus] = Field(default=None, no_database=True)

ping: Optional[int] = Field(default=None, no_database=True)

def _init__(self):
if not self.id:
self.id = urlsafe_short_hash()

@classmethod
def from_row(cls, row: Row) -> "Relay":
return cls(**dict(row))

class RelayDb(BaseModel):
id: str
url: str
active: Optional[bool] = True


class TestMessage(BaseModel):
Expand All @@ -46,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()
4 changes: 3 additions & 1 deletion nostr/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@


class NostrClient:
relay_manager = RelayManager()
relay_manager: RelayManager
running: bool

def __init__(self):
self.running = True
self.relay_manager = RelayManager()

def connect(self, relays):
for relay in relays:
Expand Down
5 changes: 0 additions & 5 deletions nostr_client.py

This file was deleted.

Loading

0 comments on commit db20915

Please sign in to comment.