import { type Event } from 'nostr-tools' import { TagType, StoreKind } from '../enum' import { type Store } from '../service' import { getTagValues } from '../util' import { EventKindHandler } from './EventKindHandler' export class EventDeletionHandler extends EventKindHandler { private readonly stores: Map constructor(stores: Map) { super() this.stores = stores } protected async _handle(deletionEvents: Array>): Promise>> { const rawEventsToDelete: Array> = [] const payloadMap = new Map() const eventsStore = this.stores.get(StoreKind.Events)! for (const deletionEvent of deletionEvents) { const ids: string[] = getTagValues(deletionEvent, TagType.Event); const rawEvents: Array> = eventsStore.getManyAsArray(ids) rawEvents.forEach((event) => { if (deletionEvent.pubkey === event.pubkey) { const { kind, id } = event; const storeKind = this.stores.get(kind)!; const storedValue = storeKind.get(id); if (storeKind) { storeKind.delete(storedValue); rawEventsToDelete.push(event); const existingPayload = payloadMap.get(kind) || []; existingPayload.push(id); payloadMap.set(kind, existingPayload); } } }); } eventsStore.delete(rawEventsToDelete) return [payloadMap] } }