Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Apr 1, 2024
1 parent c039255 commit 15e7efd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ deploy/default.ts
*cov_profile*
coverage
test.sqlite
node_modules
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand Down
33 changes: 31 additions & 2 deletions main.tsx
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";
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down
56 changes: 0 additions & 56 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NostrEvent>(["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<NostrEvent>({ 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<NostrEvent[]>;
export const GetEventsByIDs = (kv: Deno.Kv): func_GetEventsByIDs => async (ids: string[]) => {
const entries = await kv.getMany<NostrEvent[]>(ids.map((id) => ["event", id]));
Expand Down
12 changes: 12 additions & 0 deletions resolvers/policy.ts
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 () {
Expand All @@ -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> {
Expand Down

0 comments on commit 15e7efd

Please sign in to comment.