db/events: fix `local: false` filter

This commit is contained in:
Alex Gleason 2023-09-04 17:17:47 -05:00
parent 8ec215402f
commit 08dd5fa4eb
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 23 additions and 2 deletions

View File

@ -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 }]), []);
});

View File

@ -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) {