From 15e7efdd25d5417be59c8b38b6e1bdba183608e7 Mon Sep 17 00:00:00 2001 From: BlowaterNostr Date: Mon, 1 Apr 2024 16:18:06 +0800 Subject: [PATCH] + --- .gitignore | 1 + README.md | 5 ++-- main.tsx | 33 ++++++++++++++++++++++++-- resolvers/event.ts | 56 --------------------------------------------- resolvers/policy.ts | 12 ++++++++++ 5 files changed, 47 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index dca72b1..d0f0b80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ deploy/default.ts *cov_profile* coverage test.sqlite +node_modules diff --git a/README.md b/README.md index 30c8fad..bad56c6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Relayed + [![deno module](https://shield.deno.dev/x/relayed)](https://deno.land/x/relayed) -> [!IMPORTANT] -> Make Small Relay Great for the first time! +> [!IMPORTANT] Make Small Relay Great for the first time! Relayed is lightweight relay written in Deno. @@ -14,6 +14,7 @@ Relayed is lightweight relay written in Deno. 1. Install Deno at https://deno.land/manual/getting_started/installation. 2. Run following command in your CLI: + ```bash # relayed_pw is the password for the relayed admin relayed_pw=123whatever deno run -r --allow-net --allow-env --unstable https://deno.land/x/relayed/deploy/example.ts diff --git a/main.tsx b/main.tsx index 781e86a..255371a 100644 --- a/main.tsx +++ b/main.tsx @@ -1,3 +1,4 @@ +// deno-lint-ignore-file import { typeDefs } from "./graphql-schema.ts"; import { SubscriptionMap, ws_handler } from "./ws.ts"; import Error404 from "./routes/_404.tsx"; @@ -5,7 +6,7 @@ import { render } from "https://esm.sh/preact-render-to-string@6.4.1"; import { Mutation, RootResolver } from "./resolvers/root.ts"; import * as gql from "https://esm.sh/graphql@16.8.1"; -import { Policy, PolicyResolver } from "./resolvers/policy.ts"; +import { get_policies, Policy, PolicyResolver } from "./resolvers/policy.ts"; import { func_ResolvePolicyByKind } from "./resolvers/policy.ts"; import { func_GetEventsByKinds, func_WriteEvent } from "./resolvers/event.ts"; import { WriteEvent } from "./resolvers/event.ts"; @@ -14,6 +15,8 @@ import { GetEventsByIDs } from "./resolvers/event.ts"; import { GetEventsByKinds } from "./resolvers/event.ts"; import { NostrEvent, NostrKind, parseJSON, PublicKey, verifyEvent } from "./_libs.ts"; +import Dataloader from "https://esm.sh/dataloader@2.2.2"; + const schema = gql.buildSchema(gql.print(typeDefs)); export type DefaultPolicy = { @@ -59,6 +62,10 @@ export async function run(args: { resolve_hostname = resolve; }); + const getter = get_policies(args.kv); + // @ts-ignore + const loader = new Dataloader((kinds) => getter(kinds)); + const server = Deno.serve( { port, @@ -72,7 +79,29 @@ export async function run(args: { ...args, password, connections, - resolvePolicyByKind: PolicyResolver(default_policy, args.kv), + resolvePolicyByKind: async (kind: NostrKind) => { + const policy = await loader.load(kind); + if (policy == null) { + let allow_this_kind: boolean; + if (default_policy.allowed_kinds == "all") { + allow_this_kind = true; + } else if (default_policy.allowed_kinds == "none") { + allow_this_kind = false; + } else if (default_policy.allowed_kinds.includes(kind)) { + allow_this_kind = true; + } else { + allow_this_kind = false; + } + return { + kind: kind, + read: allow_this_kind, + write: allow_this_kind, + allow: new Set(), + block: new Set(), + }; + } + return policy; + }, write_event: WriteEvent(args.kv), get_events_by_IDs: GetEventsByIDs(args.kv), get_events_by_kinds: GetEventsByKinds(args.kv), diff --git a/resolvers/event.ts b/resolvers/event.ts index 8c0f491..e44fe48 100644 --- a/resolvers/event.ts +++ b/resolvers/event.ts @@ -8,67 +8,11 @@ export type Actor = { npub: string; }; -export const EventResolverFactory = (kv: Deno.Kv) => - class EventResolver { - id: string; - sig: string; - kind: NostrKind; - content: string; - - static Resolve(event: NostrEvent) { - // const policy = kv.get(["policy", event.kind]) - return new EventResolver(event); - } - - static async ByID(id: string) { - const entry = await kv.get(["event", id]); - if (entry.value) { - return new EventResolver(entry.value); - } - return null; - } - - private constructor(public event: NostrEvent) { - this.id = this.event.id; - this.sig = this.event.sig; - this.kind = this.event.kind; - this.content = this.event.content; - } - - pubkey = () => { - return pubkey_resolver( - PublicKey.FromHex(this.event.pubkey) as PublicKey, - ); - }; - }; - type Pagination = { offset?: number | undefined; limit?: number | undefined; }; -// export async function EventsResolver( -// args: { pubkey: string | undefined } & Pagination, -// ) { -// const limit = args.limit || 10; -// const list = kv.list({ prefix: ["event"] }); -// const res = [] as EventResolver[]; -// for await (const entry of list) { -// if (args.pubkey == undefined || args.pubkey == entry.value.pubkey) { -// res.push(EventResolver.Resolve(entry.value)); -// if (res.length >= limit) { -// break; -// } -// } -// } -// return { -// count: res.length, -// data: async function x() { -// return res; -// }, -// }; -// } - export type func_GetEventsByIDs = (ids: string[]) => Promise; export const GetEventsByIDs = (kv: Deno.Kv): func_GetEventsByIDs => async (ids: string[]) => { const entries = await kv.getMany(ids.map((id) => ["event", id])); diff --git a/resolvers/policy.ts b/resolvers/policy.ts index 9e5b457..1219385 100644 --- a/resolvers/policy.ts +++ b/resolvers/policy.ts @@ -1,5 +1,6 @@ import { NostrEvent, NostrKind } from "../_libs.ts"; import { DefaultPolicy } from "../main.tsx"; +import Dataloader from "https://esm.sh/dataloader@2.2.2"; export const Policies = (kv: Deno.Kv) => async function () { @@ -11,6 +12,17 @@ export const Policies = (kv: Deno.Kv) => return res; }; +export const get_policies = (kv: Deno.Kv) => async (kinds: NostrKind[]) => { + const entries = await kv.getMany(kinds.map((kind) => ["policy", kind])); + return entries.map((entry) => entry.value); +}; + +// export const get_policy_by_kind = (kv: Deno.Kv) => async (kind: NostrKind) => { +// const getter = get_policies(kv) +// const loader = new Dataloader(kinds => getter(kinds)) +// return loader.load(kind) +// } + export type func_ResolvePolicyByKind = (kind: NostrKind) => Promise; export const PolicyResolver = (default_policy: DefaultPolicy, kv: Deno.Kv): func_ResolvePolicyByKind => async function (kind: NostrKind): Promise {