pipeline: ensure event doesn't already exist in DB

This commit is contained in:
Alex Gleason 2024-06-08 12:22:00 -05:00
parent b9922f96a0
commit a30cdec79b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 8 additions and 0 deletions

View File

@ -35,6 +35,7 @@ async function handleEvent(event: DittoEvent, signal: AbortSignal): Promise<void
} }
if (!(await verifyEventWorker(event))) return; if (!(await verifyEventWorker(event))) return;
if (encounterEvent(event)) return; if (encounterEvent(event)) return;
if (await existsInDB(event)) return;
debug(`NostrEvent<${event.kind}> ${event.id}`); debug(`NostrEvent<${event.kind}> ${event.id}`);
if (event.kind !== 24133) { if (event.kind !== 24133) {
@ -84,6 +85,13 @@ function encounterEvent(event: NostrEvent): boolean {
return encountered; return encountered;
} }
/** Check if the event already exists in the database. */
async function existsInDB(event: DittoEvent): Promise<boolean> {
const store = await Storages.db();
const events = await store.query([{ ids: [event.id], limit: 1 }]);
return events.length > 0;
}
/** Hydrate the event with the user, if applicable. */ /** Hydrate the event with the user, if applicable. */
async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> { async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise<void> {
await hydrateEvents({ events: [event], store: await Storages.db(), signal }); await hydrateEvents({ events: [event], store: await Storages.db(), signal });