Stabilize (#24)

* refactor: clean-up

* refactor: extra logs plus try-catch

* refactor: do not use bare `except`

* refactor: clean-up redundant fields

* chore: pass code checks

* chore: code format

* refactor: code clean-up

* fix: refactoring stuff

* refactor: remove un-used file

* chore: code clean-up

* chore: code clean-up

* chore: code-format fix

* refactor: remove nostr.client wrapper

* refactor: code clean-up

* chore: code format

* refactor: remove `RelayList` class

* refactor: extract smaller methods with try-catch

* fix: better exception handling

* fix: remove redundant filters

* fix: simplify event

* chore: code format

* fix: code check

* fix: code check

* fix: simplify `REQ`

* fix: more clean-ups

* refactor: use simpler method

* refactor: re-order and rename

* fix: stop logic

* fix: subscription close before disconnect

* chore: play commit
This commit is contained in:
Vlad Stan 2023-11-01 17:46:42 +02:00 committed by GitHub
parent ab185bd2c4
commit 16ae9d15a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 522 additions and 717 deletions

View file

@ -3,75 +3,69 @@ import threading
from loguru import logger
from . import nostr
from . import nostr_client
from .crud import get_relays
from .nostr.message_pool import EndOfStoredEventsMessage, EventMessage, NoticeMessage
from .router import NostrRouter, nostr
from .router import NostrRouter
async def init_relays():
# reinitialize the entire client
nostr.__init__()
# get relays from db
relays = await get_relays()
# set relays and connect to them
nostr.client.relays = list(set([r.url for r in relays.__root__ if r.url]))
await nostr.client.connect()
valid_relays = list(set([r.url for r in relays if r.url]))
nostr_client.reconnect(valid_relays)
async def check_relays():
""" Check relays that have been disconnected """
"""Check relays that have been disconnected"""
while True:
try:
await asyncio.sleep(20)
nostr.client.relay_manager.check_and_restart_relays()
nostr_client.relay_manager.check_and_restart_relays()
except Exception as e:
logger.warning(f"Cannot restart relays: '{str(e)}'.")
async def subscribe_events():
while not any([r.connected for r in nostr.client.relay_manager.relays.values()]):
while not any([r.connected for r in nostr_client.relay_manager.relays.values()]):
await asyncio.sleep(2)
def callback_events(eventMessage: EventMessage):
if eventMessage.subscription_id in NostrRouter.received_subscription_events:
# do not add duplicate events (by event id)
if eventMessage.event.id in set(
[
e.id
for e in NostrRouter.received_subscription_events[eventMessage.subscription_id]
]
):
return
sub_id = eventMessage.subscription_id
if sub_id not in NostrRouter.received_subscription_events:
NostrRouter.received_subscription_events[sub_id] = [eventMessage]
return
NostrRouter.received_subscription_events[eventMessage.subscription_id].append(
eventMessage.event
)
else:
NostrRouter.received_subscription_events[eventMessage.subscription_id] = [
eventMessage.event
]
return
# do not add duplicate events (by event id)
ids = set(
[e.event_id for e in NostrRouter.received_subscription_events[sub_id]]
)
if eventMessage.event_id in ids:
return
NostrRouter.received_subscription_events[sub_id].append(eventMessage)
def callback_notices(noticeMessage: NoticeMessage):
if noticeMessage not in NostrRouter.received_subscription_notices:
NostrRouter.received_subscription_notices.append(noticeMessage)
return
def callback_eose_notices(eventMessage: EndOfStoredEventsMessage):
if eventMessage.subscription_id not in NostrRouter.received_subscription_eosenotices:
NostrRouter.received_subscription_eosenotices[
eventMessage.subscription_id
] = eventMessage
sub_id = eventMessage.subscription_id
if sub_id in NostrRouter.received_subscription_eosenotices:
return
return
NostrRouter.received_subscription_eosenotices[sub_id] = eventMessage
def wrap_async_subscribe():
asyncio.run(nostr.client.subscribe(
callback_events,
callback_notices,
callback_eose_notices,
))
asyncio.run(
nostr_client.subscribe(
callback_events,
callback_notices,
callback_eose_notices,
)
)
t = threading.Thread(
target=wrap_async_subscribe,