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

@ -2,13 +2,15 @@ import json
from queue import Queue
from threading import Lock
from .event import Event
from .message_type import RelayMessageType
class EventMessage:
def __init__(self, event: Event, subscription_id: str, url: str) -> None:
def __init__(
self, event: str, event_id: str, subscription_id: str, url: str
) -> None:
self.event = event
self.event_id = event_id
self.subscription_id = subscription_id
self.url = url
@ -59,18 +61,16 @@ class MessagePool:
message_type = message_json[0]
if message_type == RelayMessageType.EVENT:
subscription_id = message_json[1]
e = message_json[2]
event = Event(
e["content"],
e["pubkey"],
e["created_at"],
e["kind"],
e["tags"],
e["sig"],
)
event = message_json[2]
if "id" not in event:
return
event_id = event["id"]
with self.lock:
if not f"{subscription_id}_{event.id}" in self._unique_events:
self._accept_event(EventMessage(event, subscription_id, url))
if f"{subscription_id}_{event_id}" not in self._unique_events:
self._accept_event(
EventMessage(json.dumps(event), event_id, subscription_id, url)
)
elif message_type == RelayMessageType.NOTICE:
self.notices.put(NoticeMessage(message_json[1], url))
elif message_type == RelayMessageType.END_OF_STORED_EVENTS:
@ -78,10 +78,12 @@ class MessagePool:
def _accept_event(self, event_message: EventMessage):
"""
Event uniqueness is considered per `subscription_id`.
The `subscription_id` is rewritten to be unique and it is the same accross relays.
The same event can come from different subscriptions (from the same client or from different ones).
Clients that have joined later should receive older events.
Event uniqueness is considered per `subscription_id`. The `subscription_id` is
rewritten to be unique and it is the same accross relays. The same event can
come from different subscriptions (from the same client or from different ones).
Clients that have joined later should receive older events.
"""
self.events.put(event_message)
self._unique_events.add(f"{event_message.subscription_id}_{event_message.event.id}")
self._unique_events.add(
f"{event_message.subscription_id}_{event_message.event_id}"
)