Working, looking good

This commit is contained in:
benarc 2022-02-10 09:58:50 +00:00
parent 0a5495f185
commit 6917813e2a
4 changed files with 53 additions and 25 deletions

View file

@ -30,6 +30,7 @@ class nostrCreateConnections(BaseModel):
class nostrRelays(BaseModel): class nostrRelays(BaseModel):
id: Optional[str] id: Optional[str]
relay: Optional[str] relay: Optional[str]
status: Optional[bool] = False
class nostrRelayList(BaseModel): class nostrRelayList(BaseModel):
id: str id: str

View file

@ -29,7 +29,7 @@
<q-table <q-table
flat flat
dense dense
:data="nostrLinks" :data="nostrrelayLinks"
row-key="id" row-key="id"
:columns="nostrTable.columns" :columns="nostrTable.columns"
:pagination.sync="nostrTable.pagination" :pagination.sync="nostrTable.pagination"
@ -38,9 +38,6 @@
{% raw %} {% raw %}
<template v-slot:header="props"> <template v-slot:header="props">
<q-tr :props="props"> <q-tr :props="props">
<q-th style="width: 5%"></q-th>
<q-th style="width: 5%"></q-th>
<q-th <q-th
v-for="col in props.cols" v-for="col in props.cols"
:key="col.name" :key="col.name"
@ -63,7 +60,12 @@
auto-width auto-width
> >
<div v-if="col.name == 'id'"></div> <div v-if="col.name == 'id'"></div>
<div v-else>{{ col.value }}</div> <div v-else>
<div v-if="col.value == true" style="background-color: green">
{{ col.value }}
</div>
<div v-else>{{ col.value }}</div>
</div>
</q-td> </q-td>
</q-tr> </q-tr>
</template> </template>
@ -143,7 +145,7 @@
<h6 class="text-subtitle1 q-my-none">{{SITE_TITLE}} Nostr Extension</h6> <h6 class="text-subtitle1 q-my-none">{{SITE_TITLE}} Nostr Extension</h6>
<p>Only Admin users can manage this extension</p> <p>Only Admin users can manage this extension</p>
<q-card-section>Okay </q-card-section> <q-card-section></q-card-section>
</q-card-section> </q-card-section>
</q-card> </q-card>
</div> </div>
@ -153,7 +155,7 @@
<script> <script>
Vue.component(VueQrcode.name, VueQrcode) Vue.component(VueQrcode.name, VueQrcode)
var maplnurldevice = obj => { var maplrelays = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
obj.theTime = obj.time * 60 - (Date.now() / 1000 - obj.timestamp) obj.theTime = obj.time * 60 - (Date.now() / 1000 - obj.timestamp)
obj.time = obj.time + 'mins' obj.time = obj.time + 'mins'
@ -179,10 +181,16 @@
allowlist: '', allowlist: '',
denylist: '' denylist: ''
}, },
nostrLinks: [], nostrrelayLinks: [],
filter: '', filter: '',
nostrTable: { nostrTable: {
columns: [ columns: [
{
name: 'wah',
align: 'left',
label: 'id',
field: 'id'
},
{ {
name: 'relay', name: 'relay',
align: 'left', align: 'left',
@ -190,10 +198,10 @@
field: 'relay' field: 'relay'
}, },
{ {
name: 'connected', name: 'status',
align: 'left', align: 'left',
label: 'connected', label: 'status',
field: 'connected' field: 'status'
} }
], ],
pagination: { pagination: {
@ -203,7 +211,7 @@
} }
}, },
methods: { methods: {
getrelays: function () { getRelays: function () {
var self = this var self = this
LNbits.api LNbits.api
.request( .request(
@ -213,7 +221,10 @@
) )
.then(function (response) { .then(function (response) {
if (response.data) { if (response.data) {
self.nostrrelayLinks = response.data.map(maprelays) console.log(response.data)
response.data.map(maplrelays)
self.nostrrelayLinks = response.data
console.log(self.nostrrelayLinks)
} }
}) })
.catch(function (error) { .catch(function (error) {
@ -284,7 +295,7 @@
}, },
created: function () { created: function () {
var self = this var self = this
this.getrelays() this.getRelays()
this.getLists() this.getLists()
} }
}) })

View file

@ -1,6 +1,5 @@
from http import HTTPStatus from http import HTTPStatus
import asyncio import asyncio
import asyncio
from fastapi import Request from fastapi import Request
from fastapi.param_functions import Query from fastapi.param_functions import Query
from fastapi.params import Depends from fastapi.params import Depends
@ -8,14 +7,17 @@ from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse from starlette.responses import HTMLResponse
from . import nostradmin_ext, nostr_renderer from . import nostradmin_ext, nostr_renderer
# FastAPI good for incoming
from fastapi import Request, WebSocket, WebSocketDisconnect from fastapi import Request, WebSocket, WebSocketDisconnect
# Websockets needed for outgoing
import websockets
from lnbits.core.crud import update_payment_status from lnbits.core.crud import update_payment_status
from lnbits.core.models import User from lnbits.core.models import User
from lnbits.core.views.api import api_payment from lnbits.core.views.api import api_payment
from lnbits.decorators import check_user_exists from lnbits.decorators import check_user_exists
from .crud import get_nostrkeys from .crud import get_nostrkeys, get_nostrrelay
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
@ -74,8 +76,8 @@ class ConnectionManager:
manager = ConnectionManager() manager = ConnectionManager()
@nostradmin_ext.websocket("/nostradmin/ws/{nostr_id}", name="nostr_id.websocket_by_id") @nostradmin_ext.websocket("/nostradmin/ws/relayevents/{nostr_id}", name="nostr_id.websocket_by_id")
async def websocket_endpoint(websocket: WebSocket, copilot_id: str): async def websocket_endpoint(websocket: WebSocket, nostr_id: str):
await manager.connect(websocket, nostr_id) await manager.connect(websocket, nostr_id)
try: try:
while True: while True:
@ -89,3 +91,12 @@ async def updater(nostr_id, message):
if not copilot: if not copilot:
return return
await manager.send_personal_message(f"{message}", nostr_id) await manager.send_personal_message(f"{message}", nostr_id)
async def relay_check(relay: str):
async with websockets.connect(relay) as websocket:
if str(websocket.state) == "State.OPEN":
print(str(websocket.state))
return True
else:
return False

View file

@ -24,11 +24,10 @@ from .crud import (
get_nostrconnections, get_nostrconnections,
) )
from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList
from .views import relay_check
@nostradmin_ext.get("/api/v1/relays") @nostradmin_ext.get("/api/v1/relays")
async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
relays = await get_nostrrelays() relays = await get_nostrrelays()
if not relays: if not relays:
await create_nostrrelays(nostrCreateRelays(relay="wss://relayer.fiatjaf.com")) await create_nostrrelays(nostrCreateRelays(relay="wss://relayer.fiatjaf.com"))
@ -36,10 +35,16 @@ async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
nostrCreateRelays(relay="wss://nostr-pub.wellorder.net") nostrCreateRelays(relay="wss://nostr-pub.wellorder.net")
) )
relays = await get_nostrrelays() relays = await get_nostrrelays()
try: if not relays:
return [{**relays.dict()} for relays in await relays] raise HTTPException(
except: status_code=HTTPStatus.UNAUTHORIZED, detail="User not authorized."
None )
else:
for relay in relays:
relay.status = await relay_check(relay.relay)
return relays
@nostradmin_ext.get("/api/v1/relaylist") @nostradmin_ext.get("/api/v1/relaylist")
async def api_relaylist(wallet: WalletTypeInfo = Depends(get_key_type)): async def api_relaylist(wallet: WalletTypeInfo = Depends(get_key_type)):