-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,256 additions
and
1,124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.