From e377d7b3cedc3f8918dd9887407f0b302cf49ed2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 4 Sep 2023 12:58:35 -0500 Subject: [PATCH] events/db: add a deleteFilters function to delete all events from the local db matching the filters --- src/db/events.test.ts | 15 ++++++++++++++- src/db/events.ts | 22 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/db/events.test.ts b/src/db/events.test.ts index 94a4e23..6193960 100644 --- a/src/db/events.test.ts +++ b/src/db/events.test.ts @@ -1,7 +1,13 @@ import event55920b75 from '~/fixtures/events/55920b75.json' assert { type: 'json' }; import { assertEquals } from '@/deps-test.ts'; -import { getFilters, insertEvent } from './events.ts'; +import { countFilters, deleteFilters, getFilters, insertEvent } from './events.ts'; + +Deno.test('count filters', async () => { + assertEquals(await countFilters([{ kinds: [1] }]), 0); + await insertEvent(event55920b75); + assertEquals(await countFilters([{ kinds: [1] }]), 1); +}); Deno.test('insert and filter events', async () => { await insertEvent(event55920b75); @@ -15,3 +21,10 @@ Deno.test('insert and filter events', async () => { [event55920b75], ); }); + +Deno.test('delete events', async () => { + await insertEvent(event55920b75); + assertEquals(await getFilters([{ kinds: [1] }]), [event55920b75]); + await deleteFilters([{ kinds: [1] }]); + assertEquals(await getFilters([{ kinds: [1] }]), []); +}); diff --git a/src/db/events.ts b/src/db/events.ts index 1b30f5b..62db3d5 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -128,13 +128,18 @@ function getFilterQuery(filter: DittoFilter) { return query; } +/** Combine filter queries into a single union query. */ +function getFiltersQuery(filters: DittoFilter[]) { + return filters.map(getFilterQuery).reduce((acc, curr) => acc.union(curr)); +} + /** Get events for filters from the database. */ async function getFilters( filters: DittoFilter[], opts: GetFiltersOpts = {}, ): Promise[]> { if (!filters.length) return Promise.resolve([]); - let query = filters.map(getFilterQuery).reduce((acc, curr) => acc.union(curr)); + let query = getFiltersQuery(filters); if (typeof opts.limit === 'number') { query = query.limit(opts.limit); @@ -145,10 +150,21 @@ async function getFilters( )); } +/** Delete events based on filters from the database. */ +function deleteFilters(filters: DittoFilter[]) { + if (!filters.length) return Promise.resolve(); + const query = getFiltersQuery(filters); + + return db + .deleteFrom('events') + .where('id', 'in', () => query.clearSelect().select('id')) + .execute(); +} + /** Get number of events that would be returned by filters. */ async function countFilters(filters: DittoFilter[]): Promise { if (!filters.length) return Promise.resolve(0); - const query = filters.map(getFilterQuery).reduce((acc, curr) => acc.union(curr)); + const query = getFiltersQuery(filters); const [{ count }] = await query .clearSelect() @@ -176,4 +192,4 @@ function buildUserSearchContent(event: Event<0>): string { return [name, nip05, about].filter(Boolean).join('\n'); } -export { countFilters, getFilters, insertEvent }; +export { countFilters, deleteFilters, getFilters, insertEvent };