parent
d656d41b90
commit
a8eb139360
25 changed files with 3192 additions and 237 deletions
21
router.py
21
router.py
|
|
@ -3,18 +3,17 @@ import json
|
|||
from typing import Dict, List
|
||||
|
||||
from fastapi import WebSocket, WebSocketDisconnect
|
||||
from loguru import logger
|
||||
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
from loguru import logger
|
||||
|
||||
from . import nostr_client
|
||||
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
|
||||
|
||||
|
||||
class NostrRouter:
|
||||
received_subscription_events: dict[str, List[EventMessage]] = {}
|
||||
received_subscription_notices: list[NoticeMessage] = []
|
||||
received_subscription_eosenotices: dict[str, EndOfStoredEventsMessage] = {}
|
||||
received_subscription_events: dict[str, List[EventMessage]]
|
||||
received_subscription_notices: list[NoticeMessage]
|
||||
received_subscription_eosenotices: dict[str, EndOfStoredEventsMessage]
|
||||
|
||||
def __init__(self, websocket: WebSocket):
|
||||
self.connected: bool = True
|
||||
|
|
@ -61,7 +60,7 @@ class NostrRouter:
|
|||
try:
|
||||
await self._handle_client_to_nostr(json_str)
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to handle client message: '{str(e)}'.")
|
||||
logger.debug(f"Failed to handle client message: '{e!s}'.")
|
||||
|
||||
async def _nostr_to_client(self):
|
||||
"""Sends responses from relays back to the client."""
|
||||
|
|
@ -70,10 +69,9 @@ class NostrRouter:
|
|||
await self._handle_subscriptions()
|
||||
self._handle_notices()
|
||||
except Exception as e:
|
||||
logger.debug(f"Failed to handle response for client: '{str(e)}'.")
|
||||
logger.debug(f"Failed to handle response for client: '{e!s}'.")
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def _handle_subscriptions(self):
|
||||
for s in self.subscriptions:
|
||||
if s in NostrRouter.received_subscription_events:
|
||||
|
|
@ -155,6 +153,11 @@ class NostrRouter:
|
|||
if subscription_id_rewritten:
|
||||
self.original_subscription_ids.pop(subscription_id_rewritten)
|
||||
nostr_client.relay_manager.close_subscription(subscription_id_rewritten)
|
||||
logger.info(f"Unsubscribe from '{subscription_id_rewritten}'. Original id: '{subscription_id}.'")
|
||||
logger.info(
|
||||
f"""
|
||||
Unsubscribe from '{subscription_id_rewritten}'.
|
||||
Original id: '{subscription_id}.'
|
||||
"""
|
||||
)
|
||||
else:
|
||||
logger.info(f"Failed to unsubscribe from '{subscription_id}.'")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue