Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Mar 30, 2024
1 parent 4341897 commit 0e8cb60
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
3 changes: 3 additions & 0 deletions deploy/default.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import { run } from "../main.tsx";
run({
port: 8080,
password: "2af6bfe8352040640fa1b3917fd704",
default_policy: {
allowed_kinds: "all"
},
});
8 changes: 5 additions & 3 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function run(args: {
write_event: WriteEvent(kv),
get_events_by_IDs: GetEventsByIDs(kv),
get_events_by_kinds: GetEventsByKinds(kv),
kv,
}),
);
const resolvePolicyByKind = PolicyResolver(args.default_policy, kv);
Expand Down Expand Up @@ -88,14 +89,15 @@ const root_handler = (
connections: Map<WebSocket, Map<string, NostrFilters>>;
default_policy: DefaultPolicy;
resolvePolicyByKind: func_ResolvePolicyByKind;
kv: Deno.Kv
} & EventReadWriter,
) =>
async (req: Request, info: Deno.ServeHandlerInfo) => {
console.log(info.remoteAddr);
const { password } = args;
const { pathname } = new URL(req.url);
if (pathname == "/api") {
return graphql_handler(password)(req);
return graphql_handler(password, args.kv)(req);
}
if (pathname == "/") {
return ws_handler(args)(req, info);
Expand All @@ -105,7 +107,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => {
return resp;
};

const graphql_handler = (password: string) => async (req: Request) => {
const graphql_handler = (password: string, kv: Deno.Kv) => async (req: Request) => {
if (req.method == "POST") {
const query = await req.json();
const nip42 = req.headers.get("nip42");
Expand All @@ -130,7 +132,7 @@ const graphql_handler = (password: string) => async (req: Request) => {
schema: schema,
source: query.query,
variableValues: query.variables,
rootValue: RootResolver(),
rootValue: RootResolver(kv),
});
console.log(result);
return new Response(JSON.stringify(result));
Expand Down
3 changes: 0 additions & 3 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export const GetEventsByIDs = (kv: Deno.Kv): func_GetEventsByIDs => async (ids:
export type func_GetEventsByKinds = (kinds: NostrKind[]) => AsyncIterable<NostrEvent>;
export const GetEventsByKinds = (kv: Deno.Kv): func_GetEventsByKinds =>
async function* x(kinds: NostrKind[]) {
console.log("GetEventsByKinds", kinds);
for (const kind of kinds) {
console.log("event", kind);
const list = kv.list<NostrEvent>({ prefix: ["event", kind] });
for await (const entry of list) {
console.log(entry);
Expand All @@ -104,7 +102,6 @@ function fromEntries<T>(entries: Deno.KvEntryMaybe<T>[]) {

export type func_WriteEvent = (event: NostrEvent) => Promise<boolean>;
export const WriteEvent = (kv: Deno.Kv): func_WriteEvent => async (event: NostrEvent) => {
console.log("WriteEvent", event, ["event", event.kind]);
const result = await kv.atomic()
.set(["event", event.id], event)
.set(["event", event.kind, event.id], event)
Expand Down
2 changes: 1 addition & 1 deletion resolvers/policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DefaultPolicy } from "../main.tsx";
import { NostrEvent, NostrKind } from "../nostr.ts/nostr.ts";

export async function Policies(kv: Deno.Kv) {
export const Policies = (kv: Deno.Kv) => async function () {
const list = kv.list<NostrEvent>({ prefix: ["policy"] });
const res = [] as NostrEvent[];
for await (const entry of list) {
Expand Down
15 changes: 5 additions & 10 deletions resolvers/root.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { NostrKind } from "../nostr.ts/nostr.ts";
import { func_ResolvePolicyByKind, Policies, PolicyResolver } from "./policy.ts";
import { Policies } from "./policy.ts";
import { func_ResolvePolicyByKind } from "./policy.ts";


const Query = {
// events: EventsResolver,
// event: (args: { id: string }) => {
// return EventResolver.ByID(args.id);
// },
policies: Policies,
};

export const Mutation = (policyResolver: func_ResolvePolicyByKind, kv: Deno.Kv) => {
return {
Expand Down Expand Up @@ -55,9 +50,9 @@ export const Mutation = (policyResolver: func_ResolvePolicyByKind, kv: Deno.Kv)
};
};

export function RootResolver() {
export function RootResolver(kv: Deno.Kv) {
return {
...Query,
policies: Policies(kv),
...Mutation,
};
}
17 changes: 13 additions & 4 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ function onMessage(
resolvePolicyByKind: func_ResolvePolicyByKind;
} & EventReadWriter,
) {
const { this_socket, connections, resolvePolicyByKind } = deps;
const { this_socket, connections } = deps;


return async (event: MessageEvent<string>) => {
console.log(event.data);
console.log("on message", event.data);
const nostr_ws_msg = JSON.parse(event.data) as ClientRequest_Message;
const cmd = nostr_ws_msg[0];
if (cmd == "EVENT") {
Expand Down Expand Up @@ -132,13 +133,18 @@ async function handle_cmd_event(args: {
}

const _ok = await write_event(event);
send(this_socket, JSON.stringify(respond_ok(event, true, "")));
if(_ok) {
send(this_socket, JSON.stringify(respond_ok(event, true, "")));
} else {
send(this_socket, JSON.stringify(respond_ok(event, false, "")));
}
for (
const matched of matchEventWithSubscriptions(
event,
connections,
)
) {
console.log(policy)
if (policy.read === false) {
return;
}
Expand All @@ -161,6 +167,8 @@ async function handle_cmd_req(
const sub_id = nostr_ws_msg[1];
const filter = nostr_ws_msg[2];

args.connections.get(this_socket)?.set(sub_id, filter)

// query this filter
const event_candidates = new Map<string, NostrEvent>();
if (filter.ids) {
Expand All @@ -185,7 +193,6 @@ async function handle_cmd_req(
}
} else {
const events = args.get_events_by_kinds(filter.kinds);
console.log("get_events_by_kinds", filter.kinds, events);
for await (const event of events) {
console.log(event);
event_candidates.set(event.id, event);
Expand Down Expand Up @@ -254,7 +261,9 @@ function* matchEventWithSubscriptions(
connections: Map<WebSocket, Map<string, NostrFilters>>,
) {
for (const [socket, subscriptions] of connections) {
console.log(subscriptions)
for (const [sub_id, filter] of subscriptions) {
console.log(sub_id, filter)
if (isMatched(event, filter)) {
yield {
socket,
Expand Down

0 comments on commit 0e8cb60

Please sign in to comment.