From 0e8cb6057acee52d0d9ff3ee206593c2ed519ac8 Mon Sep 17 00:00:00 2001 From: BlowaterNostr Date: Sat, 30 Mar 2024 16:14:41 +0800 Subject: [PATCH] + --- deploy/default.example.ts | 3 +++ main.tsx | 8 +++++--- resolvers/event.ts | 3 --- resolvers/policy.ts | 2 +- resolvers/root.ts | 15 +++++---------- ws.ts | 17 +++++++++++++---- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/deploy/default.example.ts b/deploy/default.example.ts index 26508a7..0c40d2c 100644 --- a/deploy/default.example.ts +++ b/deploy/default.example.ts @@ -3,4 +3,7 @@ import { run } from "../main.tsx"; run({ port: 8080, password: "2af6bfe8352040640fa1b3917fd704", + default_policy: { + allowed_kinds: "all" + }, }); diff --git a/main.tsx b/main.tsx index 29b6e36..9a94198 100644 --- a/main.tsx +++ b/main.tsx @@ -57,6 +57,7 @@ export async function run(args: { write_event: WriteEvent(kv), get_events_by_IDs: GetEventsByIDs(kv), get_events_by_kinds: GetEventsByKinds(kv), + kv, }), ); const resolvePolicyByKind = PolicyResolver(args.default_policy, kv); @@ -88,6 +89,7 @@ const root_handler = ( connections: Map>; default_policy: DefaultPolicy; resolvePolicyByKind: func_ResolvePolicyByKind; + kv: Deno.Kv } & EventReadWriter, ) => async (req: Request, info: Deno.ServeHandlerInfo) => { @@ -95,7 +97,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => { const { password } = args; const { pathname } = new URL(req.url); if (pathname == "/api") { - return graphql_handler(password)(req); + return graphql_handler(password, args.kv)(req); } if (pathname == "/") { return ws_handler(args)(req, info); @@ -105,7 +107,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => { return resp; }; -const graphql_handler = (password: string) => async (req: Request) => { +const graphql_handler = (password: string, kv: Deno.Kv) => async (req: Request) => { if (req.method == "POST") { const query = await req.json(); const nip42 = req.headers.get("nip42"); @@ -130,7 +132,7 @@ const graphql_handler = (password: string) => async (req: Request) => { schema: schema, source: query.query, variableValues: query.variables, - rootValue: RootResolver(), + rootValue: RootResolver(kv), }); console.log(result); return new Response(JSON.stringify(result)); diff --git a/resolvers/event.ts b/resolvers/event.ts index b86078e..b0d82bf 100644 --- a/resolvers/event.ts +++ b/resolvers/event.ts @@ -79,9 +79,7 @@ export const GetEventsByIDs = (kv: Deno.Kv): func_GetEventsByIDs => async (ids: export type func_GetEventsByKinds = (kinds: NostrKind[]) => AsyncIterable; export const GetEventsByKinds = (kv: Deno.Kv): func_GetEventsByKinds => async function* x(kinds: NostrKind[]) { - console.log("GetEventsByKinds", kinds); for (const kind of kinds) { - console.log("event", kind); const list = kv.list({ prefix: ["event", kind] }); for await (const entry of list) { console.log(entry); @@ -104,7 +102,6 @@ function fromEntries(entries: Deno.KvEntryMaybe[]) { export type func_WriteEvent = (event: NostrEvent) => Promise; export const WriteEvent = (kv: Deno.Kv): func_WriteEvent => async (event: NostrEvent) => { - console.log("WriteEvent", event, ["event", event.kind]); const result = await kv.atomic() .set(["event", event.id], event) .set(["event", event.kind, event.id], event) diff --git a/resolvers/policy.ts b/resolvers/policy.ts index 0015225..b1adc2f 100644 --- a/resolvers/policy.ts +++ b/resolvers/policy.ts @@ -1,7 +1,7 @@ import { DefaultPolicy } from "../main.tsx"; import { NostrEvent, NostrKind } from "../nostr.ts/nostr.ts"; -export async function Policies(kv: Deno.Kv) { +export const Policies = (kv: Deno.Kv) => async function () { const list = kv.list({ prefix: ["policy"] }); const res = [] as NostrEvent[]; for await (const entry of list) { diff --git a/resolvers/root.ts b/resolvers/root.ts index 162085a..f4a59e0 100644 --- a/resolvers/root.ts +++ b/resolvers/root.ts @@ -1,13 +1,8 @@ import { NostrKind } from "../nostr.ts/nostr.ts"; -import { func_ResolvePolicyByKind, Policies, PolicyResolver } from "./policy.ts"; +import { Policies } from "./policy.ts"; +import { func_ResolvePolicyByKind } from "./policy.ts"; + -const Query = { - // events: EventsResolver, - // event: (args: { id: string }) => { - // return EventResolver.ByID(args.id); - // }, - policies: Policies, -}; export const Mutation = (policyResolver: func_ResolvePolicyByKind, kv: Deno.Kv) => { return { @@ -55,9 +50,9 @@ export const Mutation = (policyResolver: func_ResolvePolicyByKind, kv: Deno.Kv) }; }; -export function RootResolver() { +export function RootResolver(kv: Deno.Kv) { return { - ...Query, + policies: Policies(kv), ...Mutation, }; } diff --git a/ws.ts b/ws.ts index 7c4c93a..b00de56 100644 --- a/ws.ts +++ b/ws.ts @@ -60,10 +60,11 @@ function onMessage( resolvePolicyByKind: func_ResolvePolicyByKind; } & EventReadWriter, ) { - const { this_socket, connections, resolvePolicyByKind } = deps; + const { this_socket, connections } = deps; + return async (event: MessageEvent) => { - console.log(event.data); + console.log("on message", event.data); const nostr_ws_msg = JSON.parse(event.data) as ClientRequest_Message; const cmd = nostr_ws_msg[0]; if (cmd == "EVENT") { @@ -132,13 +133,18 @@ async function handle_cmd_event(args: { } const _ok = await write_event(event); - send(this_socket, JSON.stringify(respond_ok(event, true, ""))); + if(_ok) { + send(this_socket, JSON.stringify(respond_ok(event, true, ""))); + } else { + send(this_socket, JSON.stringify(respond_ok(event, false, ""))); + } for ( const matched of matchEventWithSubscriptions( event, connections, ) ) { + console.log(policy) if (policy.read === false) { return; } @@ -161,6 +167,8 @@ async function handle_cmd_req( const sub_id = nostr_ws_msg[1]; const filter = nostr_ws_msg[2]; + args.connections.get(this_socket)?.set(sub_id, filter) + // query this filter const event_candidates = new Map(); if (filter.ids) { @@ -185,7 +193,6 @@ async function handle_cmd_req( } } else { const events = args.get_events_by_kinds(filter.kinds); - console.log("get_events_by_kinds", filter.kinds, events); for await (const event of events) { console.log(event); event_candidates.set(event.id, event); @@ -254,7 +261,9 @@ function* matchEventWithSubscriptions( connections: Map>, ) { for (const [socket, subscriptions] of connections) { + console.log(subscriptions) for (const [sub_id, filter] of subscriptions) { + console.log(sub_id, filter) if (isMatched(event, filter)) { yield { socket,