From e1ae37dbdba04921019f1657bf73ab9b022da9e9 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 18 Nov 2023 21:55:28 +0100 Subject: [PATCH] [backend] Improve postgres FTS filters --- .../server/api/common/generate-fts-query.ts | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/server/api/common/generate-fts-query.ts b/packages/backend/src/server/api/common/generate-fts-query.ts index 6c67d31e7..ad31b734a 100644 --- a/packages/backend/src/server/api/common/generate-fts-query.ts +++ b/packages/backend/src/server/api/common/generate-fts-query.ts @@ -16,10 +16,12 @@ const filters = { "until": beforeFilter, "after": afterFilter, "since": afterFilter, - "domain": domainFilter, - "-domain": domainFilterInverse, - "host": domainFilter, - "-host": domainFilterInverse, + "instance": instanceFilter, + "-instance": instanceFilterInverse, + "domain": instanceFilter, + "-domain": instanceFilterInverse, + "host": instanceFilter, + "-host": instanceFilterInverse, "filter": miscFilter, "-filter": miscFilterInverse, "has": attachmentFilter, @@ -131,12 +133,14 @@ function afterFilter(query: SelectQueryBuilder, filter: string) { query.andWhere('note.createdAt > :after', { after: filter }); } -function domainFilter(query: SelectQueryBuilder, filter: string) { - query.andWhere('note.userHost = :domain', { domain: filter }); +function instanceFilter(query: SelectQueryBuilder, filter: string, id: number) { + query.andWhere(`note.userHost = :instance_${id}`); + query.setParameter(`instance_${id}`, filter); } -function domainFilterInverse(query: SelectQueryBuilder, filter: string) { - query.andWhere('note.userHost <> :domain', { domain: filter }); +function instanceFilterInverse(query: SelectQueryBuilder, filter: string, id: number) { + query.andWhere(`note.userHost <> :instance_${id}`); + query.setParameter(`instance_${id}`, filter); } function miscFilter(query: SelectQueryBuilder, filter: string) { @@ -180,9 +184,13 @@ function miscFilterInverse(query: SelectQueryBuilder, filter: string) { function attachmentFilter(query: SelectQueryBuilder, filter: string) { switch(filter) { case 'image': + query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%image/%'`); + break; case 'video': + query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%video/%'`); + break; case 'audio': - query.andWhere(`note."attachedFileTypes"::varchar ILIKE :type`, { type: `%${sqlLikeEscape(filter)}/%` }); + query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%audio/%'`); break; case 'file': query.andWhere(`note."attachedFileTypes" <> '{}'`);