EventsDB: fix plaintext search queries

This commit is contained in:
Alex Gleason 2024-10-13 16:22:03 -05:00
parent a7bca0bdff
commit feec343d5a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 24 additions and 1 deletions

View file

@ -221,3 +221,26 @@ Deno.test("throws a RelayError when querying an event with a large 'kind'", asyn
'kind filter too far into the future', 'kind filter too far into the future',
); );
}); });
Deno.test('NPostgres.query with search', async (t) => {
await using db = await createTestDB({ pure: true });
const { store } = db;
const eventA = genEvent({ kind: 1, content: 'Fediverse is vegan' });
const eventB = genEvent({ kind: 1, content: 'Im vegan btw' });
await store.event(eventA);
await store.event(eventB);
await t.step('match single event', async () => {
assertEquals(await store.query([{ search: 'Fediverse' }]), [eventA]);
});
await t.step('match multiple events', async () => {
assertEquals(await store.query([{ search: 'vegan' }]), [eventA, eventB]);
});
await t.step("don't match nonsense queries", async () => {
assertEquals(await store.query([{ search: "this shouldn't match" }]), []);
});
});

View file

@ -337,7 +337,7 @@ class EventsDB extends NPostgres {
// Re-serialize the search string without the domain key. :facepalm: // Re-serialize the search string without the domain key. :facepalm:
filter.search = tokens filter.search = tokens
.filter((t) => typeof t === 'object' && t.key !== 'domain') .filter((t) => typeof t === 'string' || typeof t === 'object' && t.key !== 'domain')
.map((t) => typeof t === 'object' ? `${t.key}:${t.value}` : t) .map((t) => typeof t === 'object' ? `${t.key}:${t.value}` : t)
.join(' '); .join(' ');
} }