diff --git a/crud.py b/crud.py index 7882943..cf14bbb 100644 --- a/crud.py +++ b/crud.py @@ -20,6 +20,17 @@ async def create_relay(user_id: str, r: NostrRelay) -> NostrRelay: assert relay, "Created relay cannot be retrieved" return relay +async def update_relay(user_id: str, r: NostrRelay) -> NostrRelay: + await db.execute( + """ + UPDATE nostrrelay.relays + SET (name, description, pubkey, contact, active) = (?, ?, ?, ?, ?) + WHERE user_id = ? AND id = ? + """, + (r.name, r.description, r.pubkey, r.contact, r.active, user_id, r.id), + ) + + return r async def get_relay(user_id: str, relay_id: str) -> Optional[NostrRelay]: row = await db.fetchone("""SELECT * FROM nostrrelay.relays WHERE user_id = ? AND id = ?""", (user_id, relay_id,)) @@ -27,7 +38,7 @@ async def get_relay(user_id: str, relay_id: str) -> Optional[NostrRelay]: return NostrRelay.from_row(row) if row else None async def get_relays(user_id: str) -> List[NostrRelay]: - rows = await db.fetchall("""SELECT * FROM nostrrelay.relays WHERE user_id = ?""", (user_id,)) + rows = await db.fetchall("""SELECT * FROM nostrrelay.relays WHERE user_id = ? ORDER BY id ASC""", (user_id,)) return [NostrRelay.from_row(row) for row in rows] diff --git a/models.py b/models.py index 5c01495..f43153e 100644 --- a/models.py +++ b/models.py @@ -14,6 +14,7 @@ class NostrRelay(BaseModel): description: Optional[str] pubkey: Optional[str] contact: Optional[str] = "https://t.me/lnbits" + active: bool = False supported_nips: List[str] = ["NIP01", "NIP09", "NIP11", "NIP15", "NIP20"] software: Optional[str] = "LNbist" version: Optional[str] diff --git a/static/js/index.js b/static/js/index.js index 3185beb..aaffaa1 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -12,32 +12,29 @@ const relays = async () => { relayLinks: [], formDialogRelay: { show: false, - showAdvanced: false, data: { + id: '', name: '', description: '', pubkey: '', - contact: '', - contact: '', - wallet: '' + contact: '' } }, relaysTable: { columns: [ - { - name: 'id', - align: 'left', - label: 'ID', - field: 'id' - }, { name: '', align: 'left', label: '', field: '' }, - + { + name: 'id', + align: 'left', + label: 'ID', + field: 'id' + }, { name: 'name', align: 'left', @@ -106,7 +103,6 @@ const relays = async () => { createRelay: async function (data) { try { - console.log('### createRelay', data) const resp = await LNbits.api.request( 'POST', '/nostrrelay/api/v1/relay', @@ -120,10 +116,41 @@ const relays = async () => { LNbits.utils.notifyApiError(error) } }, + showToggleRelayDialog: function (relay) { + console.log('### showToggleRelayDialog', relay) + if (relay.active) { + this.toggleRelay(relay) + return + } + LNbits.utils + .confirmDialog('Are you sure you want to deactivate this relay?') + .onOk(async () => { + this.toggleRelay(relay) + }) + .onCancel(async () => { + console.log('#### onCancel') + relay.active = !relay.active + }) + }, + toggleRelay: async function (relay) { + console.log('### toggleRelay', relay) + try { + const response = await LNbits.api.request( + 'PUT', + '/nostrrelay/api/v1/relay/' + relay.id, + this.g.user.wallets[0].adminkey, + relay + ) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, deleteRelay: function (relayId) { LNbits.utils - .confirmDialog('Are you sure you want to delete this survet?') + .confirmDialog( + 'All data will be lost! Are you sure you want to delete this relay?' + ) .onOk(async () => { try { const response = await LNbits.api.request( diff --git a/templates/nostrrelay/index.html b/templates/nostrrelay/index.html index 9e62c7a..fee5df6 100644 --- a/templates/nostrrelay/index.html +++ b/templates/nostrrelay/index.html @@ -68,23 +68,29 @@ /> - {{props.row.name}} {{props.row.id}} + {{props.row.name}} {{props.row.description}} - -
{{props.row.contact}}
-
{{props.row.pubkey}}
+ +
{{props.row.contact}}
+
-
ID:
-
{{props.row.id}}
+
+ +
- -
- - -
NostrRelay: +async def api_create_relay(data: NostrRelay, wallet: WalletTypeInfo = Depends(require_admin_key)) -> NostrRelay: try: relay = await create_relay(wallet.wallet.user, data) @@ -59,6 +59,34 @@ async def api_create_survey(data: NostrRelay, wallet: WalletTypeInfo = Depends(r detail="Cannot create relay", ) +@nostrrelay_ext.put("/api/v1/relay/{relay_id}") +async def api_update_relay(relay_id: str, data: NostrRelay, wallet: WalletTypeInfo = Depends(require_admin_key)) -> NostrRelay: + if relay_id != data.id: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="Cannot change the relay id", + ) + + try: + relay = await get_relay(wallet.wallet.user, data.id) + if not relay: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Relay not found", + ) + updated_relay = NostrRelay.parse_obj({**dict(relay), **dict(data)}) + updated_relay = await update_relay(wallet.wallet.user, updated_relay) + return updated_relay + + except HTTPException as ex: + raise ex + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot update relay", + ) + @nostrrelay_ext.get("/api/v1/relay") async def api_get_relays(wallet: WalletTypeInfo = Depends(require_invoice_key)) -> List[NostrRelay]: