Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement NIP-9 #7

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import { SubscriptionMap, ws_handler } from "./ws.ts";
import { render } from "https://esm.sh/[email protected]";
import { RootResolver } from "./resolvers/root.ts";
import * as gql from "https://esm.sh/[email protected]";

import { Policy } from "./resolvers/policy.ts";
import { func_ResolvePolicyByKind } from "./resolvers/policy.ts";
import { func_GetEventsByKinds, func_WriteEvent } from "./resolvers/event.ts";
import { EventStore, func_GetEventsByIDs } 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";
import { interface_GetEventsByAuthors } from "./resolvers/event.ts";
import Landing from "./routes/landing.tsx";
import Error404 from "./routes/_404.tsx";
import { RelayInformation, RelayInformationStore } from "./resolvers/nip11.ts";
import { func_GetEventsByFilter } from "./resolvers/event.ts";
import {
EventStore,
func_GetEventsByFilter,
func_GetEventsByIDs,
func_GetEventsByKinds,
func_MarkEventDeleted,
func_WriteEvent,
} from "./resolvers/event.ts";

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

Expand Down Expand Up @@ -95,6 +99,7 @@ export async function run(args: {
get_events_by_kinds: eventStore.get_events_by_kinds.bind(eventStore),
get_events_by_authors: eventStore.get_events_by_authors.bind(eventStore),
get_events_by_filter: eventStore.get_events_by_filter.bind(eventStore),
mark_event_deleted: eventStore.mark_event_deleted,
policyStore,
relayInformationStore,
kv: args.kv,
Expand Down Expand Up @@ -122,6 +127,7 @@ export type EventReadWriter = {
get_events_by_IDs: func_GetEventsByIDs;
get_events_by_kinds: func_GetEventsByKinds;
get_events_by_filter: func_GetEventsByFilter;
mark_event_deleted: func_MarkEventDeleted;
} & interface_GetEventsByAuthors;

const root_handler = (
Expand Down
17 changes: 17 additions & 0 deletions resolvers/event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NoteID } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nip19.ts";
import {
InMemoryAccountContext,
NostrEvent,
Expand Down Expand Up @@ -42,6 +43,8 @@ export type interface_WriteEvent = {
write_event: func_WriteEvent;
};

export type func_MarkEventDeleted = (event: NostrEvent | NoteID) => Promise<boolean>;

export class EventStore implements EventReadWriter {
private constructor(
private events: Map<string, NostrEvent>,
Expand Down Expand Up @@ -108,6 +111,20 @@ 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
} else {
id = event_or_id.id
}
const result = await this.kv.set(["event", "deleted", id], id);
if (result.ok) {
this.events.delete(id);
}
return result.ok;
};
}

function isMatched(event: NostrEvent, filter: NostrFilter) {
Expand Down
19 changes: 19 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SubscriptionStream,
} from "./_libs.ts";
import * as client_test from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/relay-single-test.ts";
import * as client_nip9_test from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nip9-test.ts";

const test_kv = async () => {
try {
Expand Down Expand Up @@ -151,6 +152,24 @@ Deno.test("main", async (t) => {
await relay.shutdown();
});

// https://github.com/nostr-protocol/nips/blob/master/09.md
Deno.test("NIP-9: Deletion", async () => {
const relay = await run({
password: "123",
port: 8080,
default_policy: {
allowed_kinds: "all",
},
kv: await test_kv(),
}) as Relay;
const client = SingleRelayConnection.New(relay.url, { log: false });
{
await client_nip9_test.send_deletion_event(relay.url)();
}
await client.close();
await relay.shutdown();
});

// https://github.com/nostr-protocol/nips/blob/master/11.md
Deno.test("NIP-11: Relay Information Document", async (t) => {
const relay = await run({
Expand Down
13 changes: 13 additions & 0 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
func_GetEventsByFilter,
func_GetEventsByIDs,
func_GetEventsByKinds,
func_MarkEventDeleted,
func_WriteEvent,
} from "./resolvers/event.ts";
import {
Expand All @@ -16,11 +17,14 @@ import {
ClientRequest_Event,
ClientRequest_Message,
ClientRequest_REQ,
getTags,
NostrEvent,
NostrFilter,
NostrKind,
PublicKey,
verifyEvent,
} from "./_libs.ts";
import { NoteID } from "https://raw.githubusercontent.com/BlowaterNostr/nostr.ts/main/nip19.ts";

export const ws_handler = (
args: {
Expand Down Expand Up @@ -111,6 +115,7 @@ async function handle_cmd_event(args: {
nostr_ws_msg: ClientRequest_Event;
resolvePolicyByKind: func_ResolvePolicyByKind;
write_event: func_WriteEvent;
mark_event_deleted: func_MarkEventDeleted;
}) {
const { this_socket, connections, nostr_ws_msg, resolvePolicyByKind, write_event } = args;
const event = nostr_ws_msg[1];
Expand Down Expand Up @@ -154,6 +159,14 @@ 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 write_event(event);
if (_ok) {
send(this_socket, JSON.stringify(respond_ok(event, true, "")));
Expand Down
Loading