Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Apr 30, 2024
1 parent a7ca731 commit 7fd8449
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
1 change: 0 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion graphql-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gql } from "https://deno.land/x/[email protected]/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
Expand Down
8 changes: 5 additions & 3 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -222,6 +224,7 @@ const graphql_handler = (
kv: Deno.Kv;
policyStore: PolicyStore;
relayInformationStore: RelayInformationStore;
get_events_by_authors: func_GetEventsByAuthors;
},
) =>
async (req: Request) => {
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run: fmt
deno task run
deno run --check --allow-net --allow-env --unstable deploy/default.ts

fmt:
deno fmt
Expand Down
23 changes: 10 additions & 13 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@ import {
NostrFilter,
NostrKind,
prepareNormalNostrEvent,
PublicKey,
} from "../_libs.ts";
import { EventReadWriter } from "../main.tsx";
import { assertEquals } from "https://deno.land/[email protected]/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<string>) => AsyncIterable<NostrEvent>;
export type interface_GetEventsByIDs = {
get_events_by_IDs: func_GetEventsByIDs;
Expand Down Expand Up @@ -65,8 +54,16 @@ export class EventStore implements EventReadWriter {
}

async *get_events_by_authors(authors: Set<string>): AsyncIterable<NostrEvent> {
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;
}
}
Expand Down
18 changes: 14 additions & 4 deletions resolvers/root.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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),
};
}

0 comments on commit 7fd8449

Please sign in to comment.