models decided

This commit is contained in:
benarc 2022-02-08 13:13:36 +00:00
parent 29bd8d9ce9
commit 2083ecbc27
7 changed files with 100 additions and 107 deletions

View file

@ -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():

View file

@ -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

View file

@ -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
);
"""
)

View file

@ -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]

View file

@ -6,7 +6,7 @@
<q-card-section>
<div class="row items-center no-wrap q-mb-md">
<div class="col">
<h5 class="text-subtitle1 q-my-none">Nostr</h5>
<h5 class="text-subtitle1 q-my-none">NOSTR RELAYS ONLINE</h5>
</div>
<div class="col-auto">
@ -29,7 +29,7 @@
<q-table
flat
dense
:data="lnurldeviceLinks"
:data="nostrLinks"
row-key="id"
:columns="nostrTable.columns"
:pagination.sync="nostrTable.pagination"
@ -81,21 +81,24 @@
align="justify"
narrow-indicator
>
<q-tab name="denylist" label="Deny List" />
<q-tab name="allowlist" label="Allow List" />
<q-tab name="denylist" label="Deny List"></q-tab>
<q-tab name="allowlist" label="Allow List"></q-tab>
</q-tabs>
<q-separator />
<q-separator></q-separator>
<q-tab-panels v-model="tab" animated>
<q-tab-panels v-model="listSelection" animated>
<q-tab-panel name="denylist">
<div class="text-h6">
Deny List (denys use of relays in this list)
</div>
<q-form class="q-gutter-md q-y-md" @submit="setDenyList">
<div class="text-h6">Relays in this list will NOT be used</div>
<q-form class="q-gutter-md q-y-md" @submit="setRelayList">
<div class="row">
<div class="col q-mx-lg">
<q-input v-model="denyList" dense filled autogrow />
<q-input
v-model="setList.denylist"
dense
filled
autogrow
></q-input>
</div>
<div
class="col q-mx-lg items-align flex items-center justify-center"
@ -103,22 +106,22 @@
<q-btn unelevated color="primary" type="submit">
Update Deny List
</q-btn>
<q-btn @click="loadShop" flat color="grey" class="q-ml-auto"
>Reset</q-btn
>
</div>
</div>
</q-form>
</q-tab-panel>
<q-tab-panel name="allowlist">
<div class="text-h6">
Allow List (denys any relays not in this list)
</div>
<q-form class="q-gutter-md q-y-md" @submit="setAllowList">
<div class="text-h6">ONLY relays in this list will be used</div>
<q-form class="q-gutter-md q-y-md" @submit="setRelayList">
<div class="row">
<div class="col q-mx-lg">
<q-input v-model="allowList" dense filled autogrow />
<q-input
v-model="setList.allowlist"
dense
filled
autogrow
></q-input>
</div>
<div
class="col q-mx-lg items-align flex items-center justify-center"
@ -126,9 +129,6 @@
<q-btn unelevated color="primary" type="submit">
Update Allow List
</q-btn>
<q-btn @click="loadShop" flat color="grey" class="q-ml-auto"
>Reset</q-btn
>
</div>
</div>
</q-form>
@ -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 () {

View file

@ -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()}

View file

@ -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)
return await update_nostrrelaysetlist(data)