From 7fd84499dc53d006d2ee55a5b983889eeb3cc4df Mon Sep 17 00:00:00 2001 From: BlowaterNostr Date: Wed, 1 May 2024 05:24:56 +0800 Subject: [PATCH] fix --- deno.json | 1 - graphql-schema.ts | 2 +- main.tsx | 8 +++++--- makefile | 2 +- resolvers/event.ts | 23 ++++++++++------------- resolvers/root.ts | 18 ++++++++++++++---- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/deno.json b/deno.json index 1eaa94b..98b5c1f 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,5 @@ { "tasks": { - "run": "deno run --allow-net --allow-env --unstable deploy/default.ts", "test": "deno test --allow-net --unstable --allow-read --allow-write --coverage test.ts" }, "lint": { diff --git a/graphql-schema.ts b/graphql-schema.ts index c3bcf03..4d002ce 100644 --- a/graphql-schema.ts +++ b/graphql-schema.ts @@ -2,7 +2,7 @@ import { gql } from "https://deno.land/x/graphql_tag@0.1.2/mod.ts"; export const typeDefs = gql` type Query { - events(pubkey: String, offset: Int, limit: Int): Events + events(pubkey: [String!]!, offset: Int, limit: Int): Events event(id: String): Event policies: [Policy] relayInformation: RelayInformation diff --git a/main.tsx b/main.tsx index e6bcf14..e055e88 100644 --- a/main.tsx +++ b/main.tsx @@ -9,6 +9,7 @@ import { NostrEvent, NostrKind, PublicKey, verifyEvent } from "./_libs.ts"; import { PolicyStore } from "./resolvers/policy.ts"; import { Policies } from "./resolvers/policy.ts"; import { + func_GetEventsByAuthors, func_GetReplaceableEvents, func_WriteReplaceableEvent, interface_GetEventsByAuthors, @@ -158,7 +159,8 @@ export type EventReadWriter = { get_events_by_filter: func_GetEventsByFilter; mark_event_deleted: func_MarkEventDeleted; get_replaceable_events: func_GetReplaceableEvents; -} & interface_GetEventsByAuthors; + get_events_by_authors: func_GetEventsByAuthors; +}; const root_handler = ( args: { @@ -222,6 +224,7 @@ const graphql_handler = ( kv: Deno.Kv; policyStore: PolicyStore; relayInformationStore: RelayInformationStore; + get_events_by_authors: func_GetEventsByAuthors; }, ) => async (req: Request) => { @@ -233,9 +236,8 @@ async (req: Request) => { if (!token) { return new Response(`{"errors":"no token"}`); } - console.log(`get token: ${token}`); + const event = JSON.parse(atob(token)); - console.log(`get event: ${JSON.stringify(event)}`); const error = await verifyToken(event, args.relayInformationStore); if (error instanceof Error) { return new Response(JSON.stringify({ diff --git a/makefile b/makefile index 2c11bb7..9f8d53a 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ run: fmt - deno task run + deno run --check --allow-net --allow-env --unstable deploy/default.ts fmt: deno fmt diff --git a/resolvers/event.ts b/resolvers/event.ts index 85b4d78..43e46c9 100644 --- a/resolvers/event.ts +++ b/resolvers/event.ts @@ -5,22 +5,11 @@ import { NostrFilter, NostrKind, prepareNormalNostrEvent, + PublicKey, } from "../_libs.ts"; import { EventReadWriter } from "../main.tsx"; import { assertEquals } from "https://deno.land/std@0.202.0/assert/assert_equals.ts"; -export type Actor = { - type: "admin"; -} | { - type: "user"; - npub: string; -}; - -type Pagination = { - offset?: number | undefined; - limit?: number | undefined; -}; - export type func_GetEventsByIDs = (ids: Set) => AsyncIterable; export type interface_GetEventsByIDs = { get_events_by_IDs: func_GetEventsByIDs; @@ -65,8 +54,16 @@ export class EventStore implements EventReadWriter { } async *get_events_by_authors(authors: Set): AsyncIterable { + const hex_keys = new Set(); + for (const author of authors) { + const key = PublicKey.FromString(author); + if (key instanceof Error) { + continue; + } + hex_keys.add(key.hex); + } for (const event of this.events.values()) { - if (authors.has(event.pubkey)) { + if (hex_keys.has(event.pubkey)) { yield event; } } diff --git a/resolvers/root.ts b/resolvers/root.ts index 0968e5f..cd0134d 100644 --- a/resolvers/root.ts +++ b/resolvers/root.ts @@ -1,6 +1,8 @@ import { PolicyStore } from "./policy.ts"; import { Policies } from "./policy.ts"; import { RelayInformationStore } from "./nip11.ts"; +import { EventStore, func_GetEventsByAuthors } from "./event.ts"; +import { EventReadWriter } from "../main.tsx"; export const Mutation = (args: { policyStore: PolicyStore; @@ -38,14 +40,22 @@ export const Mutation = (args: { }; }; -export function RootResolver(args: { +export function RootResolver(deps: { policyStore: PolicyStore; relayInformationStore: RelayInformationStore; kv: Deno.Kv; + get_events_by_authors: func_GetEventsByAuthors; }) { return { - policies: Policies(args.kv), - relayInformation: args.relayInformationStore.resolveRelayInformation, - ...Mutation(args), + policies: Policies(deps.kv), + relayInformation: deps.relayInformationStore.resolveRelayInformation, + events: async (args: { pubkey: string[]; offset: number; limit: number }) => { + console.log(args.pubkey); + const events = await Array.fromAsync(deps.get_events_by_authors(new Set(args.pubkey))); + return { + count: events.length, + }; + }, + ...Mutation(deps), }; }