feat(snapshot): NIP-09-filter

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jörg Lohrer 2026-04-28 08:09:37 +02:00
parent 300cd9bea9
commit ccd7daf14d
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,22 @@
import type { SignedEvent } from './types.ts'
export function filterDeleted(
events: SignedEvent[],
deletions: SignedEvent[],
authorPubkey: string,
): SignedEvent[] {
const deletedCoords = new Set<string>()
for (const del of deletions) {
if (del.kind !== 5) continue
if (del.pubkey !== authorPubkey) continue
for (const tag of del.tags) {
if (tag[0] === 'a' && tag[1]) deletedCoords.add(tag[1])
}
}
return events.filter((ev) => {
const d = ev.tags.find((t) => t[0] === 'd')?.[1]
if (!d) return true
const coord = `${ev.kind}:${ev.pubkey}:${d}`
return !deletedCoords.has(coord)
})
}

View File

@ -0,0 +1,30 @@
import { assertEquals } from '@std/assert'
import { filterDeleted } from '../src/core/nip09-filter.ts'
import type { SignedEvent } from '../src/core/types.ts'
function post(d: string, id: string): SignedEvent {
return { id, pubkey: 'P', created_at: 1, kind: 30023, sig: 's', content: '', tags: [['d', d]] }
}
function deletion(coords: string[]): SignedEvent {
return {
id: 'del', pubkey: 'P', created_at: 2, kind: 5, sig: 's', content: '',
tags: coords.map((c) => ['a', c]),
}
}
Deno.test('filterDeleted entfernt events deren coord in einem kind:5 referenziert ist', () => {
const out = filterDeleted(
[post('alive', 'a'), post('dead', 'b')],
[deletion(['30023:P:dead'])],
'P',
)
assertEquals(out.map((e) => e.id), ['a'])
})
Deno.test('filterDeleted ignoriert kind:5 fremder pubkeys', () => {
const fremde: SignedEvent = {
...deletion(['30023:P:alive']), pubkey: 'OTHER',
}
const out = filterDeleted([post('alive', 'a')], [fremde], 'P')
assertEquals(out.length, 1)
})