diff --git a/lnbits/extensions/nostradmin/__init__.py b/lnbits/extensions/nostradmin/__init__.py index 7034ca46b..797542c49 100644 --- a/lnbits/extensions/nostradmin/__init__.py +++ b/lnbits/extensions/nostradmin/__init__.py @@ -5,7 +5,7 @@ from lnbits.helpers import template_renderer db = Database("ext_nostradmin") -nostr_ext: APIRouter = APIRouter(prefix="/nostradmin", tags=["nostradmin"]) +nostradmin_ext: APIRouter = APIRouter(prefix="/nostradmin", tags=["nostradmin"]) def nostr_renderer(): diff --git a/lnbits/extensions/nostradmin/crud.py b/lnbits/extensions/nostradmin/crud.py index 08908e856..1e1bf8106 100644 --- a/lnbits/extensions/nostradmin/crud.py +++ b/lnbits/extensions/nostradmin/crud.py @@ -12,6 +12,7 @@ from .models import ( nostrCreateConnections, nostrRelayList, ) +from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList ###############KEYS################## @@ -100,31 +101,20 @@ async def get_nostrrelay(nostrrelay_id: str) -> nostrRelays: row = await db.fetchone("SELECT * FROM nostradmin.relays WHERE id = ?", (nostrrelay_id,)) return nostrRelays(**row) if row else None - -async def update_nostrrelayallowlist(allowlist: str) -> nostrRelayList: +async def update_nostrrelaysetlist(data: nostrRelaySetList) -> nostrRelayList: await db.execute( """ - UPDATE nostradmin.relaylist SET + UPDATE nostradmin.relaylists SET + denylist = ?, allowlist = ? WHERE id = ? """, - (allowlist, 1), - ) - return await get_nostrrelaylist() - -async def update_nostrrelaydenylist(denylist: str) -> nostrRelayList: - await db.execute( - """ - UPDATE nostradmin.relaylist SET - denylist = ? - WHERE id = ? - """, - (denylist, 1), + (data.denylist, data.allowlist, 1), ) return await get_nostrrelaylist() async def get_nostrrelaylist() -> nostrRelayList: - row = await db.fetchone("SELECT * FROM nostradmin.relaylist WHERE id = ?", (1,)) + row = await db.fetchone("SELECT * FROM nostradmin.relaylists WHERE id = ?", (1,)) return nostrRelayList(**row) if row else None diff --git a/lnbits/extensions/nostradmin/migrations.py b/lnbits/extensions/nostradmin/migrations.py index 09b28117d..590f72ea7 100644 --- a/lnbits/extensions/nostradmin/migrations.py +++ b/lnbits/extensions/nostradmin/migrations.py @@ -1,11 +1,8 @@ from lnbits.db import Database -db2 = Database("ext_nostr") - - async def m001_initial(db): """ - Initial nostr table. + Initial nostradmin table. """ await db.execute( f""" @@ -40,24 +37,23 @@ async def m001_initial(db): f""" CREATE TABLE nostradmin.relaylists ( id TEXT NOT NULL PRIMARY KEY DEFAULT 1, - allowlist TEXT NOT NULL, - denylist TEXT NOT NULL + allowlist TEXT, + denylist TEXT ); """ ) - try: - await db.execute( - """ - INSERT INTO nostradmin.relaylist ( - id, - denylist - ) - VALUES (?, ?,) - """, - (1, "\n".join(["wss://zucks-meta-relay.com", "wss://nostradmin.cia.gov"])), + + await db.execute( + """ + INSERT INTO nostradmin.relaylists ( + id, + denylist ) - except: - return + VALUES (?, ?) + """, + ("1", "wss://zucks-meta-relay.com\nwss://nostr.cia.gov",), + ) + await db.execute( f""" CREATE TABLE nostradmin.connections ( @@ -67,3 +63,12 @@ async def m001_initial(db): ); """ ) + await db.execute( + f""" + CREATE TABLE nostradmin.subscribed ( + id TEXT NOT NULL PRIMARY KEY, + userPubkey TEXT NOT NULL, + subscribedPubkey TEXT NOT NULL + ); + """ + ) \ No newline at end of file diff --git a/lnbits/extensions/nostradmin/models.py b/lnbits/extensions/nostradmin/models.py index fd89f5155..1968567f3 100644 --- a/lnbits/extensions/nostradmin/models.py +++ b/lnbits/extensions/nostradmin/models.py @@ -5,7 +5,7 @@ from typing import Optional from fastapi import Request from pydantic import BaseModel from pydantic.main import BaseModel - +from fastapi.param_functions import Query class nostrKeys(BaseModel): pubkey: str @@ -21,28 +21,31 @@ class nostrNotes(BaseModel): sig: str class nostrCreateRelays(BaseModel): - relay: str + relay: str = Query(None) class nostrCreateConnections(BaseModel): - pubkey: str - relayid: str + pubkey: str = Query(None) + relayid: str = Query(None) class nostrRelays(BaseModel): - id: str - relay: str + id: Optional[str] + relay: Optional[str] class nostrRelayList(BaseModel): id: str - allowlist: str - denylist: str + allowlist: Optional[str] + denylist: Optional[str] -class nostrRelayDenyList(BaseModel): - denylist: str - -class nostrRelayAllowList(BaseModel): - allowlist: str +class nostrRelaySetList(BaseModel): + allowlist: Optional[str] + denylist: Optional[str] class nostrConnections(BaseModel): id: str - pubkey: str - relayid: str + pubkey: Optional[str] + relayid: Optional[str] + +class nostrSubscriptions(BaseModel): + id: str + userPubkey: Optional[str] + subscribedPubkey: Optional[str] \ No newline at end of file diff --git a/lnbits/extensions/nostradmin/templates/nostradmin/index.html b/lnbits/extensions/nostradmin/templates/nostradmin/index.html index adab98e2f..27decdc80 100644 --- a/lnbits/extensions/nostradmin/templates/nostradmin/index.html +++ b/lnbits/extensions/nostradmin/templates/nostradmin/index.html @@ -6,7 +6,7 @@
-
Nostr
+
NOSTR RELAYS ONLINE
@@ -29,7 +29,7 @@ - - + + - + - + -
- Deny List (denys use of relays in this list) -
- +
Relays in this list will NOT be used
+
- +
Update Deny List - Reset
-
- Allow List (denys any relays not in this list) -
- +
ONLY relays in this list will be used
+
- +
Update Allow List - Reset
@@ -175,8 +175,12 @@ data: function () { return { listSelection: 'denylist', - allowList: [], - denyList: [], + setList: { + allowlist: '', + denylist: '' + }, + nostrLinks: [], + filter: '', nostrTable: { columns: [ { @@ -234,18 +238,20 @@ LNbits.utils.notifyApiError(error) }) }, - setDenyList: function () { + setRelayList: function () { var self = this + console.log(self.setList) LNbits.api .request( 'POST', - '/nostradmin/api/v1/denylist', + '/nostradmin/api/v1/setlist', self.g.user.wallets[0].adminkey, - self.allowList + self.setList ) .then(function (response) { if (response.data) { - self.denyList = response.data + console.log(response.data) + // self.denyList = response.data } }) .catch(function (error) { @@ -262,13 +268,18 @@ ) .then(function (response) { if (response.data) { - self.denyList = response.data.denylist - self.allowList = response.data.allowlist + console.log(response.data) + self.setList.denylist = response.data.denylist + self.setList.allowlist = response.data.allowlist } }) .catch(function (error) { LNbits.utils.notifyApiError(error) }) + }, + exportlnurldeviceCSV: function () { + var self = this + LNbits.utils.exportCSV(self.nostrTable.columns, this.nostrLinks) } }, created: function () { diff --git a/lnbits/extensions/nostradmin/views.py b/lnbits/extensions/nostradmin/views.py index 5609c2185..51297320b 100644 --- a/lnbits/extensions/nostradmin/views.py +++ b/lnbits/extensions/nostradmin/views.py @@ -6,18 +6,19 @@ from fastapi.params import Depends from fastapi.templating import Jinja2Templates from starlette.exceptions import HTTPException from starlette.responses import HTMLResponse +from . import nostradmin_ext, nostr_renderer from lnbits.core.crud import update_payment_status from lnbits.core.models import User from lnbits.core.views.api import api_payment from lnbits.decorators import check_user_exists -from . import nostr_ext, nostr_renderer + templates = Jinja2Templates(directory="templates") -@nostr_ext.get("/", response_class=HTMLResponse) +@nostradmin_ext.get("/", response_class=HTMLResponse) async def index(request: Request, user: User = Depends(check_user_exists)): return nostr_renderer().TemplateResponse( "nostradmin/index.html", {"request": request, "user": user.dict()} diff --git a/lnbits/extensions/nostradmin/views_api.py b/lnbits/extensions/nostradmin/views_api.py index da6e140ba..d79315ac8 100644 --- a/lnbits/extensions/nostradmin/views_api.py +++ b/lnbits/extensions/nostradmin/views_api.py @@ -7,11 +7,10 @@ from starlette.exceptions import HTTPException from lnbits.core.crud import get_user from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key -from lnbits.extensions.nostr import nostr_ext from lnbits.utils.exchange_rates import currencies -from . import nostr_ext from lnbits.settings import LNBITS_ADMIN_USERS +from . import nostradmin_ext from .crud import ( create_nostrkeys, get_nostrkeys, @@ -20,13 +19,11 @@ from .crud import ( create_nostrrelays, get_nostrrelays, get_nostrrelaylist, - update_nostrrelayallowlist, - update_nostrrelaydenylist, + update_nostrrelaysetlist, create_nostrconnections, get_nostrconnections, ) -from .models import nostrKeys, nostrCreateRelays, nostrRelayAllowList, nostrRelayDenyList - +from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList # while True: async def nostr_subscribe(): @@ -41,13 +38,7 @@ async def nostr_subscribe(): websocket_queue = asyncio.Queue(1000) -async def internal_invoice_listener(): - while True: - checking_id = await internal_invoice_queue.get() - asyncio.create_task(invoice_callback_dispatcher(checking_id)) - - -@nostr_ext.get("/api/v1/relays") +@nostradmin_ext.get("/api/v1/relays") async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): relays = await get_nostrrelays() @@ -62,7 +53,7 @@ async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): except: None -@nostr_ext.get("/api/v1/relaylist") +@nostradmin_ext.get("/api/v1/relaylist") async def api_relaylist(wallet: WalletTypeInfo = Depends(get_key_type)): if wallet.wallet.user not in LNBITS_ADMIN_USERS: raise HTTPException( @@ -70,18 +61,10 @@ async def api_relaylist(wallet: WalletTypeInfo = Depends(get_key_type)): ) return await get_nostrrelaylist() -@nostr_ext.post("/api/v1/allowlist") -async def api_relaysallowed(data: nostrRelayAllowList, wallet: WalletTypeInfo = Depends(get_key_type)): +@nostradmin_ext.post("/api/v1/setlist") +async def api_relayssetlist(data: nostrRelaySetList, wallet: WalletTypeInfo = Depends(get_key_type)): if wallet.wallet.user not in LNBITS_ADMIN_USERS: raise HTTPException( status_code=HTTPStatus.UNAUTHORIZED, detail="User not authorized." ) - return await update_nostrrelayallowlist(data) - -@nostr_ext.post("/api/v1/denylist") -async def api_relaysdenyed(data: nostrRelayDenyList, wallet: WalletTypeInfo = Depends(get_key_type)): - if wallet.wallet.user not in LNBITS_ADMIN_USERS: - raise HTTPException( - status_code=HTTPStatus.UNAUTHORIZED, detail="User not authorized." - ) - return await update_nostrrelaydenylist(data) \ No newline at end of file + return await update_nostrrelaysetlist(data) \ No newline at end of file