Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
BlowaterNostr committed Apr 14, 2024
1 parent fd3f544 commit 7e9da24
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
17 changes: 1 addition & 16 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function run(args: {

export type EventReadWriter = {
write_regular_event: func_WriteRegularEvent;
write_replaceable_event: func_WriteReplaceableEvent
write_replaceable_event: func_WriteReplaceableEvent;
get_events_by_IDs: func_GetEventsByIDs;
get_events_by_kinds: func_GetEventsByKinds;
get_events_by_filter: func_GetEventsByFilter;
Expand Down Expand Up @@ -179,31 +179,16 @@ async (req: Request) => {
const { password, policyStore } = args;
if (req.method == "POST") {
const query = await req.json();
const nip42 = req.headers.get("nip42");
console.log("nip42 header", nip42);

const pw = req.headers.get("password");
if (pw != password) {
return new Response(`{"errors":"incorrect password"}`);
}

if (nip42) {
const auth_event = parseJSON<NostrEvent>(nip42);
if (auth_event instanceof Error) {
return new Response(`{errors:["no auth"]}`);
}
const ok = await verifyEvent(auth_event);
if (!ok) {
return new Response(`{"errors":["no auth"]}`);
}
}
const result = await gql.graphql({
schema: schema,
source: query.query,
variableValues: query.variables,
rootValue: RootResolver(args),
});
console.log(result);
return new Response(JSON.stringify(result));
} else if (req.method == "GET") {
const res = new Response(graphiql);
Expand Down
1 change: 0 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ fmt:

test: fmt
deno test --allow-net --unstable --allow-read --allow-write \
--filter main \
--coverage test.ts
16 changes: 8 additions & 8 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class EventStore implements EventReadWriter {
}

write_regular_event = async (event: NostrEvent) => {
if(isReplaceableEvent(event.kind)) {
if (isReplaceableEvent(event.kind)) {
return false;
}
console.log("write_event", event);
Expand All @@ -112,11 +112,11 @@ export class EventStore implements EventReadWriter {
}

return result.ok;
}
};

write_replaceable_event = async (event: NostrEvent) => {
const kind = event.kind;
if(!isReplaceableEvent(kind)) {
if (!isReplaceableEvent(kind)) {
return false;
}
console.log("write_replaceable_event", event);
Expand All @@ -130,14 +130,14 @@ export class EventStore implements EventReadWriter {
}

return result.ok;
}
};

mark_event_deleted = async (event_or_id: NostrEvent | NoteID) => {
let id: string;
if(event_or_id instanceof NoteID) {
id = event_or_id.hex
if (event_or_id instanceof NoteID) {
id = event_or_id.hex;
} else {
id = event_or_id.id
id = event_or_id.id;
}
const result = await this.kv.set(["event", "deleted", id], id);
if (result.ok) {
Expand All @@ -148,7 +148,7 @@ export class EventStore implements EventReadWriter {
}

function isReplaceableEvent(kind: NostrKind) {
return kind == NostrKind.META_DATA || kind == NostrKind.CONTACTS || (10000 <= kind && kind < 20000)
return kind == NostrKind.META_DATA || kind == NostrKind.CONTACTS || (10000 <= kind && kind < 20000);
}

function isMatched(event: NostrEvent, filter: NostrFilter) {
Expand Down
10 changes: 4 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Deno.test("main", async (t) => {
});

// https://github.com/nostr-protocol/nips/blob/master/01.md#kinds
Deno.test("replaceable events", async () => {
Deno.test("replaceable events", async (t) => {
const relay = await run({
password: "123",
port: 8080,
Expand All @@ -163,12 +163,12 @@ Deno.test("replaceable events", async () => {
kv: await test_kv(),
}) as Relay;
const client = SingleRelayConnection.New(relay.url, { log: false });
{
await t.step("get_replaceable_event", async () => {
await client_test.get_replaceable_event(relay.url)();
}
});
await client.close();
await relay.shutdown();
})
});

// https://github.com/nostr-protocol/nips/blob/master/09.md
Deno.test("NIP-9: Deletion", async () => {
Expand Down Expand Up @@ -204,8 +204,6 @@ Deno.test("NIP-11: Relay Information Document", async (t) => {

await t.step("get relay information", async () => {
const information = await relay.get_relay_information();
console.log(`information`, information);

assertEquals(information, { name: "Nostr Relay", ...not_modifiable_information });
});

Expand Down
16 changes: 10 additions & 6 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,23 @@ async function handle_cmd_event(args: {

if (event.kind == NostrKind.DELETE) {
for (const e of getTags(event).e) {
const ok = await args.mark_event_deleted(NoteID.FromHex(e))
if(!ok) {
console.error("failed to delete", e)
const ok = await args.mark_event_deleted(NoteID.FromHex(e));
if (!ok) {
console.error("failed to delete", e);
}
}
}

let ok: boolean;
if (event.kind == NostrKind.META_DATA || event.kind == NostrKind.CONTACTS || (10000 <= event.kind && event.kind < 20000)) {
ok = await args.write_replaceable_event(event)
if (
event.kind == NostrKind.META_DATA || event.kind == NostrKind.CONTACTS ||
(10000 <= event.kind && event.kind < 20000)
) {
ok = await args.write_replaceable_event(event);
} else {
ok = await args.write_regular_event(event);
}

if (ok) {
send(this_socket, JSON.stringify(respond_ok(event, true, "")));
} else {
Expand Down Expand Up @@ -241,6 +244,7 @@ async function handle_filter(args: {
}) {
const event_candidates = new Map<string, NostrEvent>();
const { filter, get_events_by_IDs, resolvePolicyByKind, get_events_by_kinds } = args;

if (filter.ids) {
const events = get_events_by_IDs(new Set(filter.ids));
for await (const event of events) {
Expand Down

0 comments on commit 7e9da24

Please sign in to comment.