feat: extract nostr_client class
This commit is contained in:
parent
45c7744282
commit
f6b5034e74
5 changed files with 90 additions and 97 deletions
|
|
@ -1,3 +1,6 @@
|
|||
import asyncio
|
||||
import json
|
||||
from asyncio import Queue
|
||||
from threading import Thread
|
||||
from typing import Callable
|
||||
|
||||
|
|
@ -6,53 +9,82 @@ from websocket import WebSocketApp
|
|||
|
||||
from lnbits.app import settings
|
||||
|
||||
from .. import send_req_queue
|
||||
from .event import NostrEvent
|
||||
|
||||
|
||||
async def publish_nostr_event(e: NostrEvent):
|
||||
print("### publish_nostr_event", e.dict())
|
||||
await send_req_queue.put(["EVENT", e.dict()])
|
||||
class NostrClient:
|
||||
def __init__(self):
|
||||
self.recieve_event_queue: Queue = Queue()
|
||||
self.send_req_queue: Queue = Queue()
|
||||
self.ws: WebSocketApp = None
|
||||
|
||||
async def get_event(self):
|
||||
return await self.recieve_event_queue.get()
|
||||
|
||||
async def connect_to_nostrclient_ws(
|
||||
on_open: Callable, on_message: Callable
|
||||
) -> WebSocketApp:
|
||||
def on_error(_, error):
|
||||
logger.warning(error)
|
||||
async def publish_nostr_event(self, e: NostrEvent):
|
||||
print("### publish_nostr_event", e.dict())
|
||||
await self.send_req_queue.put(["EVENT", e.dict()])
|
||||
|
||||
logger.debug(f"Subscribing to websockets for nostrclient extension")
|
||||
ws = WebSocketApp(
|
||||
f"ws://localhost:{settings.port}/nostrclient/api/v1/relay",
|
||||
on_message=on_message,
|
||||
on_open=on_open,
|
||||
on_error=on_error,
|
||||
)
|
||||
async def connect_to_nostrclient_ws(
|
||||
self, on_open: Callable, on_message: Callable
|
||||
) -> WebSocketApp:
|
||||
def on_error(_, error):
|
||||
logger.warning(error)
|
||||
|
||||
wst = Thread(target=ws.run_forever)
|
||||
wst.daemon = True
|
||||
wst.start()
|
||||
logger.debug(f"Subscribing to websockets for nostrclient extension")
|
||||
ws = WebSocketApp(
|
||||
f"ws://localhost:{settings.port}/nostrclient/api/v1/relay",
|
||||
on_message=on_message,
|
||||
on_open=on_open,
|
||||
on_error=on_error,
|
||||
)
|
||||
|
||||
return ws
|
||||
wst = Thread(target=ws.run_forever)
|
||||
wst.daemon = True
|
||||
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)
|
||||
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 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]
|
||||
)
|
||||
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 subscribe_to_nostr_client(self):
|
||||
def on_open(_):
|
||||
logger.info("Connected to 'nostrclient' websocket")
|
||||
|
||||
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}"])
|
||||
def on_message(_, message):
|
||||
# print("### on_message", message)
|
||||
self.recieve_event_queue.put_nowait(message)
|
||||
|
||||
while True:
|
||||
try:
|
||||
req = None
|
||||
if not self.ws:
|
||||
self.ws = await self.connect_to_nostrclient_ws(on_open, on_message)
|
||||
# be sure the connection is open
|
||||
await asyncio.sleep(3)
|
||||
req = await self.send_req_queue.get()
|
||||
self.ws.send(json.dumps(req))
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
if req:
|
||||
await self.send_req_queue.put(req)
|
||||
self.ws = None # todo close
|
||||
await asyncio.sleep(5)
|
||||
|
||||
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-out:{public_key}"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue