From 9492b7654f19fe700878f383512c7b673e87536c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 29 Dec 2023 20:53:01 -0600 Subject: [PATCH] db/events: fix the `local` filter --- src/db/events.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/db/events.ts b/src/db/events.ts index 0c03157..00187a4 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -154,11 +154,11 @@ function getFilterQuery(filter: DittoFilter): EventQuery { } } - // FIXME: local filtering is broken. if (typeof filter.local === 'boolean') { query = filter.local - ? query.innerJoin('users', 'users.pubkey', 'events.pubkey') as typeof query - : query.leftJoin('users', 'users.pubkey', 'events.pubkey').where('users.pubkey', 'is', null) as typeof query; + ? query.innerJoin(usersQuery, (join) => join.onRef('users.d_tag', '=', 'events.pubkey')) + : query.leftJoin(usersQuery, (join) => join.onRef('users.d_tag', '=', 'events.pubkey')) + .where('users.d_tag', 'is', null); } if (filter.relations?.includes('author')) { @@ -220,6 +220,15 @@ function getEventsQuery(filters: DittoFilter[]) { .reduce((result, query) => result.unionAll(query)); } +/** Query to get user events, joined by tags. */ +function usersQuery() { + return getFilterQuery({ kinds: [30361], authors: [Conf.pubkey] }) + .leftJoin('tags', 'tags.event_id', 'events.id') + .where('tags.tag', '=', 'd') + .select('tags.value as d_tag') + .as('users'); +} + /** Get events for filters from the database. */ async function getEvents( filters: DittoFilter[],