From 6498f1aa965c8d504238acc0c0ae10713ea42c8e Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 15 Mar 2023 23:08:37 +0200 Subject: [PATCH] fix: create invoice for fresh merchant --- __init__.py | 2 +- crud.py | 2 +- nostr/nostr_client.py | 22 +++++++++++++++++++ .../merchant-details/merchant-details.js | 1 + tasks.py | 17 +++----------- views_api.py | 17 +++++++++++--- 6 files changed, 42 insertions(+), 19 deletions(-) diff --git a/__init__.py b/__init__.py index 4a2d0e7..6f753a2 100644 --- a/__init__.py +++ b/__init__.py @@ -49,7 +49,7 @@ def nostrmarket_start(): async def _wait_for_nostr_events(): # wait for this extension to initialize await asyncio.sleep(5) - await wait_for_nostr_events(recieve_event_queue, send_req_queue) + await wait_for_nostr_events(recieve_event_queue) loop = asyncio.get_event_loop() task1 = loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) diff --git a/crud.py b/crud.py index f3b7fa6..92bb703 100644 --- a/crud.py +++ b/crud.py @@ -73,7 +73,7 @@ async def get_merchant_for_user(user_id: str) -> Optional[Merchant]: return Merchant.from_row(row) if row else None -async def delete_merchants(merchant_id: str) -> None: +async def delete_merchant(merchant_id: str) -> None: await db.execute( "DELETE FROM nostrmarket.merchants WHERE id = ?", (merchant_id,), diff --git a/nostr/nostr_client.py b/nostr/nostr_client.py index d5bfd7a..87682f5 100644 --- a/nostr/nostr_client.py +++ b/nostr/nostr_client.py @@ -34,3 +34,25 @@ async def connect_to_nostrclient_ws( wst.start() return ws + + +async def subscribe_to_direct_messages(public_key: str, since: int): + in_messages_filter = {"kind": 4, "#p": [public_key]} + out_messages_filter = {"kind": 4, "authors": [public_key]} + if since != 0: + in_messages_filter["since"] = since + out_messages_filter["since"] = since + print("### in_messages_filter", in_messages_filter) + print("### out_messages_filter", out_messages_filter) + + await send_req_queue.put( + ["REQ", f"direct-messages-in:{public_key}", in_messages_filter] + ) + await send_req_queue.put( + ["REQ", f"direct-messages-out:{public_key}", out_messages_filter] + ) + + +async def unsubscribe_from_direct_messages(public_key: str): + await send_req_queue.put(["CLOSE", f"direct-messages-in:{public_key}"]) + await send_req_queue.put(["CLOSE", f"direct-messages-out:{public_key}"]) diff --git a/static/components/merchant-details/merchant-details.js b/static/components/merchant-details/merchant-details.js index bc53077..1327ba5 100644 --- a/static/components/merchant-details/merchant-details.js +++ b/static/components/merchant-details/merchant-details.js @@ -30,6 +30,7 @@ async function merchantDetails(path) { '/nostrmarket/api/v1/merchant/' + this.merchantId, this.adminkey ) + // todo: refresh parent page this.$emit('merchant-deleted', this.merchantId) this.$q.notify({ type: 'positive', diff --git a/tasks.py b/tasks.py index 57cc36f..bedd683 100644 --- a/tasks.py +++ b/tasks.py @@ -14,7 +14,7 @@ from .crud import ( get_last_order_time, get_public_keys_for_merchants, ) -from .nostr.nostr_client import connect_to_nostrclient_ws +from .nostr.nostr_client import connect_to_nostrclient_ws, subscribe_to_direct_messages from .services import handle_order_paid, process_nostr_message @@ -65,25 +65,14 @@ async def subscribe_to_nostr_client(recieve_event_queue: Queue, send_req_queue: await asyncio.sleep(5) -async def wait_for_nostr_events(recieve_event_queue: Queue, send_req_queue: Queue): +async def wait_for_nostr_events(recieve_event_queue: Queue): public_keys = await get_public_keys_for_merchants() for p in public_keys: last_order_time = await get_last_order_time(p) last_dm_time = await get_last_direct_messages_time(p) since = max(last_order_time, last_dm_time) - in_messages_filter = {"kind": 4, "#p": [p]} - out_messages_filter = {"kind": 4, "authors": [p]} - if since != 0: - in_messages_filter["since"] = since - # out_messages_filter["since"] = since - print("### in_messages_filter", in_messages_filter) - print("### out_messages_filter", out_messages_filter) - - await send_req_queue.put(["REQ", f"direct-messages-in:{p}", in_messages_filter]) - await send_req_queue.put( - ["REQ", f"direct-messages-out:{p}", out_messages_filter] - ) + await subscribe_to_direct_messages(p, since) while True: message = await recieve_event_queue.get() diff --git a/views_api.py b/views_api.py index e3a8982..ba3c0f6 100644 --- a/views_api.py +++ b/views_api.py @@ -22,12 +22,12 @@ from .crud import ( create_product, create_stall, create_zone, + delete_merchant, delete_merchant_direct_messages, delete_merchant_orders, delete_merchant_products, delete_merchant_stalls, delete_merchant_zones, - delete_merchants, delete_product, delete_stall, delete_zone, @@ -61,7 +61,11 @@ from .models import ( Stall, Zone, ) -from .nostr.nostr_client import publish_nostr_event +from .nostr.nostr_client import ( + publish_nostr_event, + subscribe_to_direct_messages, + unsubscribe_from_direct_messages, +) from .services import sign_and_send_to_nostr ######################################## MERCHANT ######################################## @@ -74,7 +78,11 @@ async def api_create_merchant( ) -> Merchant: try: + merchant = await get_merchant_for_user(wallet.wallet.user) + assert merchant == None, "A merchant already exists for this user" + merchant = await create_merchant(wallet.wallet.user, data) + await subscribe_to_direct_messages(data.public_key, 0) return merchant except Exception as ex: @@ -117,7 +125,9 @@ async def api_delete_merchant( await delete_merchant_stalls(merchant.id) await delete_merchant_direct_messages(merchant.id) await delete_merchant_zones(merchant.id) - await delete_merchants(merchant.id) + + await unsubscribe_from_direct_messages(merchant.public_key) + await delete_merchant(merchant.id) except Exception as ex: logger.warning(ex) raise HTTPException( @@ -646,4 +656,5 @@ async def api_stop(wallet: WalletTypeInfo = Depends(check_admin)): except Exception as ex: logger.warning(ex) + # todo: close websocket return {"success": True}