refactor: make some fields static
This commit is contained in:
parent
cd1e830947
commit
e35dafe056
2 changed files with 21 additions and 25 deletions
23
services.py
23
services.py
|
|
@ -13,10 +13,6 @@ from .nostr.filter import Filter as NostrFilter
|
||||||
from .nostr.filter import Filters as NostrFilters
|
from .nostr.filter import Filters as NostrFilters
|
||||||
from .nostr.message_pool import EndOfStoredEventsMessage, NoticeMessage
|
from .nostr.message_pool import EndOfStoredEventsMessage, NoticeMessage
|
||||||
|
|
||||||
received_subscription_events: dict[str, list[Event]] = {}
|
|
||||||
received_subscription_notices: list[NoticeMessage] = []
|
|
||||||
received_subscription_eosenotices: dict[str, EndOfStoredEventsMessage] = {}
|
|
||||||
|
|
||||||
|
|
||||||
class NostrClient:
|
class NostrClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -27,6 +23,11 @@ nostr = NostrClient()
|
||||||
|
|
||||||
|
|
||||||
class NostrRouter:
|
class NostrRouter:
|
||||||
|
|
||||||
|
received_subscription_events: dict[str, list[Event]] = {}
|
||||||
|
received_subscription_notices: list[NoticeMessage] = []
|
||||||
|
received_subscription_eosenotices: dict[str, EndOfStoredEventsMessage] = {}
|
||||||
|
|
||||||
def __init__(self, websocket):
|
def __init__(self, websocket):
|
||||||
self.subscriptions: List[str] = []
|
self.subscriptions: List[str] = []
|
||||||
self.connected: bool = True
|
self.connected: bool = True
|
||||||
|
|
@ -82,9 +83,9 @@ class NostrRouter:
|
||||||
|
|
||||||
async def _handle_subscriptions(self):
|
async def _handle_subscriptions(self):
|
||||||
for s in self.subscriptions:
|
for s in self.subscriptions:
|
||||||
if s in received_subscription_events:
|
if s in NostrRouter.received_subscription_events:
|
||||||
await self._handle_received_subscription_events(s)
|
await self._handle_received_subscription_events(s)
|
||||||
if s in received_subscription_eosenotices:
|
if s in NostrRouter.received_subscription_eosenotices:
|
||||||
await self._handle_received_subscription_eosenotices(s)
|
await self._handle_received_subscription_eosenotices(s)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -92,13 +93,13 @@ class NostrRouter:
|
||||||
async def _handle_received_subscription_eosenotices(self, s):
|
async def _handle_received_subscription_eosenotices(self, s):
|
||||||
s_original = self.original_subscription_ids[s]
|
s_original = self.original_subscription_ids[s]
|
||||||
event_to_forward = ["EOSE", s_original]
|
event_to_forward = ["EOSE", s_original]
|
||||||
del received_subscription_eosenotices[s]
|
del NostrRouter.received_subscription_eosenotices[s]
|
||||||
|
|
||||||
await self.websocket.send_text(json.dumps(event_to_forward))
|
await self.websocket.send_text(json.dumps(event_to_forward))
|
||||||
|
|
||||||
async def _handle_received_subscription_events(self, s):
|
async def _handle_received_subscription_events(self, s):
|
||||||
while len(received_subscription_events[s]):
|
while len(NostrRouter.received_subscription_events[s]):
|
||||||
my_event = received_subscription_events[s].pop(0)
|
my_event = NostrRouter.received_subscription_events[s].pop(0)
|
||||||
# event.to_message() does not include the subscription ID, we have to add it manually
|
# event.to_message() does not include the subscription ID, we have to add it manually
|
||||||
event_json = {
|
event_json = {
|
||||||
"id": my_event.id,
|
"id": my_event.id,
|
||||||
|
|
@ -117,8 +118,8 @@ class NostrRouter:
|
||||||
await self.websocket.send_text(json.dumps(event_to_forward))
|
await self.websocket.send_text(json.dumps(event_to_forward))
|
||||||
|
|
||||||
def _handle_notices(self):
|
def _handle_notices(self):
|
||||||
while len(received_subscription_notices):
|
while len(NostrRouter.received_subscription_notices):
|
||||||
my_event = received_subscription_notices.pop(0)
|
my_event = NostrRouter.received_subscription_notices.pop(0)
|
||||||
event_to_forward = ["NOTICE", my_event.content]
|
event_to_forward = ["NOTICE", my_event.content]
|
||||||
# note: we don't send it to the user because we don't know who should receive it
|
# note: we don't send it to the user because we don't know who should receive it
|
||||||
logger.debug("Nostrclient: Received notice: ", event_to_forward[1])
|
logger.debug("Nostrclient: Received notice: ", event_to_forward[1])
|
||||||
|
|
|
||||||
23
tasks.py
23
tasks.py
|
|
@ -3,12 +3,7 @@ import threading
|
||||||
|
|
||||||
from .crud import get_relays
|
from .crud import get_relays
|
||||||
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
|
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
|
||||||
from .services import (
|
from .services import NostrRouter, nostr
|
||||||
nostr,
|
|
||||||
received_subscription_eosenotices,
|
|
||||||
received_subscription_events,
|
|
||||||
received_subscription_notices,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def init_relays():
|
async def init_relays():
|
||||||
|
|
@ -26,33 +21,33 @@ async def subscribe_events():
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
def callback_events(eventMessage: EventMessage):
|
def callback_events(eventMessage: EventMessage):
|
||||||
if eventMessage.subscription_id in received_subscription_events:
|
if eventMessage.subscription_id in NostrRouter.received_subscription_events:
|
||||||
# do not add duplicate events (by event id)
|
# do not add duplicate events (by event id)
|
||||||
if eventMessage.event.id in set(
|
if eventMessage.event.id in set(
|
||||||
[
|
[
|
||||||
e.id
|
e.id
|
||||||
for e in received_subscription_events[eventMessage.subscription_id]
|
for e in NostrRouter.received_subscription_events[eventMessage.subscription_id]
|
||||||
]
|
]
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
received_subscription_events[eventMessage.subscription_id].append(
|
NostrRouter.received_subscription_events[eventMessage.subscription_id].append(
|
||||||
eventMessage.event
|
eventMessage.event
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
received_subscription_events[eventMessage.subscription_id] = [
|
NostrRouter.received_subscription_events[eventMessage.subscription_id] = [
|
||||||
eventMessage.event
|
eventMessage.event
|
||||||
]
|
]
|
||||||
return
|
return
|
||||||
|
|
||||||
def callback_notices(noticeMessage: NoticeMessage):
|
def callback_notices(noticeMessage: NoticeMessage):
|
||||||
if noticeMessage not in received_subscription_notices:
|
if noticeMessage not in NostrRouter.received_subscription_notices:
|
||||||
received_subscription_notices.append(noticeMessage)
|
NostrRouter.received_subscription_notices.append(noticeMessage)
|
||||||
return
|
return
|
||||||
|
|
||||||
def callback_eose_notices(eventMessage: EndOfStoredEventsMessage):
|
def callback_eose_notices(eventMessage: EndOfStoredEventsMessage):
|
||||||
if eventMessage.subscription_id not in received_subscription_eosenotices:
|
if eventMessage.subscription_id not in NostrRouter.received_subscription_eosenotices:
|
||||||
received_subscription_eosenotices[
|
NostrRouter.received_subscription_eosenotices[
|
||||||
eventMessage.subscription_id
|
eventMessage.subscription_id
|
||||||
] = eventMessage
|
] = eventMessage
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue