diff --git a/client_manager.py b/client_manager.py index 89074dd..0ee0e6f 100644 --- a/client_manager.py +++ b/client_manager.py @@ -8,6 +8,7 @@ from loguru import logger from .crud import ( create_event, delete_events, + get_account, get_config_for_all_active_relays, get_event, get_events, @@ -15,7 +16,7 @@ from .crud import ( mark_events_deleted, prune_old_events, ) -from .models import NostrEvent, NostrEventType, NostrFilter, RelaySpec +from .models import NostrAccount, NostrEvent, NostrEventType, NostrFilter, RelaySpec class NostrClientManager: @@ -264,19 +265,20 @@ class NostrClientConnection: return True, "" - async def _validate_storage(self, pubkey: str, size_bytes: int) -> Tuple[bool, str]: - if self.client_config.free_storage_value == 0: - if not self.client_config.is_paid_relay: - return False, "Cannot write event, relay is read-only" - # todo: handeld paid paid plan - return True, "Temp OK" + async def _validate_storage(self, pubkey: str, event_size_bytes: int) -> Tuple[bool, str]: + if self.client_config.is_read_only_relay: + return False, "Cannot write event, relay is read-only" + + account = await get_account(self.relay_id, pubkey) + if not account: + account = NostrAccount.null_account() + + if not account.paid_to_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) - if self.client_config.is_paid_relay: - # todo: handeld paid paid plan - return True, "Temp OK" - - if (stored_bytes + size_bytes) <= self.client_config.free_storage_bytes_value: + total_available_storage = account.storage + self.client_config.free_storage_bytes_value + if (stored_bytes + event_size_bytes) <= total_available_storage: return True, "" if self.client_config.full_storage_action == "block": @@ -285,7 +287,10 @@ class NostrClientConnection: f"Cannot write event, no more storage available for public key: '{pubkey}'", ) - await prune_old_events(self.relay_id, pubkey, size_bytes) + if event_size_bytes > total_available_storage: + return False, "Message is too large. Not enough storage available for it." + + await prune_old_events(self.relay_id, pubkey, event_size_bytes) return True, "" diff --git a/models.py b/models.py index d83f4ef..45f6b51 100644 --- a/models.py +++ b/models.py @@ -62,6 +62,13 @@ class StorageSpec(Spec): value *= 1024 return value +class PaymentSpec(BaseModel): + is_paid_relay = Field(False, alias="isPaidRelay") + cost_to_join = Field(0, alias="costToJoin") + + storage_cost_value = Field(0, alias="storageCostValue") + storage_cost_unit = Field("MB", alias="storageCostUnit") + class AuthorSpec(Spec): allowed_public_keys = Field([], alias="allowedPublicKeys") @@ -75,29 +82,21 @@ class AuthorSpec(Spec): # todo: check payment return p in self.allowed_public_keys - -class PaymentSpec(BaseModel): - is_paid_relay = Field(False, alias="isPaidRelay") - cost_to_join = Field(0, alias="costToJoin") - - storage_cost_value = Field(0, alias="storageCostValue") - storage_cost_unit = Field("MB", alias="storageCostUnit") - - class WalletSpec(Spec): wallet = Field("") -class RelaySpec( - FilterSpec, EventSpec, StorageSpec, AuthorSpec, PaymentSpec, WalletSpec -): - pass - - class RelayPublicSpec(FilterSpec, EventSpec, StorageSpec, PaymentSpec): + @property + def is_read_only_relay(self): + self.free_storage_value == 0 and not self.is_paid_relay + +class RelaySpec(RelayPublicSpec, AuthorSpec, WalletSpec): pass + + class NostrRelay(BaseModel): id: str name: str @@ -324,6 +323,10 @@ class NostrAccount(BaseModel): allowed = False blocked = False + @classmethod + def null_account(cls) -> "NostrAccount": + return NostrAccount(pubkey="") + @classmethod def from_row(cls, row: Row) -> "NostrAccount": return cls(**dict(row)) \ No newline at end of file diff --git a/tasks.py b/tasks.py index db09d7b..c3a8322 100644 --- a/tasks.py +++ b/tasks.py @@ -4,7 +4,6 @@ import re from loguru import logger from lnbits.core.models import Payment -from lnbits.extensions.nostrrelay.models import NostrAccount from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener diff --git a/templates/nostrrelay/index.html b/templates/nostrrelay/index.html index c001f66..77291c3 100644 --- a/templates/nostrrelay/index.html +++ b/templates/nostrrelay/index.html @@ -68,7 +68,15 @@ /> - {{props.row.id}} + + + {{props.row.id}} +