Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Apr 2, 2024
1 parent 15e7efd commit 8d627e7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 55 deletions.
56 changes: 16 additions & 40 deletions main.tsx
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));

Expand Down Expand Up @@ -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,
Expand All @@ -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,
};
}
Expand All @@ -137,6 +113,7 @@ const root_handler = (
connections: Map<WebSocket, SubscriptionMap>;
default_policy: DefaultPolicy;
resolvePolicyByKind: func_ResolvePolicyByKind;
policyStore: PolicyStore;
kv: Deno.Kv;
} & EventReadWriter,
) =>
Expand All @@ -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");
Expand Down
60 changes: 53 additions & 7 deletions resolvers/policy.ts
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 () {
Expand All @@ -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> {
Expand Down Expand Up @@ -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;
};
}
18 changes: 10 additions & 8 deletions resolvers/root.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { NostrKind } from "../_libs.ts";
import { PolicyStore } from "./policy.ts";
import { Policies } from "./policy.ts";
import { func_ResolvePolicyByKind } from "./policy.ts";

export const Mutation = (args: {
resolvePolicyByKind: func_ResolvePolicyByKind;
// resolvePolicyByKind: func_ResolvePolicyByKind;
policyStore: PolicyStore;
kv: Deno.Kv;
}) => {
const { resolvePolicyByKind, kv } = args;
const { policyStore, kv } = args;
return {
add_block: async (args: { kind: number; pubkey: string }) => {
const policy = await resolvePolicyByKind(args.kind);
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.block.add(args.pubkey);
await kv.set(["policy", args.kind], policy);
return policy;
},
remove_block: async (args: { kind: number; pubkey: string }) => {
const policy = await resolvePolicyByKind(args.kind);
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.block.delete(args.pubkey);
await kv.set(["policy", args.kind], policy);
return policy;
},
add_allow: async (args: { kind: number; pubkey: string }) => {
const policy = await resolvePolicyByKind(args.kind);
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.allow.add(args.pubkey);
await kv.set(["policy", args.kind], policy);
return policy;
},
remove_allow: async (args: { kind: number; pubkey: string }) => {
const policy = await resolvePolicyByKind(args.kind);
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.allow.delete(args.pubkey);
await kv.set(["policy", args.kind], policy);
return policy;
Expand All @@ -39,7 +41,7 @@ export const Mutation = (args: {
write?: boolean;
},
) => {
const policy = await resolvePolicyByKind(args.kind);
const policy = await policyStore.resolvePolicyByKind(args.kind);
if (args.read != undefined) {
policy.read = args.read;
}
Expand All @@ -53,7 +55,7 @@ export const Mutation = (args: {
};

export function RootResolver(args: {
resolvePolicyByKind: func_ResolvePolicyByKind;
policyStore: PolicyStore;
kv: Deno.Kv;
}) {
return {
Expand Down

0 comments on commit 8d627e7

Please sign in to comment.