refactor: reorder and rename methods
This commit is contained in:
parent
f6b5034e74
commit
fb888c1c3d
2 changed files with 30 additions and 24 deletions
|
|
@ -42,7 +42,7 @@ def nostrmarket_start():
|
||||||
async def _subscribe_to_nostr_client():
|
async def _subscribe_to_nostr_client():
|
||||||
# wait for 'nostrclient' extension to initialize
|
# wait for 'nostrclient' extension to initialize
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
await nostr_client.subscribe_to_nostr_client()
|
await nostr_client.run_forever()
|
||||||
|
|
||||||
async def _wait_for_nostr_events():
|
async def _wait_for_nostr_events():
|
||||||
# wait for this extension to initialize
|
# wait for this extension to initialize
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,6 @@ class NostrClient:
|
||||||
self.send_req_queue: Queue = Queue()
|
self.send_req_queue: Queue = Queue()
|
||||||
self.ws: WebSocketApp = None
|
self.ws: WebSocketApp = None
|
||||||
|
|
||||||
async def get_event(self):
|
|
||||||
return await self.recieve_event_queue.get()
|
|
||||||
|
|
||||||
async def publish_nostr_event(self, e: NostrEvent):
|
|
||||||
print("### publish_nostr_event", e.dict())
|
|
||||||
await self.send_req_queue.put(["EVENT", e.dict()])
|
|
||||||
|
|
||||||
async def connect_to_nostrclient_ws(
|
async def connect_to_nostrclient_ws(
|
||||||
self, on_open: Callable, on_message: Callable
|
self, on_open: Callable, on_message: Callable
|
||||||
) -> WebSocketApp:
|
) -> WebSocketApp:
|
||||||
|
|
@ -45,23 +38,10 @@ class NostrClient:
|
||||||
|
|
||||||
return ws
|
return ws
|
||||||
|
|
||||||
async def subscribe_to_direct_messages(self, public_key: str, since: int):
|
async def get_event(self):
|
||||||
in_messages_filter = {"kind": 4, "#p": [public_key]}
|
return await self.recieve_event_queue.get()
|
||||||
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 self.send_req_queue.put(
|
async def run_forever(self):
|
||||||
["REQ", f"direct-messages-in:{public_key}", in_messages_filter]
|
|
||||||
)
|
|
||||||
await self.send_req_queue.put(
|
|
||||||
["REQ", f"direct-messages-out:{public_key}", out_messages_filter]
|
|
||||||
)
|
|
||||||
|
|
||||||
async def subscribe_to_nostr_client(self):
|
|
||||||
def on_open(_):
|
def on_open(_):
|
||||||
logger.info("Connected to 'nostrclient' websocket")
|
logger.info("Connected to 'nostrclient' websocket")
|
||||||
|
|
||||||
|
|
@ -85,6 +65,32 @@ class NostrClient:
|
||||||
self.ws = None # todo close
|
self.ws = None # todo close
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
async def publish_nostr_event(self, e: NostrEvent):
|
||||||
|
print("### publish_nostr_event", e.dict())
|
||||||
|
await self.send_req_queue.put(["EVENT", e.dict()])
|
||||||
|
|
||||||
|
async def subscribe_to_direct_messages(self, 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 self.send_req_queue.put(
|
||||||
|
["REQ", f"direct-messages-in:{public_key}", in_messages_filter]
|
||||||
|
)
|
||||||
|
await self.send_req_queue.put(
|
||||||
|
["REQ", f"direct-messages-out:{public_key}", out_messages_filter]
|
||||||
|
)
|
||||||
|
|
||||||
async def unsubscribe_from_direct_messages(self, public_key: str):
|
async def unsubscribe_from_direct_messages(self, public_key: str):
|
||||||
await self.send_req_queue.put(["CLOSE", f"direct-messages-in:{public_key}"])
|
await self.send_req_queue.put(["CLOSE", f"direct-messages-in:{public_key}"])
|
||||||
await self.send_req_queue.put(["CLOSE", f"direct-messages-out:{public_key}"])
|
await self.send_req_queue.put(["CLOSE", f"direct-messages-out:{public_key}"])
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
try:
|
||||||
|
self.ws.close()
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue