Skip to content

Commit e841183

Browse files
authored
fix: add extra checks (#21)
* fix: add extra checks * fix: remove redundant try-catch
1 parent 6f5e9e3 commit e841183

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

router.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,39 +89,49 @@ async def _handle_subscriptions(self):
8989

9090

9191
async def _handle_received_subscription_eosenotices(self, s):
92-
s_original = self.original_subscription_ids[s]
93-
event_to_forward = ["EOSE", s_original]
94-
del NostrRouter.received_subscription_eosenotices[s]
95-
96-
await self.websocket.send_text(json.dumps(event_to_forward))
97-
98-
async def _handle_received_subscription_events(self, s):
99-
while len(NostrRouter.received_subscription_events[s]):
100-
my_event = NostrRouter.received_subscription_events[s].pop(0)
101-
# event.to_message() does not include the subscription ID, we have to add it manually
102-
event_json = {
103-
"id": my_event.id,
104-
"pubkey": my_event.public_key,
105-
"created_at": my_event.created_at,
106-
"kind": my_event.kind,
107-
"tags": my_event.tags,
108-
"content": my_event.content,
109-
"sig": my_event.signature,
110-
}
111-
112-
# this reconstructs the original response from the relay
113-
# reconstruct original subscription id
92+
try:
93+
if s not in self.original_subscription_ids:
94+
return
11495
s_original = self.original_subscription_ids[s]
115-
event_to_forward = ["EVENT", s_original, event_json]
96+
event_to_forward = ["EOSE", s_original]
97+
del NostrRouter.received_subscription_eosenotices[s]
98+
11699
await self.websocket.send_text(json.dumps(event_to_forward))
100+
except Exception as e:
101+
logger.debug(e)
102+
103+
async def _handle_received_subscription_events(self, s):
104+
try:
105+
if s not in NostrRouter.received_subscription_events:
106+
return
107+
while len(NostrRouter.received_subscription_events[s]):
108+
my_event = NostrRouter.received_subscription_events[s].pop(0)
109+
# event.to_message() does not include the subscription ID, we have to add it manually
110+
event_json = {
111+
"id": my_event.id,
112+
"pubkey": my_event.public_key,
113+
"created_at": my_event.created_at,
114+
"kind": my_event.kind,
115+
"tags": my_event.tags,
116+
"content": my_event.content,
117+
"sig": my_event.signature,
118+
}
119+
120+
# this reconstructs the original response from the relay
121+
# reconstruct original subscription id
122+
s_original = self.original_subscription_ids[s]
123+
event_to_forward = ["EVENT", s_original, event_json]
124+
await self.websocket.send_text(json.dumps(event_to_forward))
125+
except Exception as e:
126+
logger.debug(e)
117127

118128
def _handle_notices(self):
119129
while len(NostrRouter.received_subscription_notices):
120130
my_event = NostrRouter.received_subscription_notices.pop(0)
121131
# note: we don't send it to the user because we don't know who should receive it
122132
logger.info(f"Relay ('{my_event.url}') notice: '{my_event.content}']")
123133
nostr.client.relay_manager.handle_notice(my_event)
124-
134+
125135

126136

127137
def _marshall_nostr_filters(self, data: Union[dict, list]):

0 commit comments

Comments
 (0)