From 08dd5fa4ebece7a5051d981a9ba5c7c2b7a33887 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 4 Sep 2023 17:17:47 -0500 Subject: [PATCH] db/events: fix `local: false` filter --- src/db/events.test.ts | 19 +++++++++++++++++++ src/db/events.ts | 6 ++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/db/events.test.ts b/src/db/events.test.ts index 6193960..a9ab016 100644 --- a/src/db/events.test.ts +++ b/src/db/events.test.ts @@ -2,6 +2,7 @@ import event55920b75 from '~/fixtures/events/55920b75.json' assert { type: 'json import { assertEquals } from '@/deps-test.ts'; import { countFilters, deleteFilters, getFilters, insertEvent } from './events.ts'; +import { insertUser } from '@/db/users.ts'; Deno.test('count filters', async () => { assertEquals(await countFilters([{ kinds: [1] }]), 0); @@ -28,3 +29,21 @@ Deno.test('delete events', async () => { await deleteFilters([{ kinds: [1] }]); assertEquals(await getFilters([{ kinds: [1] }]), []); }); + +Deno.test('query events with local filter', async () => { + await insertEvent(event55920b75); + + assertEquals(await getFilters([{}]), [event55920b75]); + assertEquals(await getFilters([{ local: true }]), []); + assertEquals(await getFilters([{ local: false }]), [event55920b75]); + + await insertUser({ + username: 'alex', + pubkey: event55920b75.pubkey, + inserted_at: new Date(), + admin: 0, + }); + + assertEquals(await getFilters([{ local: true }]), [event55920b75]); + assertEquals(await getFilters([{ local: false }]), []); +}); diff --git a/src/db/events.ts b/src/db/events.ts index 3edefec..59480d3 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -115,8 +115,10 @@ function getFilterQuery(filter: DittoFilter) { } } - if (filter.local) { - query = query.innerJoin('users', 'users.pubkey', 'events.pubkey'); + if (typeof filter.local === 'boolean') { + query = filter.local + ? query.innerJoin('users', 'users.pubkey', 'events.pubkey') + : query.leftJoin('users', 'users.pubkey', 'events.pubkey').where('users.pubkey', 'is', null); } if (filter.search) {