From ebada934b0eee4bf2035983de1d2dbef93a9e708 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 10 Feb 2023 17:25:02 +0200 Subject: [PATCH] chore: code format --- crud.py | 7 +++---- helpers.py | 10 +++++----- models.py | 19 +++++++++++++++---- templates/nostrrelay/public.html | 2 +- views.py | 8 ++++---- views_api.py | 15 ++++----------- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/crud.py b/crud.py index cf5b786..6a859b9 100644 --- a/crud.py +++ b/crud.py @@ -61,13 +61,12 @@ async def get_relay(user_id: str, relay_id: str) -> Optional[NostrRelay]: return NostrRelay.from_row(row) if row else None + async def get_relay_by_id(relay_id: str) -> Optional[NostrRelay]: """Note: it does not require `user_id`. Can read any relay. Use it with care.""" row = await db.fetchone( """SELECT * FROM nostrrelay.relays WHERE id = ?""", - ( - relay_id, - ), + (relay_id,), ) return NostrRelay.from_row(row) if row else None @@ -111,7 +110,7 @@ async def get_public_relay(relay_id: str) -> Optional[dict]: "description": relay.description, "pubkey": relay.pubkey, "contact": relay.contact, - "config": RelayPublicSpec(**dict(relay.config)).dict(by_alias=True) + "config": RelayPublicSpec(**dict(relay.config)).dict(by_alias=True), } diff --git a/helpers.py b/helpers.py index fdff734..bcf5c02 100644 --- a/helpers.py +++ b/helpers.py @@ -2,18 +2,18 @@ from bech32 import bech32_decode, convertbits def normalize_public_key(pubkey: str) -> str: - if pubkey.startswith('npub1'): + if pubkey.startswith("npub1"): _, decoded_data = bech32_decode(pubkey) if not decoded_data: raise ValueError("Public Key is not valid npub") decoded_data_bits = convertbits(decoded_data, 5, 8, False) - if not decoded_data_bits: + if not decoded_data_bits: raise ValueError("Public Key is not valid npub") return bytes(decoded_data_bits).hex() - - #check if valid hex + + # check if valid hex if len(pubkey) != 64: raise ValueError("Public Key is not valid hex") int(pubkey, 16) - return pubkey \ No newline at end of file + return pubkey diff --git a/models.py b/models.py index d9151fa..47c3dde 100644 --- a/models.py +++ b/models.py @@ -11,10 +11,13 @@ from secp256k1 import PublicKey class Spec(BaseModel): class Config: allow_population_by_field_name = True + + class FilterSpec(Spec): max_client_filters = Field(0, alias="maxClientFilters") limit_per_filter = Field(1000, alias="limitPerFilter") + class EventSpec(Spec): max_events_per_second = Field(0, alias="maxEventsPerSecond") @@ -28,7 +31,6 @@ class EventSpec(Spec): created_at_minutes_future = Field(0, alias="createdAtMinutesFuture") created_at_seconds_future = Field(0, alias="createdAtSecondsFuture") - @property def created_at_in_past(self) -> int: return ( @@ -47,6 +49,7 @@ class EventSpec(Spec): + self.created_at_seconds_future ) + class StorageSpec(Spec): free_storage_value = Field(1, alias="freeStorageValue") free_storage_unit = Field("MB", alias="freeStorageUnit") @@ -59,6 +62,7 @@ class StorageSpec(Spec): value *= 1024 return value + class AuthorSpec(Spec): allowed_public_keys = Field([], alias="allowedPublicKeys") blocked_public_keys = Field([], alias="blockedPublicKeys") @@ -78,15 +82,22 @@ class PaymentSpec(BaseModel): 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): + +class RelaySpec( + FilterSpec, EventSpec, StorageSpec, AuthorSpec, PaymentSpec, WalletSpec +): pass + class RelayPublicSpec(FilterSpec, EventSpec, StorageSpec, PaymentSpec): pass + class NostrRelay(BaseModel): id: str name: str @@ -99,7 +110,7 @@ class NostrRelay(BaseModel): @property def is_free_to_join(self): - return not self.config.is_paid_relay or self.config.cost_to_join == 0 + return not self.config.is_paid_relay or self.config.cost_to_join == 0 @classmethod def from_row(cls, row: Row) -> "NostrRelay": @@ -298,4 +309,4 @@ class NostrFilter(BaseModel): class RelayJoin(BaseModel): relay_id: str - pubkey: str \ No newline at end of file + pubkey: str diff --git a/templates/nostrrelay/public.html b/templates/nostrrelay/public.html index ffaf409..1a7a96b 100644 --- a/templates/nostrrelay/public.html +++ b/templates/nostrrelay/public.html @@ -42,7 +42,7 @@ diff --git a/views.py b/views.py index 2f85ed6..0dedab6 100644 --- a/views.py +++ b/views.py @@ -24,12 +24,12 @@ async def index(request: Request, user: User = Depends(check_user_exists)): @nostrrelay_ext.get("/{relay_id}") async def nostrrelay(request: Request, relay_id: str): relay_public_data = await get_public_relay(relay_id) - + if not relay_public_data: raise HTTPException( - status_code=HTTPStatus.NOT_FOUND, - detail="Cannot find relay", - ) + status_code=HTTPStatus.NOT_FOUND, + detail="Cannot find relay", + ) if request.headers.get("accept") == "application/nostr+json": return JSONResponse( diff --git a/views_api.py b/views_api.py index cb8c646..e4ad15f 100644 --- a/views_api.py +++ b/views_api.py @@ -155,10 +155,8 @@ async def api_delete_relay( @nostrrelay_ext.put("/api/v1/join") -async def api_pay_to_join( - data: RelayJoin -): - +async def api_pay_to_join(data: RelayJoin): + try: pubkey = normalize_public_key(data.pubkey) relay = await get_relay_by_id(data.relay_id) @@ -179,13 +177,11 @@ async def api_pay_to_join( "tag": "nostrrely", "action": "join", "relay": relay.id, - "pubkey": pubkey + "pubkey": pubkey, }, ) print("### payment_request", payment_request) - return { - "invoice": payment_request - } + return {"invoice": payment_request} except ValueError as ex: raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, @@ -199,6 +195,3 @@ async def api_pay_to_join( status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail="Cannot create invoice for client to join", ) - - -