Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Mar 29, 2024
1 parent 5ab9022 commit 0afd68a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => {
return graphql_handler(password)(req);
}
if (pathname == "/") {
return ws_handler(args)(req);
return ws_handler(args)(req, info);
}
const resp = new Response(render(Error404()), { status: 404 });
resp.headers.set("content-type", "html");
Expand Down
5 changes: 1 addition & 4 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ Deno.test("localhost", async () => {
const event_got = await client.getEvent(event_sent.id);
assertEquals(event_got, undefined);

await relay.set_policy({
kind: NostrKind.TEXT_NOTE,
write: true,
});
await relay.set_policy({ kind: NostrKind.TEXT_NOTE, read: true });
const event_got_2 = await client.getEvent(event_sent.id);
assertEquals(event_got_2, event_sent);
}
Expand Down
20 changes: 14 additions & 6 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ws_handler = (
resolvePolicyByKind: func_ResolvePolicyByKind;
} & EventReadWriter,
) =>
(req: Request) => {
(req: Request, info: Deno.ServeHandlerInfo) => {
const { connections } = args;

if (req.headers.get("upgrade") != "websocket") {
Expand All @@ -34,11 +34,12 @@ export const ws_handler = (
const { socket, response } = Deno.upgradeWebSocket(req);

socket.onopen = ((socket: WebSocket) => (e) => {
console.log("a client connected!");
console.log("a client connected!", info.remoteAddr);
connections.set(socket, new Map());
})(socket);

socket.onclose = ((socket: WebSocket) => (e) => {
console.log("client disconnected", info.remoteAddr);
connections.delete(socket);
})(socket);

Expand Down Expand Up @@ -154,19 +155,26 @@ async function handle_cmd_req(
nostr_ws_msg: ClientRequest_REQ,
args: {
this_socket: WebSocket;
// connections: Map<WebSocket, Map<string, NostrFilters>>;
connections: Map<WebSocket, Map<string, NostrFilters>>;
resolvePolicyByKind: func_ResolvePolicyByKind;
} & EventReadWriter,
) {
const { this_socket, resolvePolicyByKind, get_events_by_IDs } = args;
const sub_id = nostr_ws_msg[1];
const filter = nostr_ws_msg[2];

// connections.get(this_socket)?.set(sub_id, filter);

// query this filter
if (filter.ids) {
get_events_by_IDs(filter.ids);
const events = await get_events_by_IDs(filter.ids);
console.log(events);
for (const event of events) {
const policy = await resolvePolicyByKind(event.kind);
if (policy.read == false) {
continue;
}
send(this_socket, JSON.stringify(respond_event(sub_id, event)));
console.log(event);
}
}

// for await (
Expand Down

0 comments on commit 0afd68a

Please sign in to comment.