-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e7efd
commit 8d627e7
Showing
3 changed files
with
79 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
// deno-lint-ignore-file | ||
import { typeDefs } from "./graphql-schema.ts"; | ||
import { SubscriptionMap, ws_handler } from "./ws.ts"; | ||
import Error404 from "./routes/_404.tsx"; | ||
import { render } from "https://esm.sh/[email protected]"; | ||
import { Mutation, RootResolver } from "./resolvers/root.ts"; | ||
import * as gql from "https://esm.sh/[email protected]"; | ||
|
||
import { get_policies, Policy, PolicyResolver } from "./resolvers/policy.ts"; | ||
import { Policy } 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"; | ||
import { func_GetEventsByIDs } from "./resolvers/event.ts"; | ||
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/[email protected]"; | ||
import { PolicyStore } from "./resolvers/policy.ts"; | ||
|
||
const schema = gql.buildSchema(gql.print(typeDefs)); | ||
|
||
|
@@ -62,10 +60,7 @@ export async function run(args: { | |
resolve_hostname = resolve; | ||
}); | ||
|
||
const getter = get_policies(args.kv); | ||
// @ts-ignore | ||
const loader = new Dataloader<NostrKind, Policy | null>((kinds) => getter(kinds)); | ||
|
||
const policyStore = new PolicyStore(default_policy, args.kv); | ||
const server = Deno.serve( | ||
{ | ||
port, | ||
|
@@ -79,48 +74,29 @@ export async function run(args: { | |
...args, | ||
password, | ||
connections, | ||
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; | ||
}, | ||
resolvePolicyByKind: policyStore.resolvePolicyByKind, | ||
write_event: WriteEvent(args.kv), | ||
get_events_by_IDs: GetEventsByIDs(args.kv), | ||
get_events_by_kinds: GetEventsByKinds(args.kv), | ||
policyStore, | ||
kv: args.kv, | ||
}), | ||
); | ||
const resolvePolicyByKind = PolicyResolver(args.default_policy, args.kv); | ||
const mutation_resolver = Mutation({ ...args, resolvePolicyByKind, kv: args.kv }); | ||
|
||
const mutation_resolver = Mutation({ | ||
...args, | ||
policyStore, | ||
kv: args.kv, | ||
}); | ||
return { | ||
server, | ||
url: `ws://${await hostname}:${port}`, | ||
shutdown: async () => { | ||
await server.shutdown(); | ||
args.kv?.close(); | ||
}, | ||
set_policy: mutation_resolver.set_policy, | ||
get_policy: (kind: NostrKind) => { | ||
return resolvePolicyByKind(kind); | ||
}, | ||
set_policy: policyStore.set_policy, | ||
get_policy: policyStore.resolvePolicyByKind, | ||
default_policy: args.default_policy, | ||
}; | ||
} | ||
|
@@ -137,6 +113,7 @@ const root_handler = ( | |
connections: Map<WebSocket, SubscriptionMap>; | ||
default_policy: DefaultPolicy; | ||
resolvePolicyByKind: func_ResolvePolicyByKind; | ||
policyStore: PolicyStore; | ||
kv: Deno.Kv; | ||
} & EventReadWriter, | ||
) => | ||
|
@@ -156,9 +133,8 @@ async (req: Request, info: Deno.ServeHandlerInfo) => { | |
}; | ||
|
||
const graphql_handler = | ||
(args: { password: string; kv: Deno.Kv; resolvePolicyByKind: func_ResolvePolicyByKind }) => | ||
async (req: Request) => { | ||
const { password, kv, resolvePolicyByKind } = args; | ||
(args: { password: string; kv: Deno.Kv; policyStore: PolicyStore }) => async (req: Request) => { | ||
const { password, policyStore } = args; | ||
if (req.method == "POST") { | ||
const query = await req.json(); | ||
const nip42 = req.headers.get("nip42"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { NostrEvent, NostrKind } from "../_libs.ts"; | ||
import { DefaultPolicy } from "../main.tsx"; | ||
import Dataloader from "https://esm.sh/[email protected]"; | ||
|
||
export const Policies = (kv: Deno.Kv) => | ||
async function () { | ||
|
@@ -17,12 +16,6 @@ export const get_policies = (kv: Deno.Kv) => async (kinds: NostrKind[]) => { | |
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<NostrKind, Policy | null>(kinds => getter(kinds)) | ||
// return loader.load(kind) | ||
// } | ||
|
||
export type func_ResolvePolicyByKind = (kind: NostrKind) => Promise<Policy>; | ||
export const PolicyResolver = (default_policy: DefaultPolicy, kv: Deno.Kv): func_ResolvePolicyByKind => | ||
async function (kind: NostrKind): Promise<Policy> { | ||
|
@@ -80,3 +73,56 @@ export type Policy = { | |
allow: Set<string>; | ||
block: Set<string>; | ||
}; | ||
|
||
export class PolicyStore { | ||
policies = new Map<NostrKind, Policy>(); | ||
|
||
constructor( | ||
private default_policy: DefaultPolicy, | ||
private kv: Deno.Kv, | ||
) {} | ||
|
||
resolvePolicyByKind = async (kind: NostrKind): Promise<Policy> => { | ||
const policy = this.policies.get(kind); | ||
if (policy == undefined) { | ||
const default_policy = this.default_policy; | ||
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; | ||
}; | ||
|
||
set_policy = async ( | ||
args: { | ||
kind: NostrKind; | ||
read?: boolean; | ||
write?: boolean; | ||
}, | ||
) => { | ||
const policy = await this.resolvePolicyByKind(args.kind); | ||
if (args.read != undefined) { | ||
policy.read = args.read; | ||
} | ||
if (args.write != undefined) { | ||
policy.write = args.write; | ||
} | ||
this.policies.set(args.kind, policy); | ||
await this.kv.set(["policy", args.kind], policy); | ||
return policy; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters