Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add extra checks #21

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions router.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,49 @@ async def _handle_subscriptions(self):


async def _handle_received_subscription_eosenotices(self, s):
s_original = self.original_subscription_ids[s]
event_to_forward = ["EOSE", s_original]
del NostrRouter.received_subscription_eosenotices[s]

await self.websocket.send_text(json.dumps(event_to_forward))

async def _handle_received_subscription_events(self, s):
while len(NostrRouter.received_subscription_events[s]):
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_json = {
"id": my_event.id,
"pubkey": my_event.public_key,
"created_at": my_event.created_at,
"kind": my_event.kind,
"tags": my_event.tags,
"content": my_event.content,
"sig": my_event.signature,
}

# this reconstructs the original response from the relay
# reconstruct original subscription id
try:
if s not in self.original_subscription_ids:
return
s_original = self.original_subscription_ids[s]
event_to_forward = ["EVENT", s_original, event_json]
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):
try:
if s not in NostrRouter.received_subscription_events:
return
while len(NostrRouter.received_subscription_events[s]):
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_json = {
"id": my_event.id,
"pubkey": my_event.public_key,
"created_at": my_event.created_at,
"kind": my_event.kind,
"tags": my_event.tags,
"content": my_event.content,
"sig": my_event.signature,
}

# this reconstructs the original response from the relay
# reconstruct original subscription id
s_original = self.original_subscription_ids[s]
event_to_forward = ["EVENT", s_original, event_json]
await self.websocket.send_text(json.dumps(event_to_forward))
except Exception as e:
logger.debug(e)

def _handle_notices(self):
while len(NostrRouter.received_subscription_notices):
my_event = NostrRouter.received_subscription_notices.pop(0)
# 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}']")
nostr.client.relay_manager.handle_notice(my_event)



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