feat: publish event on C_UD for stalls

This commit is contained in:
Vlad Stan 2023-03-02 10:14:11 +02:00
parent aba3706a71
commit 5972c44ad1
7 changed files with 305 additions and 40 deletions

View file

@ -1,10 +1,13 @@
import json
import time
from sqlite3 import Row
from typing import List, Optional
from fastapi import Query
from pydantic import BaseModel
from .helpers import sign_message_hash
from .nostr.event import NostrEvent
######################################## MERCHANT ########################################
class MerchantConfig(BaseModel):
@ -20,6 +23,9 @@ class PartialMerchant(BaseModel):
class Merchant(PartialMerchant):
id: str
def sign_hash(self, hash: bytes) -> str:
return sign_message_hash(self.private_key, hash)
@classmethod
def from_row(cls, row: Row) -> "Merchant":
merchant = cls(**dict(row))
@ -49,24 +55,57 @@ class Zone(PartialZone):
class StallConfig(BaseModel):
"""Last published nostr event id for this Stall"""
event_id: Optional[str]
image_url: Optional[str]
fiat_base_multiplier: int = 1 # todo: reminder wht is this for?
description: Optional[str]
class PartialStall(BaseModel):
wallet: str
name: str
currency: str = "sat"
shipping_zones: List[str] = []
shipping_zones: List[PartialZone] = []
config: StallConfig = StallConfig()
class Stall(PartialStall):
id: str
def to_nostr_event(self, pubkey: str) -> NostrEvent:
content = {
"name": self.name,
"description": self.config.description,
"currency": self.currency,
"shipping": [dict(z) for z in self.shipping_zones],
}
event = NostrEvent(
pubkey=pubkey,
created_at=round(time.time()),
kind=30005,
tags=[["d", self.id]],
content=json.dumps(content, separators=(",", ":"), ensure_ascii=False),
)
event.id = event.event_id
return event
def to_nostr_delete_event(self, pubkey: str) -> NostrEvent:
delete_event = NostrEvent(
pubkey=pubkey,
created_at=round(time.time()),
kind=5,
tags=[["e", self.config.event_id]],
content="Stall deleted",
)
delete_event.id = delete_event.event_id
return delete_event
@classmethod
def from_row(cls, row: Row) -> "Stall":
stall = cls(**dict(row))
stall.config = StallConfig(**json.loads(row["meta"]))
stall.shipping_zones = json.loads(row["zones"])
stall.shipping_zones = [PartialZone(**z) for z in json.loads(row["zones"])]
return stall