refactor: extract simpler methods
This commit is contained in:
parent
35298a4f44
commit
1b317b1b9b
2 changed files with 46 additions and 52 deletions
13
models.py
13
models.py
|
|
@ -256,6 +256,7 @@ class OrderContact(BaseModel):
|
||||||
nostr: Optional[str]
|
nostr: Optional[str]
|
||||||
phone: Optional[str]
|
phone: Optional[str]
|
||||||
email: Optional[str]
|
email: Optional[str]
|
||||||
|
address: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
class PartialOrder(BaseModel):
|
class PartialOrder(BaseModel):
|
||||||
|
|
@ -307,15 +308,3 @@ class PaymentRequest(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
message: Optional[str]
|
message: Optional[str]
|
||||||
payment_options: List[PaymentOption]
|
payment_options: List[PaymentOption]
|
||||||
|
|
||||||
def to_nostr_event(self, author_pubkey: str, to_pubkey: str) -> NostrEvent:
|
|
||||||
event = NostrEvent(
|
|
||||||
pubkey=author_pubkey,
|
|
||||||
created_at=round(time.time()),
|
|
||||||
kind=4,
|
|
||||||
tags=[["p", to_pubkey]],
|
|
||||||
content=json.dumps(self.dict(), separators=(",", ":"), ensure_ascii=False),
|
|
||||||
)
|
|
||||||
event.id = event.event_id
|
|
||||||
|
|
||||||
return event
|
|
||||||
|
|
|
||||||
85
tasks.py
85
tasks.py
|
|
@ -89,62 +89,67 @@ async def handle_message(msg: str):
|
||||||
if type.upper() == "EVENT":
|
if type.upper() == "EVENT":
|
||||||
event = NostrEvent(**event)
|
event = NostrEvent(**event)
|
||||||
if event.kind == 4:
|
if event.kind == 4:
|
||||||
merchant = await get_merchant_by_pubkey(public_key)
|
await handle_nip04_message(public_key, event)
|
||||||
assert merchant, f"Merchant not found for public key '{public_key}'"
|
|
||||||
|
|
||||||
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
|
|
||||||
dm_resp = await handle_dirrect_message(
|
|
||||||
event.pubkey, event.id, clear_text_msg
|
|
||||||
)
|
|
||||||
if dm_resp:
|
|
||||||
dm_event = merchant.build_dm_event(dm_resp, event.pubkey)
|
|
||||||
await publish_nostr_event(dm_event)
|
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.warning(ex)
|
logger.warning(ex)
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_nip04_message(public_key: str, event: NostrEvent):
|
||||||
|
merchant = await get_merchant_by_pubkey(public_key)
|
||||||
|
assert merchant, f"Merchant not found for public key '{public_key}'"
|
||||||
|
|
||||||
|
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
|
||||||
|
dm_resp = await handle_dirrect_message(event.pubkey, event.id, clear_text_msg)
|
||||||
|
if dm_resp:
|
||||||
|
dm_event = merchant.build_dm_event(dm_resp, event.pubkey)
|
||||||
|
await publish_nostr_event(dm_event)
|
||||||
|
|
||||||
|
|
||||||
async def handle_dirrect_message(
|
async def handle_dirrect_message(
|
||||||
from_pubkey: str, event_id: str, msg: str
|
from_pubkey: str, event_id: str, msg: str
|
||||||
) -> Optional[str]:
|
) -> Optional[str]:
|
||||||
order, text_msg = order_from_json(msg)
|
order, text_msg = order_from_json(msg)
|
||||||
try:
|
try:
|
||||||
if order:
|
if order:
|
||||||
### check that event_id not parsed already
|
|
||||||
order["pubkey"] = from_pubkey
|
order["pubkey"] = from_pubkey
|
||||||
order["event_id"] = event_id
|
order["event_id"] = event_id
|
||||||
partial_order = PartialOrder(**order)
|
return await handle_new_order(PartialOrder(**order))
|
||||||
partial_order.validate_order()
|
|
||||||
assert len(partial_order.items) != 0, "Order has no items. Order: " + msg
|
|
||||||
|
|
||||||
first_product_id = partial_order.items[0].product_id
|
|
||||||
wallet_id = await get_wallet_for_product(first_product_id)
|
|
||||||
assert (
|
|
||||||
wallet_id
|
|
||||||
), f"Cannot find wallet id for product id: {first_product_id}"
|
|
||||||
|
|
||||||
wallet = await get_wallet(wallet_id)
|
|
||||||
assert wallet, f"Cannot find wallet for product id: {first_product_id}"
|
|
||||||
|
|
||||||
market_url = url_for(f"/nostrmarket/api/v1/order", external=True)
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
resp = await client.post(
|
|
||||||
url=market_url,
|
|
||||||
headers={
|
|
||||||
"X-Api-Key": wallet.adminkey,
|
|
||||||
},
|
|
||||||
json=order,
|
|
||||||
)
|
|
||||||
resp.raise_for_status()
|
|
||||||
data = resp.json()
|
|
||||||
return (
|
|
||||||
json.dumps(data, separators=(",", ":"), ensure_ascii=False)
|
|
||||||
if data
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
print("### text_msg", text_msg)
|
print("### text_msg", text_msg)
|
||||||
return None
|
return None
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logger.warning(ex)
|
logger.warning(ex)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_new_order(order: PartialOrder):
|
||||||
|
### check that event_id not parsed already
|
||||||
|
|
||||||
|
order.validate_order()
|
||||||
|
assert (
|
||||||
|
len(order.items) != 0
|
||||||
|
), f"Order has no items. Order: '{order.id}' ({order.event_id})"
|
||||||
|
|
||||||
|
first_product_id = order.items[0].product_id
|
||||||
|
wallet_id = await get_wallet_for_product(first_product_id)
|
||||||
|
assert wallet_id, f"Cannot find wallet id for product id: {first_product_id}"
|
||||||
|
|
||||||
|
wallet = await get_wallet(wallet_id)
|
||||||
|
assert wallet, f"Cannot find wallet for product id: {first_product_id}"
|
||||||
|
|
||||||
|
market_url = url_for(f"/nostrmarket/api/v1/order", external=True)
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
resp = await client.post(
|
||||||
|
url=market_url,
|
||||||
|
headers={
|
||||||
|
"X-Api-Key": wallet.adminkey,
|
||||||
|
},
|
||||||
|
json=order.dict(),
|
||||||
|
)
|
||||||
|
resp.raise_for_status()
|
||||||
|
data = resp.json()
|
||||||
|
if data:
|
||||||
|
return json.dumps(data, separators=(",", ":"), ensure_ascii=False)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue