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 7e9da24 commit 50f83ca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
10 changes: 8 additions & 2 deletions main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ 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 { NostrEvent, NostrKind, parseJSON, PublicKey, verifyEvent } from "./_libs.ts";
import { NostrKind, PublicKey } from "./_libs.ts";
import { PolicyStore } from "./resolvers/policy.ts";
import { Policies } from "./resolvers/policy.ts";
import { func_WriteReplaceableEvent, interface_GetEventsByAuthors } from "./resolvers/event.ts";
import {
func_GetReplaceableEvents,
func_WriteReplaceableEvent,
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";
Expand Down Expand Up @@ -98,6 +102,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),
get_replaceable_events: eventStore.get_replaceable_events.bind(eventStore),
mark_event_deleted: eventStore.mark_event_deleted,
write_regular_event: eventStore.write_regular_event.bind(eventStore),
write_replaceable_event: eventStore.write_replaceable_event,
Expand Down Expand Up @@ -130,6 +135,7 @@ export type EventReadWriter = {
get_events_by_kinds: func_GetEventsByKinds;
get_events_by_filter: func_GetEventsByFilter;
mark_event_deleted: func_MarkEventDeleted;
get_replaceable_events: func_GetReplaceableEvents;
} & interface_GetEventsByAuthors;

const root_handler = (
Expand Down
24 changes: 23 additions & 1 deletion resolvers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export type func_GetEventsByFilter = (filter: NostrFilter) => AsyncIterable<Nost
export type func_WriteRegularEvent = (event: NostrEvent) => Promise<boolean>;

export type func_WriteReplaceableEvent = (event: NostrEvent) => Promise<boolean>;
export type func_GetReplaceableEvents = (args: {
kinds: NostrKind[];
authors: string[];
}) => AsyncIterable<NostrEvent>;

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

Expand Down Expand Up @@ -96,6 +100,24 @@ export class EventStore implements EventReadWriter {
}
}

async *get_replaceable_events(args: {
kinds: NostrKind[];
authors: string[];
}) {
const keys: Deno.KvKey[] = [];
for (const kind of args.kinds) {
for (const author of args.authors) {
keys.push(["event", kind, author]);
}
}
const results = await this.kv.getMany<NostrEvent[]>(keys);
for (const result of results) {
if (result.value) {
yield result.value;
}
}
}

write_regular_event = async (event: NostrEvent) => {
if (isReplaceableEvent(event.kind)) {
return false;
Expand Down Expand Up @@ -147,7 +169,7 @@ export class EventStore implements EventReadWriter {
};
}

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

Expand Down
35 changes: 23 additions & 12 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
import { func_ResolvePolicyByKind } from "./resolvers/policy.ts";
import { DefaultPolicy, EventReadWriter } from "./main.tsx";
import {
func_GetEventsByAuthors,
func_GetEventsByFilter,
func_GetEventsByIDs,
func_GetEventsByKinds,
func_MarkEventDeleted,
func_WriteRegularEvent,
func_WriteReplaceableEvent,
isReplaceableEvent,
} from "./resolvers/event.ts";
import {
_RelayResponse_EOSE,
Expand Down Expand Up @@ -234,17 +231,31 @@ async function handle_cmd_req(
return send(this_socket, JSON.stringify(respond_eose(sub_id)));
}

async function handle_filter(args: {
filter: NostrFilter;
get_events_by_IDs: func_GetEventsByIDs;
get_events_by_kinds: func_GetEventsByKinds;
get_events_by_authors: func_GetEventsByAuthors;
get_events_by_filter: func_GetEventsByFilter;
resolvePolicyByKind: func_ResolvePolicyByKind;
}) {
async function handle_filter(
args: {
filter: NostrFilter;
resolvePolicyByKind: func_ResolvePolicyByKind;
} & EventReadWriter,
) {
const event_candidates = new Map<string, NostrEvent>();
const { filter, get_events_by_IDs, resolvePolicyByKind, get_events_by_kinds } = args;

if (filter.kinds) {
const replaceable_kinds: NostrKind[] = [];
for (const kind of filter.kinds) {
if (isReplaceableEvent(kind)) {
replaceable_kinds.push(kind);
}
}
const events = args.get_replaceable_events({
authors: filter.authors || [],
kinds: replaceable_kinds,
});
for await (const event of events) {
event_candidates.set(event.id, event);
}
}

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 50f83ca

Please sign in to comment.