From 5a984bddcd188fc2df48a9db5f357f72bf3f877b Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 16 Feb 2023 14:17:21 +0200 Subject: [PATCH] feat: mage block/allow accounts --- client_manager.py | 14 +-- models.py | 19 +-- .../relay-details/relay-details.html | 116 ++++++++++++++---- .../components/relay-details/relay-details.js | 95 ++++++++++++-- views_api.py | 2 +- 5 files changed, 188 insertions(+), 58 deletions(-) diff --git a/client_manager.py b/client_manager.py index 256b9fe..b8f61ab 100644 --- a/client_manager.py +++ b/client_manager.py @@ -299,12 +299,6 @@ class NostrClientConnection: if self._exceeded_max_events_per_second(): return False, f"Exceeded max events per second limit'!" - if not self.client_config.is_author_allowed(e.pubkey): - return ( - False, - f"Public key '{e.pubkey}' is not allowed in relay '{self.relay_id}'!", - ) - try: e.check_signature() except ValueError: @@ -326,7 +320,13 @@ class NostrClientConnection: if not account: account = NostrAccount.null_account() - if not account.paid_to_join and self.client_config.is_paid_relay: + if account.blocked: + return ( + False, + f"Public key '{pubkey}' is not allowed in relay '{self.relay_id}'!", + ) + + if not account.can_join and self.client_config.is_paid_relay: return False, f"This is a paid relay: '{self.relay_id}'" stored_bytes = await get_storage_for_public_key(self.relay_id, pubkey) diff --git a/models.py b/models.py index 9afcc20..d482f19 100644 --- a/models.py +++ b/models.py @@ -83,18 +83,6 @@ class PaymentSpec(BaseModel): storage_cost_unit = Field("MB", alias="storageCostUnit") -class AuthorSpec(Spec): - allowed_public_keys = Field([], alias="allowedPublicKeys") - blocked_public_keys = Field([], alias="blockedPublicKeys") - - def is_author_allowed(self, p: str) -> bool: - if p in self.blocked_public_keys: - return False - if len(self.allowed_public_keys) == 0: - return True - # todo: check payment - return p in self.allowed_public_keys - class WalletSpec(Spec): wallet = Field("") @@ -108,7 +96,7 @@ class RelayPublicSpec(FilterSpec, EventSpec, StorageSpec, PaymentSpec): self.free_storage_value == 0 and not self.is_paid_relay -class RelaySpec(RelayPublicSpec, AuthorSpec, WalletSpec, AuthSpec): +class RelaySpec(RelayPublicSpec, WalletSpec, AuthSpec): pass @@ -357,6 +345,11 @@ class NostrAccount(BaseModel): storage = 0 paid_to_join = False + @property + def can_join(self): + """If an account is explicitly allowed then it does not need to pay""" + return self.paid_to_join or self.allowed + @classmethod def null_account(cls) -> "NostrAccount": return NostrAccount(pubkey="") diff --git a/static/components/relay-details/relay-details.html b/static/components/relay-details/relay-details.html index 92aec13..96df833 100644 --- a/static/components/relay-details/relay-details.html +++ b/static/components/relay-details/relay-details.html @@ -526,33 +526,101 @@
+ +
+
Public Key:
+
+ +
+
+ Allow +
+
+ Block +
+
+
+
+
-
Public Key:
-
- Filter:
+
+ Show Allowed Account + + Show Blocked Accounts +
+
+ +
+
+ -
-
- Allow -
-
- Block + +
diff --git a/static/components/relay-details/relay-details.js b/static/components/relay-details/relay-details.js index 1b47cb2..193b222 100644 --- a/static/components/relay-details/relay-details.js +++ b/static/components/relay-details/relay-details.js @@ -9,6 +9,7 @@ async function relayDetails(path) { return { tab: 'info', relay: null, + accounts: [], accountPubkey: '', formDialogItem: { show: false, @@ -17,6 +18,52 @@ async function relayDetails(path) { description: '' } }, + accountsFilter: '', + showBlockedAccounts: true, + showAllowedAccounts: false, + accountsTable: { + columns: [ + { + name: 'pubkey', + align: 'left', + label: 'Public Key', + field: 'pubkey' + }, + { + name: 'allowed', + align: 'left', + label: 'Allowed', + field: 'allowed' + }, + { + name: 'blocked', + align: 'left', + label: 'Blocked', + field: 'blocked' + }, + { + name: 'paid_to_join', + align: 'left', + label: 'Paid to join', + field: 'paid_to_join' + }, + { + name: 'sats', + align: 'left', + label: 'Spent Sats', + field: 'sats' + }, + { + name: 'storage', + align: 'left', + label: 'Storage', + field: 'storage' + } + ], + pagination: { + rowsPerPage: 10 + } + }, skipEventKind: 0, forceEventKind: 0 } @@ -113,11 +160,39 @@ async function relayDetails(path) { this.relay.config.wallet = this.relay.config.wallet || this.walletOptions[0].value }, - allowPublicKey: async function (allowed) { - await this.updatePublicKey({allowed}) + getAccounts: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + `/nostrrelay/api/v1/account?relay_id=${this.relay.id}&allowed=${this.showAllowedAccounts}&blocked=${this.showBlockedAccounts}`, + this.inkey + ) + this.accounts = data + + console.log('### this.accounts', this.accounts) + } catch (error) { + LNbits.utils.notifyApiError(error) + } }, - blockPublicKey: async function (blocked = true) { - await this.updatePublicKey({blocked}) + allowPublicKey: async function (pubkey, allowed) { + await this.updatePublicKey({pubkey, allowed}) + }, + blockPublicKey: async function (pubkey, blocked = true) { + await this.updatePublicKey({pubkey, blocked}) + }, + togglePublicKey: async function (account, action) { + if (action === 'allow') { + await this.updatePublicKey({ + pubkey: account.pubkey, + allowed: account.allowed + }) + } + if (action === 'block') { + await this.updatePublicKey({ + pubkey: account.pubkey, + blocked: account.blocked + }) + } }, updatePublicKey: async function (ops) { try { @@ -127,7 +202,7 @@ async function relayDetails(path) { this.adminkey, { relay_id: this.relay.id, - pubkey: this.accountPubkey, + pubkey: ops.pubkey, allowed: ops.allowed, blocked: ops.blocked } @@ -138,18 +213,11 @@ async function relayDetails(path) { timeout: 5000 }) this.accountPubkey = '' + await this.getAccounts() } catch (error) { LNbits.utils.notifyApiError(error) } }, - deleteAllowedPublicKey: function (pubKey) { - this.relay.config.allowedPublicKeys = - this.relay.config.allowedPublicKeys.filter(p => p !== pubKey) - }, - deleteBlockedPublicKey: function (pubKey) { - this.relay.config.blockedPublicKeys = - this.relay.config.blockedPublicKeys.filter(p => p !== pubKey) - }, addSkipAuthForEvent: function () { value = +this.skipEventKind @@ -179,6 +247,7 @@ async function relayDetails(path) { created: async function () { await this.getRelay() + await this.getAccounts() } }) } diff --git a/views_api.py b/views_api.py index 8eb6965..94bb1df 100644 --- a/views_api.py +++ b/views_api.py @@ -182,7 +182,7 @@ async def api_create_or_update_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) + relay_id: str, allowed = False, blocked = True, wallet: WalletTypeInfo = Depends(require_invoice_key) ) -> List[NostrAccount]: try: # make sure the user has access to the relay