Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
bob2402 committed Jun 17, 2024
1 parent 91bf36f commit 2d794e5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 77 deletions.
168 changes: 93 additions & 75 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
func_DeleteEventsFromPubkey,
func_GetDeletedEventIDs,
} from "./resolvers/event_deletion.ts";
import { InvalidKey, PublicKey } from "./nostr.ts/key.ts";
import { PublicKey } from "./nostr.ts/key.ts";
import { parseJSON } from "./nostr.ts/_helper.ts";

const schema = gql.buildSchema(gql.print(typeDefs));
Expand Down Expand Up @@ -206,7 +206,7 @@ export async function run(args: {
add_space_member: add_space_member(db),
is_space_member: is_space_member({
admin: args.admin,
db
db,
}),
// channel
create_channel: create_channel_sqlite(db),
Expand Down Expand Up @@ -295,46 +295,10 @@ async (req: Request, info: Deno.ServeHandlerInfo) => {
}
const url = new URL(req.url);
if (url.pathname === "/api/auth/login") {
const body = await req.json();
if (!body) {
return new Response(`{"errors":"request body is null"}`, { status: 400 });
}
const error = await verifyToken(body, args.relayInformationStore);
if (error instanceof Error) {
return new Response(JSON.stringify(error.message), { status: 400 });
} else {
const auth = btoa(JSON.stringify(body));
const headers = new Headers();
const cookie: Cookie = {
name: "token",
value: auth,
path: "/",
secure: true,
httpOnly: true,
sameSite: "Strict",
};
setCookie(headers, cookie);
const resp = new Response("", { status: 200, headers });
return resp;
}
return graphql_login_handler(args)(req);
}
if (url.pathname == "/api/members") {
const members = await args.get_space_members();
if (members instanceof Error) {
console.log(members);
return new Response("", { status: 500 });
}
const body = {
data: members.map((event) => event.member),
};
const resp = new Response(JSON.stringify(body), {
status: 200,
});
resp.headers.set("content-type", "application/json; charset=utf-8");
resp.headers.set("Access-Control-Allow-Origin", "*");
resp.headers.set("Access-Control-Allow-Methods", "GET");
resp.headers.set("Access-Control-Allow-Headers", "accept,content-type");
return resp;
return members_handler(args);
}
if (url.pathname == "/api") {
return graphql_handler(args)(req);
Expand All @@ -359,41 +323,7 @@ async (req: Request, info: Deno.ServeHandlerInfo) => {
}
}
} else if (req.method == "POST") {
const text = await req.text();
const event = parseJSON<Event_V2>(text);
if (event instanceof Error) {
return new Response(event.message, {
status: 400,
});
}
const ok = await verify_event_v2(event);
if (!ok) {
console.error("event", event);
return new Response("event is not valid", { status: 400 });
}
if (event.kind == Kind_V2.ChannelCreation) {
const ok = await args.create_channel(event);
if (ok) {
return new Response();
} else {
return new Response("failed to write event", { status: 400 });
}
} else if (event.kind == Kind_V2.ChannelEdition) {
const res = await args.edit_channel(event);
if (res instanceof Error) {
return new Response(res.message, { status: 400 });
}
return new Response();
} else if (event.kind == Kind_V2.SpaceMember) {
const res = await args.add_space_member(event);
if (res instanceof Error) {
console.error(res);
return new Response(res.message, { status: 400 });
}
return new Response();
} else {
return new Response(`not a recognizable event`, { status: 400 });
}
return event_v2_handler(args)(req);
}
}
const resp = new Response(render(Error404()), { status: 404 });
Expand Down Expand Up @@ -474,6 +404,51 @@ async (req: Request) => {
export const supported_nips = [1, 2, 11];
export const software = "https://github.com/BlowaterNostr/relayed";

const graphql_login_handler =
(args: { relayInformationStore: RelayInformationStore }) => async (req: Request) => {
const body = await req.json();
if (!body) {
return new Response(`{"errors":"request body is null"}`, { status: 400 });
}
const error = await verifyToken(body, args.relayInformationStore);
if (error instanceof Error) {
return new Response(JSON.stringify(error.message), { status: 400 });
} else {
const auth = btoa(JSON.stringify(body));
const headers = new Headers();
const cookie: Cookie = {
name: "token",
value: auth,
path: "/",
secure: true,
httpOnly: true,
sameSite: "Strict",
};
setCookie(headers, cookie);
const resp = new Response("", { status: 200, headers });
return resp;
}
};

const members_handler = async (args: { get_space_members: func_GetSpaceMembers }) => {
const members = await args.get_space_members();
if (members instanceof Error) {
console.log(members);
return new Response("", { status: 500 });
}
const body = {
data: members.map((event) => event.member),
};
const resp = new Response(JSON.stringify(body), {
status: 200,
});
resp.headers.set("content-type", "application/json; charset=utf-8");
resp.headers.set("Access-Control-Allow-Origin", "*");
resp.headers.set("Access-Control-Allow-Methods", "GET");
resp.headers.set("Access-Control-Allow-Headers", "accept,content-type");
return resp;
};

const landing_handler = async (args: { relayInformationStore: RelayInformationStore }) => {
const storeInformation = await args.relayInformationStore.resolveRelayInformation();
if (storeInformation instanceof Error) {
Expand Down Expand Up @@ -501,6 +476,49 @@ const information_handler = async (args: { relayInformationStore: RelayInformati
return resp;
};

const event_v2_handler = (args: {
create_channel: func_CreateChannel;
edit_channel: func_EditChannel;
add_space_member: func_AddSpaceMember;
}) =>
async (req: Request) => {
const text = await req.text();
const event = parseJSON<Event_V2>(text);
if (event instanceof Error) {
return new Response(event.message, {
status: 400,
});
}
const ok = await verify_event_v2(event);
if (!ok) {
console.error("event", event);
return new Response("event is not valid", { status: 400 });
}
if (event.kind == Kind_V2.ChannelCreation) {
const ok = await args.create_channel(event);
if (ok) {
return new Response();
} else {
return new Response("failed to write event", { status: 400 });
}
} else if (event.kind == Kind_V2.ChannelEdition) {
const res = await args.edit_channel(event);
if (res instanceof Error) {
return new Response(res.message, { status: 400 });
}
return new Response();
} else if (event.kind == Kind_V2.SpaceMember) {
const res = await args.add_space_member(event);
if (res instanceof Error) {
console.error(res);
return new Response(res.message, { status: 400 });
}
return new Response();
} else {
return new Response(`not a recognizable event`, { status: 400 });
}
};

async function verifyToken(event: NostrEvent, relayInformationStore: RelayInformationStore) {
if (!await verifyEvent(event)) {
return new Error("token not verified");
Expand Down
2 changes: 1 addition & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "../nostr.ts/nostr.ts";
import { prepareNormalNostrEvent } from "../nostr.ts/event.ts";
import { RelayRejectedEvent, SingleRelayConnection, SubscriptionStream } from "../nostr.ts/relay-single.ts";
import { PrivateKey, PublicKey } from "../nostr.ts/key.ts";
import { PrivateKey } from "../nostr.ts/key.ts";
import { sleep } from "https://raw.githubusercontent.com/BlowaterNostr/csp/master/csp.ts";
import { RFC3339 } from "../nostr.ts/_helper.ts";
import { format } from "https://deno.land/[email protected]/datetime/format.ts";
Expand Down
2 changes: 1 addition & 1 deletion ws.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// deno-lint-ignore-file
import {func_IsSpaceMember, func_ResolvePolicyByKind} from "./resolvers/policy.ts";
import { func_IsSpaceMember, func_ResolvePolicyByKind } from "./resolvers/policy.ts";
import { atobSafe, DefaultPolicy } from "./main.ts";
import { func_WriteRegularEvent, func_WriteReplaceableEvent } from "./resolvers/event.ts";

Expand Down

0 comments on commit 2d794e5

Please sign in to comment.