feat: check paid_to_join and storage

This commit is contained in:
Vlad Stan 2023-02-14 09:46:33 +02:00
parent dfda2367a2
commit 44ae8086cc
4 changed files with 45 additions and 30 deletions

View file

@ -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, ""

View file

@ -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))

View file

@ -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

View file

@ -68,7 +68,15 @@
/>
</q-td>
<q-td key="id" :props="props"> {{props.row.id}} </q-td>
<q-td key="id" :props="props">
<a
style="color: unset"
:href="props.row.id"
target="_blank"
>
{{props.row.id}}</a
>
</q-td>
<q-td key="toggle" :props="props">
<q-toggle
size="sm"