-
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
c039255
commit 15e7efd
Showing
5 changed files
with
47 additions
and
60 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 |
---|---|---|
|
@@ -10,3 +10,4 @@ deploy/default.ts | |
*cov_profile* | ||
coverage | ||
test.sqlite | ||
node_modules |
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
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,11 +1,12 @@ | ||
// 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 { 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/[email protected]"; | ||
|
||
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<NostrKind, Policy | null>((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), | ||
|
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
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,5 +1,6 @@ | ||
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 () { | ||
|
@@ -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<Policy[]>(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<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> { | ||
|