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 f3f23db commit db4ed78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 67 deletions.
9 changes: 3 additions & 6 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { GetEventsByIDs } from "./resolvers/event.ts";
import { GetEventsByKinds } from "./resolvers/event.ts";
import { NostrEvent, NostrKind, parseJSON, PublicKey, verifyEvent } from "./_libs.ts";
import { PolicyStore } from "./resolvers/policy.ts";
import { Policies } from "./resolvers/policy.ts";

const schema = gql.buildSchema(gql.print(typeDefs));

Expand Down Expand Up @@ -60,7 +61,8 @@ export async function run(args: {
resolve_hostname = resolve;
});

const policyStore = new PolicyStore(default_policy, args.kv);
const get_all_policies = Policies(args.kv);
const policyStore = new PolicyStore(default_policy, args.kv, await get_all_policies());
const server = Deno.serve(
{
port,
Expand All @@ -83,11 +85,6 @@ export async function run(args: {
}),
);

const mutation_resolver = Mutation({
...args,
policyStore,
kv: args.kv,
});
return {
server,
url: `ws://${await hostname}:${port}`,
Expand Down
74 changes: 17 additions & 57 deletions resolvers/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,15 @@ import { DefaultPolicy } from "../main.tsx";

export const Policies = (kv: Deno.Kv) =>
async function () {
const list = kv.list<NostrEvent>({ prefix: ["policy"] });
const res = [] as NostrEvent[];
const list = kv.list<Policy>({ prefix: ["policy"] });
const res = [] as Policy[];
for await (const entry of list) {
res.push(entry.value);
}
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 type func_ResolvePolicyByKind = (kind: NostrKind) => Promise<Policy>;
export const PolicyResolver = (default_policy: DefaultPolicy, kv: Deno.Kv): func_ResolvePolicyByKind =>
async function (kind: NostrKind): Promise<Policy> {
const entry = await kv.get<Policy>(["policy", kind]);
if (entry.value == 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(),
};
}
const policy = entry.value;

const allow = new Set<string>();
for (const item of policy.allow) {
if (typeof item == "string") {
allow.add(item);
}
}
policy.allow = allow;

const block = new Set<string>();
for (const item of policy.block) {
if (typeof item == "string") {
block.add(item);
}
}
policy.block = block;
policy.kind = kind;
if (policy.read == null) {
policy.read = true;
}
if (policy.write == null) {
policy.write = true;
}
return policy;
};

export type Policy = {
kind: NostrKind;
Expand All @@ -80,7 +27,12 @@ export class PolicyStore {
constructor(
private default_policy: DefaultPolicy,
private kv: Deno.Kv,
) {}
private initial_policies: Policy[],
) {
for (const policy of initial_policies) {
this.policies.set(policy.kind, policy);
}
}

resolvePolicyByKind = async (kind: NostrKind): Promise<Policy> => {
const policy = this.policies.get(kind);
Expand Down Expand Up @@ -112,7 +64,9 @@ export class PolicyStore {
kind: NostrKind;
read?: boolean;
write?: boolean;
},
allow?: Set<string>;
block?: Set<string>;
} | Policy,
) => {
const policy = await this.resolvePolicyByKind(args.kind);
if (args.read != undefined) {
Expand All @@ -121,6 +75,12 @@ export class PolicyStore {
if (args.write != undefined) {
policy.write = args.write;
}
if (args.allow) {
policy.allow = args.allow;
}
if (args.block) {
policy.block = args.block;
}
this.policies.set(args.kind, policy);
await this.kv.set(["policy", args.kind], policy);
return policy;
Expand Down
8 changes: 4 additions & 4 deletions resolvers/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ export const Mutation = (args: {
add_block: async (args: { kind: number; pubkey: string }) => {
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.block.add(args.pubkey);
await kv.set(["policy", args.kind], policy);
await policyStore.set_policy(policy);
return policy;
},
remove_block: async (args: { kind: number; pubkey: string }) => {
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.block.delete(args.pubkey);
await kv.set(["policy", args.kind], policy);
await policyStore.set_policy(policy);
return policy;
},
add_allow: async (args: { kind: number; pubkey: string }) => {
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.allow.add(args.pubkey);
await kv.set(["policy", args.kind], policy);
await policyStore.set_policy(policy);
return policy;
},
remove_allow: async (args: { kind: number; pubkey: string }) => {
const policy = await policyStore.resolvePolicyByKind(args.kind);
policy.allow.delete(args.pubkey);
await kv.set(["policy", args.kind], policy);
await policyStore.set_policy(policy);
return policy;
},
set_policy: policyStore.set_policy,
Expand Down

0 comments on commit db4ed78

Please sign in to comment.