fix: add extra checks (#21)

* fix: add extra checks

* fix: remove redundant try-catch
This commit is contained in:
Vlad Stan 2023-09-25 10:13:55 +03:00 committed by GitHub
parent 6f5e9e3458
commit e841183c10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -89,31 +89,41 @@ 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] try:
event_to_forward = ["EOSE", s_original] if s not in self.original_subscription_ids:
del NostrRouter.received_subscription_eosenotices[s] return
s_original = self.original_subscription_ids[s]
await self.websocket.send_text(json.dumps(event_to_forward)) event_to_forward = ["EOSE", s_original]
del NostrRouter.received_subscription_eosenotices[s]
await self.websocket.send_text(json.dumps(event_to_forward))
except Exception as e:
logger.debug(e)
async def _handle_received_subscription_events(self, s): async def _handle_received_subscription_events(self, s):
while len(NostrRouter.received_subscription_events[s]): try:
my_event = NostrRouter.received_subscription_events[s].pop(0) if s not in NostrRouter.received_subscription_events:
# event.to_message() does not include the subscription ID, we have to add it manually return
event_json = { while len(NostrRouter.received_subscription_events[s]):
"id": my_event.id, my_event = NostrRouter.received_subscription_events[s].pop(0)
"pubkey": my_event.public_key, # event.to_message() does not include the subscription ID, we have to add it manually
"created_at": my_event.created_at, event_json = {
"kind": my_event.kind, "id": my_event.id,
"tags": my_event.tags, "pubkey": my_event.public_key,
"content": my_event.content, "created_at": my_event.created_at,
"sig": my_event.signature, "kind": my_event.kind,
} "tags": my_event.tags,
"content": my_event.content,
"sig": my_event.signature,
}
# this reconstructs the original response from the relay # this reconstructs the original response from the relay
# reconstruct original subscription id # reconstruct original subscription id
s_original = self.original_subscription_ids[s] s_original = self.original_subscription_ids[s]
event_to_forward = ["EVENT", s_original, event_json] event_to_forward = ["EVENT", s_original, event_json]
await self.websocket.send_text(json.dumps(event_to_forward)) await self.websocket.send_text(json.dumps(event_to_forward))
except Exception as e:
logger.debug(e)
def _handle_notices(self): def _handle_notices(self):
while len(NostrRouter.received_subscription_notices): while len(NostrRouter.received_subscription_notices):
@ -121,7 +131,7 @@ class NostrRouter:
# 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.info(f"Relay ('{my_event.url}') notice: '{my_event.content}']") logger.info(f"Relay ('{my_event.url}') notice: '{my_event.content}']")
nostr.client.relay_manager.handle_notice(my_event) nostr.client.relay_manager.handle_notice(my_event)
def _marshall_nostr_filters(self, data: Union[dict, list]): def _marshall_nostr_filters(self, data: Union[dict, list]):