feat: check paid_to_join and storage
This commit is contained in:
parent
dfda2367a2
commit
44ae8086cc
4 changed files with 45 additions and 30 deletions
|
|
@ -8,6 +8,7 @@ from loguru import logger
|
||||||
from .crud import (
|
from .crud import (
|
||||||
create_event,
|
create_event,
|
||||||
delete_events,
|
delete_events,
|
||||||
|
get_account,
|
||||||
get_config_for_all_active_relays,
|
get_config_for_all_active_relays,
|
||||||
get_event,
|
get_event,
|
||||||
get_events,
|
get_events,
|
||||||
|
|
@ -15,7 +16,7 @@ from .crud import (
|
||||||
mark_events_deleted,
|
mark_events_deleted,
|
||||||
prune_old_events,
|
prune_old_events,
|
||||||
)
|
)
|
||||||
from .models import NostrEvent, NostrEventType, NostrFilter, RelaySpec
|
from .models import NostrAccount, NostrEvent, NostrEventType, NostrFilter, RelaySpec
|
||||||
|
|
||||||
|
|
||||||
class NostrClientManager:
|
class NostrClientManager:
|
||||||
|
|
@ -264,19 +265,20 @@ class NostrClientConnection:
|
||||||
|
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
async def _validate_storage(self, pubkey: str, size_bytes: int) -> Tuple[bool, str]:
|
async def _validate_storage(self, pubkey: str, event_size_bytes: int) -> Tuple[bool, str]:
|
||||||
if self.client_config.free_storage_value == 0:
|
if self.client_config.is_read_only_relay:
|
||||||
if not self.client_config.is_paid_relay:
|
|
||||||
return False, "Cannot write event, relay is read-only"
|
return False, "Cannot write event, relay is read-only"
|
||||||
# todo: handeld paid paid plan
|
|
||||||
return True, "Temp OK"
|
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)
|
stored_bytes = await get_storage_for_public_key(self.relay_id, pubkey)
|
||||||
if self.client_config.is_paid_relay:
|
total_available_storage = account.storage + self.client_config.free_storage_bytes_value
|
||||||
# todo: handeld paid paid plan
|
if (stored_bytes + event_size_bytes) <= total_available_storage:
|
||||||
return True, "Temp OK"
|
|
||||||
|
|
||||||
if (stored_bytes + size_bytes) <= self.client_config.free_storage_bytes_value:
|
|
||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
if self.client_config.full_storage_action == "block":
|
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}'",
|
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, ""
|
return True, ""
|
||||||
|
|
||||||
|
|
|
||||||
33
models.py
33
models.py
|
|
@ -62,6 +62,13 @@ class StorageSpec(Spec):
|
||||||
value *= 1024
|
value *= 1024
|
||||||
return value
|
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):
|
class AuthorSpec(Spec):
|
||||||
allowed_public_keys = Field([], alias="allowedPublicKeys")
|
allowed_public_keys = Field([], alias="allowedPublicKeys")
|
||||||
|
|
@ -75,29 +82,21 @@ class AuthorSpec(Spec):
|
||||||
# todo: check payment
|
# todo: check payment
|
||||||
return p in self.allowed_public_keys
|
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):
|
class WalletSpec(Spec):
|
||||||
wallet = Field("")
|
wallet = Field("")
|
||||||
|
|
||||||
|
|
||||||
class RelaySpec(
|
|
||||||
FilterSpec, EventSpec, StorageSpec, AuthorSpec, PaymentSpec, WalletSpec
|
|
||||||
):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class RelayPublicSpec(FilterSpec, EventSpec, StorageSpec, PaymentSpec):
|
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
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class NostrRelay(BaseModel):
|
class NostrRelay(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
name: str
|
name: str
|
||||||
|
|
@ -324,6 +323,10 @@ class NostrAccount(BaseModel):
|
||||||
allowed = False
|
allowed = False
|
||||||
blocked = False
|
blocked = False
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def null_account(cls) -> "NostrAccount":
|
||||||
|
return NostrAccount(pubkey="")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_row(cls, row: Row) -> "NostrAccount":
|
def from_row(cls, row: Row) -> "NostrAccount":
|
||||||
return cls(**dict(row))
|
return cls(**dict(row))
|
||||||
1
tasks.py
1
tasks.py
|
|
@ -4,7 +4,6 @@ import re
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.extensions.nostrrelay.models import NostrAccount
|
|
||||||
from lnbits.helpers import get_current_extension_name
|
from lnbits.helpers import get_current_extension_name
|
||||||
from lnbits.tasks import register_invoice_listener
|
from lnbits.tasks import register_invoice_listener
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,15 @@
|
||||||
/>
|
/>
|
||||||
</q-td>
|
</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-td key="toggle" :props="props">
|
||||||
<q-toggle
|
<q-toggle
|
||||||
size="sm"
|
size="sm"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue