Public Key:
diff --git a/static/components/relay-details/relay-details.js b/static/components/relay-details/relay-details.js
index 99f75c4..31662f2 100644
--- a/static/components/relay-details/relay-details.js
+++ b/static/components/relay-details/relay-details.js
@@ -114,9 +114,29 @@ async function relayDetails(path) {
this.relay.config.wallet =
this.relay.config.wallet || this.walletOptions[0].value
},
- allowPublicKey: function () {
- this.relay.config.allowedPublicKeys.push(this.allowedPubkey)
- this.allowedPubkey = ''
+ allowPublicKey: async function () {
+ try {
+ const {data} = await LNbits.api.request(
+ 'PUT',
+ '/nostrrelay/api/v1/account',
+ this.adminkey,
+ {
+ pubkey: this.allowedPubkey,
+ allowed: true
+ }
+ )
+ this.relay = data
+ this.$emit('relay-updated', this.relay)
+ this.$q.notify({
+ type: 'positive',
+ message: 'Account Updated',
+ timeout: 5000
+ })
+ this.allowedPubkey = ''
+ } catch (error) {
+ LNbits.utils.notifyApiError(error)
+ }
+
},
blockPublicKey: function () {
this.relay.config.blockedPublicKeys.push(this.blockedPubkey)
diff --git a/tasks.py b/tasks.py
index 88c21fc..b902dbb 100644
--- a/tasks.py
+++ b/tasks.py
@@ -27,21 +27,21 @@ async def on_invoice_paid(payment: Payment):
pubkey = payment.extra.get("pubkey")
if payment.extra.get("action") == "join":
- await invoice_paid_to_join(relay_id, pubkey)
+ await invoice_paid_to_join(relay_id, pubkey, payment.amount)
return
if payment.extra.get("action") == "storage":
storage_to_buy = payment.extra.get("storage_to_buy")
- await invoice_paid_for_storage(relay_id, pubkey, storage_to_buy)
+ await invoice_paid_for_storage(relay_id, pubkey, storage_to_buy, payment.amount)
return
-async def invoice_paid_to_join(relay_id: str, pubkey: str):
+async def invoice_paid_to_join(relay_id: str, pubkey: str, amount: int):
try:
account = await get_account(relay_id, pubkey)
if not account:
await create_account(
- relay_id, NostrAccount(pubkey=pubkey, paid_to_join=True)
+ relay_id, NostrAccount(pubkey=pubkey, paid_to_join=True, sats=amount)
)
return
@@ -49,18 +49,19 @@ async def invoice_paid_to_join(relay_id: str, pubkey: str):
return
account.paid_to_join = True
+ account.sats += amount
await update_account(relay_id, account)
except Exception as ex:
logger.warning(ex)
-async def invoice_paid_for_storage(relay_id: str, pubkey: str, storage_to_buy: int):
+async def invoice_paid_for_storage(relay_id: str, pubkey: str, storage_to_buy: int, amount: str):
try:
account = await get_account(relay_id, pubkey)
if not account:
await create_account(
- relay_id, NostrAccount(pubkey=pubkey, storage=storage_to_buy)
+ relay_id, NostrAccount(pubkey=pubkey, storage=storage_to_buy, sats=amount)
)
return
@@ -68,6 +69,7 @@ async def invoice_paid_for_storage(relay_id: str, pubkey: str, storage_to_buy: i
return
account.storage = storage_to_buy
+ account.sats += amount
await update_account(relay_id, account)
except Exception as ex:
diff --git a/views_api.py b/views_api.py
index ca45aa1..3e1ae0b 100644
--- a/views_api.py
+++ b/views_api.py
@@ -18,16 +18,20 @@ from lnbits.helpers import urlsafe_short_hash
from . import nostrrelay_ext
from .client_manager import NostrClientConnection, NostrClientManager
from .crud import (
+ create_account,
create_relay,
delete_all_events,
delete_relay,
+ get_account,
+ get_accounts,
get_relay,
get_relay_by_id,
get_relays,
+ update_account,
update_relay,
)
from .helpers import extract_domain, normalize_public_key
-from .models import BuyOrder, NostrRelay
+from .models import BuyOrder, NostrAccount, NostrPartialAccount, NostrRelay
client_manager = NostrClientManager()
@@ -141,6 +145,64 @@ async def api_get_relay(
return relay
+@nostrrelay_ext.put("/api/v1/account")
+async def api_create_or_update_account(
+ data: NostrPartialAccount,
+ wallet: WalletTypeInfo = Depends(require_admin_key),
+) -> NostrAccount:
+
+ try:
+ data.pubkey = normalize_public_key(data.pubkey)
+ account = await get_account(data.relay_id, data.pubkey)
+ if not account:
+ return await create_account(data.relay_id, NostrAccount.parse_obj(data.dict()))
+
+ if data.blocked is not None:
+ account.blocked = data.blocked
+ if data.allowed is not None:
+ account.allowed = data.allowed
+ return await update_account(data.relay_id, account)
+
+ except ValueError as ex:
+ raise HTTPException(
+ status_code=HTTPStatus.BAD_REQUEST,
+ detail=str(ex),
+ )
+ except HTTPException as ex:
+ raise ex
+ except Exception as ex:
+ logger.warning(ex)
+ raise HTTPException(
+ status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
+ detail="Cannot create account",
+ )
+
+
+@nostrrelay_ext.get("/api/v1/account")
+async def api_get_accounts(
+ relay_id: str, allowed: bool, blocked: bool, wallet: WalletTypeInfo = Depends(require_invoice_key)
+) -> List[NostrAccount]:
+ try:
+ # make sure the user has access to the relay
+ relay = await get_relay(wallet.wallet.user, relay_id)
+ accounts = await get_accounts(relay.id, allowed, blocked)
+ return accounts
+ except ValueError as ex:
+ raise HTTPException(
+ status_code=HTTPStatus.BAD_REQUEST,
+ detail=str(ex),
+ )
+ except HTTPException as ex:
+ raise ex
+ except Exception as ex:
+ logger.warning(ex)
+ raise HTTPException(
+ status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
+ detail="Cannot fetch accounts",
+ )
+
+
+
@nostrrelay_ext.delete("/api/v1/relay/{relay_id}")
async def api_delete_relay(
relay_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
@@ -193,7 +255,6 @@ async def api_pay_to_join(data: BuyOrder):
"storage_to_buy": storage_to_buy,
},
)
- print("### payment_request", payment_request)
return {"invoice": payment_request}
except ValueError as ex:
raise HTTPException(