Scaffold db, handler, query
This commit is contained in:
parent
ec12da2710
commit
c3b8ad1c28
|
@ -0,0 +1,7 @@
|
||||||
|
import { lmdb } from '@/deps.ts';
|
||||||
|
|
||||||
|
const db = lmdb.open('db', {});
|
||||||
|
|
||||||
|
const gossipDB = db.openDB('gossip', { dupSort: true });
|
||||||
|
|
||||||
|
export { db, gossipDB };
|
|
@ -1,7 +1,6 @@
|
||||||
import { Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
|
export { type Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
|
||||||
export { Hono, validator };
|
|
||||||
export { cors } from 'https://deno.land/x/hono@v3.0.2/middleware.ts';
|
export { cors } from 'https://deno.land/x/hono@v3.0.2/middleware.ts';
|
||||||
export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts';
|
export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts';
|
||||||
export { Author, RelayPool } from 'https://dev.jspm.io/nostr-relaypool@0.5.3';
|
export { Author, RelayPool } from 'https://dev.jspm.io/nostr-relaypool@0.5.3';
|
||||||
export { getEventHash, getPublicKey, nip19, signEvent } from 'npm:nostr-tools@^1.7.4';
|
export { type Filter, getEventHash, getPublicKey, nip19, signEvent } from 'npm:nostr-tools@^1.7.4';
|
||||||
export type { Context };
|
export { default as lmdb } from 'npm:lmdb';
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { parseRelay } from './schema.ts';
|
||||||
|
|
||||||
|
import type { Event } from './event.ts';
|
||||||
|
|
||||||
|
/** Gets relays which pertain to the author from the event. */
|
||||||
|
function getAuthorRelays(event: Event): URL[] {
|
||||||
|
const relays: string[] = [];
|
||||||
|
|
||||||
|
switch (event.kind) {
|
||||||
|
case 10002:
|
||||||
|
event.tags.forEach((tag) => tag[0] === 'r' && relays.push(tag[1]));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
relays.push(event.content);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return relays.map(parseRelay).filter((r): r is URL => !!r);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { getAuthorRelays };
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { gossipDB } from '@/db.ts';
|
||||||
|
|
||||||
|
import { getAuthorRelays } from './gossip.ts';
|
||||||
|
|
||||||
|
import type { Event } from './event.ts';
|
||||||
|
|
||||||
|
function handleEvent(event: Event): void {
|
||||||
|
handleRelays(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Add author relays into the database. */
|
||||||
|
function handleRelays(event: Event): Promise<boolean[]> {
|
||||||
|
const relays = getAuthorRelays(event);
|
||||||
|
return Promise.all(
|
||||||
|
relays.map((relay) => gossipDB.put(event.pubkey, relay.toString())),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { handleEvent };
|
|
@ -0,0 +1,7 @@
|
||||||
|
import type { Filter } from '@/deps.ts';
|
||||||
|
|
||||||
|
function query(_filter: Filter, _relays?: URL[]) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
export { query };
|
|
@ -22,5 +22,23 @@ const metaContentSchema = z.object({
|
||||||
|
|
||||||
type MetaContent = z.infer<typeof metaContentSchema>;
|
type MetaContent = z.infer<typeof metaContentSchema>;
|
||||||
|
|
||||||
export { jsonSchema, metaContentSchema };
|
/** It's like `safeParse` except it returns `T` on success and `undefined` on fail. */
|
||||||
|
function parseish<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
|
||||||
|
const result = schema.safeParse(value);
|
||||||
|
return result.success ? result.data : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parseRelay = (relay: string | URL) => parseish(relaySchema, relay);
|
||||||
|
|
||||||
|
const relaySchema = z.custom<URL>((relay) => {
|
||||||
|
if (typeof relay !== 'string') return false;
|
||||||
|
try {
|
||||||
|
const { protocol } = new URL(relay);
|
||||||
|
return protocol === 'wss:' || protocol === 'ws:';
|
||||||
|
} catch (_e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export { jsonSchema, metaContentSchema, parseRelay, relaySchema };
|
||||||
export type { MetaContent };
|
export type { MetaContent };
|
||||||
|
|
Loading…
Reference in New Issue