Track pubkey domains

This commit is contained in:
Alex Gleason 2024-03-19 17:45:19 -05:00
parent d9056f66c6
commit e2cc6132be
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 60 additions and 0 deletions

View File

@ -14,6 +14,7 @@ interface DittoDB {
unattached_media: UnattachedMediaRow; unattached_media: UnattachedMediaRow;
author_stats: AuthorStatsRow; author_stats: AuthorStatsRow;
event_stats: EventStatsRow; event_stats: EventStatsRow;
pubkey_domains: PubkeyDomainRow;
} }
interface AuthorStatsRow { interface AuthorStatsRow {
@ -66,6 +67,11 @@ interface UnattachedMediaRow {
uploaded_at: Date; uploaded_at: Date;
} }
interface PubkeyDomainRow {
pubkey: string;
domain: string;
}
const sqliteWorker = new SqliteWorker(); const sqliteWorker = new SqliteWorker();
await sqliteWorker.open(Conf.dbPath); await sqliteWorker.open(Conf.dbPath);

View File

@ -0,0 +1,21 @@
import { Kysely } from '@/deps.ts';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('pubkey_domains')
.ifNotExists()
.addColumn('pubkey', 'text', (col) => col.primaryKey())
.addColumn('domain', 'text')
.execute();
await db.schema
.createIndex('pubkey_domains_domain_index')
.on('pubkey_domains')
.column('domain')
.ifNotExists()
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('pubkey_domains').execute();
}

View File

@ -1,4 +1,6 @@
import { NSchema as n } from '@soapbox/nspec';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { db } from '@/db.ts';
import { addRelays } from '@/db/relays.ts'; import { addRelays } from '@/db/relays.ts';
import { deleteAttachedMedia } from '@/db/unattached-media.ts'; import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { Debug, LNURL, type NostrEvent } from '@/deps.ts'; import { Debug, LNURL, type NostrEvent } from '@/deps.ts';
@ -15,6 +17,7 @@ import { TrendsWorker } from '@/workers/trends.ts';
import { verifyEventWorker } from '@/workers/verify.ts'; import { verifyEventWorker } from '@/workers/verify.ts';
import { AdminSigner } from '@/signers/AdminSigner.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts';
import { lnurlCache } from '@/utils/lnurl.ts'; import { lnurlCache } from '@/utils/lnurl.ts';
import { nip05Cache } from '@/utils/nip05.ts';
const debug = Debug('ditto:pipeline'); const debug = Debug('ditto:pipeline');
@ -30,6 +33,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
await Promise.all([ await Promise.all([
storeEvent(event, signal), storeEvent(event, signal),
parseMetadata(event, signal),
processDeletions(event, signal), processDeletions(event, signal),
trackRelays(event), trackRelays(event),
trackHashtags(event), trackHashtags(event),
@ -74,6 +78,35 @@ async function storeEvent(event: DittoEvent, signal?: AbortSignal): Promise<void
} }
} }
/** Parse kind 0 metadata and track indexes in the database. */
async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<void> {
if (event.kind !== 0) return;
// Parse metadata.
const metadata = n.json().pipe(n.metadata()).safeParse(event.content);
if (!metadata.success) return;
// Get nip05.
const { nip05 } = metadata.data;
if (!nip05) return;
// Fetch nip05.
const result = await nip05Cache.fetch(nip05, { signal }).catch(() => undefined);
if (!result) return;
// Ensure pubkey matches event.
const { pubkey } = result;
if (pubkey !== event.pubkey) return;
// Track pubkey domain.
const [, domain] = nip05.split('@');
await db
.insertInto('pubkey_domains')
.values({ pubkey, domain })
.execute()
.catch(debug);
}
/** Query to-be-deleted events, ensure their pubkey matches, then delete them from the database. */ /** Query to-be-deleted events, ensure their pubkey matches, then delete them from the database. */
async function processDeletions(event: NostrEvent, signal: AbortSignal): Promise<void> { async function processDeletions(event: NostrEvent, signal: AbortSignal): Promise<void> {
if (event.kind === 5) { if (event.kind === 5) {