feat: listen for direct messages

This commit is contained in:
Vlad Stan 2023-03-03 18:24:53 +02:00
parent 9a82577493
commit cec7d2ee25
6 changed files with 142 additions and 11 deletions

View file

@ -1,18 +1,25 @@
import asyncio
import json
import threading
from asyncio import Queue
import httpx
import websocket
from loguru import logger
from websocket import WebSocketApp
from lnbits.core.models import Payment
from lnbits.helpers import url_for
from lnbits.tasks import register_invoice_listener
from .crud import get_merchant, get_merchant_by_pubkey, get_public_keys_for_merchants
from .nostr.event import NostrEvent
from .nostr.nostr_client import connect_to_nostrclient_ws
recieve_event_queue: Queue = Queue()
send_req_queue: Queue = Queue()
async def wait_for_paid_invoices():
invoice_queue = asyncio.Queue()
invoice_queue = Queue()
register_invoice_listener(invoice_queue)
while True:
@ -27,5 +34,61 @@ async def on_invoice_paid(payment: Payment) -> None:
print("### on_invoice_paid")
async def subscribe_nostrclient_ws():
async def subscribe_nostrclient():
print("### subscribe_nostrclient_ws")
def on_open(_):
logger.info("Connected to 'nostrclient' websocket")
def on_message(_, message):
print("### on_message", message)
recieve_event_queue.put_nowait(message)
# wait for 'nostrclient' extension to initialize
await asyncio.sleep(5)
ws: WebSocketApp = None
while True:
try:
req = None
if not ws:
ws = await connect_to_nostrclient_ws(on_open, on_message)
# be sure the connection is open
await asyncio.sleep(3)
req = await send_req_queue.get()
print("### req", req)
ws.send(json.dumps(req))
except Exception as ex:
logger.warning(ex)
if req:
await send_req_queue.put(req)
ws = None # todo close
await asyncio.sleep(5)
async def wait_for_nostr_events():
public_keys = await get_public_keys_for_merchants()
for p in public_keys:
await send_req_queue.put(
["REQ", f"direct-messages:{p}", {"kind": 4, "#p": [p]}]
)
while True:
message = await recieve_event_queue.get()
await handle_message(message)
async def handle_message(msg: str):
try:
type, subscription_id, event = json.loads(msg)
_, public_key = subscription_id.split(":")
if type.upper() == "EVENT":
event = NostrEvent(**event)
if event.kind == 4:
merchant = await get_merchant_by_pubkey(public_key)
if not merchant:
return
clear_text_msg = merchant.decrypt_message(event.content, event.pubkey)
print("### clear_text_msg", clear_text_msg)
except Exception as ex:
logger.warning(ex)