Add last_updated_at column to pubkey_domains

This commit is contained in:
Alex Gleason 2024-04-05 21:41:55 -05:00
parent 8801debb1a
commit c0c586b364
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 24 additions and 7 deletions

View File

@ -70,6 +70,7 @@ interface UnattachedMediaRow {
interface PubkeyDomainRow {
pubkey: string;
domain: string;
last_updated_at: number;
}
const sqliteWorker = new SqliteWorker();

View File

@ -0,0 +1,12 @@
import { Kysely } from '@/deps.ts';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pubkey_domains')
.addColumn('last_updated_at', 'integer', (col) => col.notNull().defaultTo(0))
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.alterTable('pubkey_domains').dropColumn('last_updated_at').execute();
}

View File

@ -3,7 +3,7 @@ import { Conf } from '@/config.ts';
import { db } from '@/db.ts';
import { addRelays } from '@/db/relays.ts';
import { deleteAttachedMedia } from '@/db/unattached-media.ts';
import { Debug, LNURL, type NostrEvent } from '@/deps.ts';
import { Debug, LNURL, type NostrEvent, sql } from '@/deps.ts';
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { isEphemeralKind } from '@/kinds.ts';
import { DVM } from '@/pipeline/DVM.ts';
@ -109,11 +109,15 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
// Track pubkey domain.
try {
const { domain } = parseNip05(nip05);
await db
.insertInto('pubkey_domains')
.values({ pubkey, domain })
.execute()
.catch(debug);
await sql`
INSERT INTO pubkey_domains (pubkey, domain, last_updated_at)
VALUES (${pubkey}, ${domain}, ${event.created_at})
ON CONFLICT(pubkey) DO UPDATE SET
domain = excluded.domain,
last_updated_at = excluded.last_updated_at
WHERE excluded.last_updated_at > pubkey_domains.last_updated_at
`.execute(db);
} catch (_e) {
// do nothing
}

View File

@ -36,7 +36,7 @@ Deno.test('query events with domain search filter', async () => {
await db
.insertInto('pubkey_domains')
.values({ pubkey: event1.pubkey, domain: 'localhost:8000' })
.values({ pubkey: event1.pubkey, domain: 'localhost:8000', last_updated_at: event1.created_at })
.execute();
assertEquals(await eventsDB.query([{ kinds: [1], search: 'domain:localhost:8000' }]), [event1]);