lnbits-legend/lnbits/extensions/nostr/crud.py

115 lines
2.9 KiB
Python
Raw Normal View History

2022-02-07 13:22:43 +00:00
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
2022-02-07 20:01:01 +00:00
import shortuuid
2022-02-07 13:22:43 +00:00
from . import db
2022-02-07 20:01:01 +00:00
from .models import nostrKeys, nostrNotes, nostrCreateRelays, nostrRelays, nostrConnections, nostrCreateConnections
2022-02-07 13:22:43 +00:00
###############KEYS##################
async def create_nostrkeys(
data: nostrKeys
) -> nostrKeys:
await db.execute(
"""
INSERT INTO nostr.keys (
pubkey,
privkey
)
2022-02-07 20:01:01 +00:00
VALUES (?, ?)
2022-02-07 13:22:43 +00:00
""",
2022-02-07 20:01:01 +00:00
(data.pubkey, data.privkey),
2022-02-07 13:22:43 +00:00
)
return await get_nostrkeys(nostrkey_id)
2022-02-07 20:01:01 +00:00
async def get_nostrkeys(pubkey: str) -> nostrKeys:
2022-02-07 13:22:43 +00:00
row = await db.fetchone(
2022-02-07 20:01:01 +00:00
"SELECT * FROM nostr.keys WHERE pubkey = ?",
(pubkey,),
2022-02-07 13:22:43 +00:00
)
return nostrKeys(**row) if row else None
###############NOTES##################
async def create_nostrnotes(
data: nostrNotes
) -> nostrNotes:
await db.execute(
"""
INSERT INTO nostr.notes (
id,
pubkey,
created_at,
kind,
tags,
content,
sig
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(data.id, data.pubkey, data.created_at, data.kind, data.tags, data.content, data.sig),
)
return await get_nostrnotes(data.id)
async def get_nostrnotes(nostrnote_id: str) -> nostrNotes:
row = await db.fetchone(
"SELECT * FROM nostr.notes WHERE id = ?",
(nostrnote_id,),
)
return nostrNotes(**row) if row else None
###############RELAYS##################
async def create_nostrrelays(
2022-02-07 20:01:01 +00:00
data: nostrCreateRelays
) -> nostrCreateRelays:
nostrrelay_id = shortuuid.uuid(name=relay)
if await get_nostrrelays(nostrrelay_id):
return "error"
2022-02-07 13:22:43 +00:00
await db.execute(
"""
INSERT INTO nostr.relays (
id,
relay
)
VALUES (?, ?)
""",
2022-02-07 20:01:01 +00:00
(nostrrelay_id, data.relay),
2022-02-07 13:22:43 +00:00
)
return await get_nostrnotes(nostrrelay_id)
async def get_nostrrelays(nostrrelay_id: str) -> nostrRelays:
row = await db.fetchone(
"SELECT * FROM nostr.relays WHERE id = ?",
(nostrnote_id,),
)
return nostrRelays(**row) if row else None
###############CONNECTIONS##################
async def create_nostrconnections(
2022-02-07 20:01:01 +00:00
data: nostrCreateConnections
) -> nostrCreateConnections:
nostrrelay_id = shortuuid.uuid(name=data.relayid + data.pubkey)
2022-02-07 13:22:43 +00:00
await db.execute(
"""
2022-02-07 20:01:01 +00:00
INSERT INTO nostr.connections (
2022-02-07 13:22:43 +00:00
id,
pubkey,
2022-02-07 20:01:01 +00:00
relayid
2022-02-07 13:22:43 +00:00
)
2022-02-07 20:01:01 +00:00
VALUES (?, ?, ?)
2022-02-07 13:22:43 +00:00
""",
2022-02-07 20:01:01 +00:00
(data.id, data.pubkey, data.relayid),
2022-02-07 13:22:43 +00:00
)
2022-02-07 20:01:01 +00:00
return await get_nostrconnections(data.id)
2022-02-07 13:22:43 +00:00
2022-02-07 20:01:01 +00:00
async def get_nostrconnections(nostrconnections_id: str) -> nostrConnections:
2022-02-07 13:22:43 +00:00
row = await db.fetchone(
2022-02-07 20:01:01 +00:00
"SELECT * FROM nostr.connections WHERE id = ?",
(nostrconnections_id,),
2022-02-07 13:22:43 +00:00
)
2022-02-07 20:01:01 +00:00
return nostrConnections(**row) if row else None