[backend] Improve postgres FTS filters

This commit is contained in:
Laura Hausmann 2023-11-18 21:55:28 +01:00
parent 5d1bb36be3
commit e1ae37dbdb
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -16,10 +16,12 @@ const filters = {
"until": beforeFilter, "until": beforeFilter,
"after": afterFilter, "after": afterFilter,
"since": afterFilter, "since": afterFilter,
"domain": domainFilter, "instance": instanceFilter,
"-domain": domainFilterInverse, "-instance": instanceFilterInverse,
"host": domainFilter, "domain": instanceFilter,
"-host": domainFilterInverse, "-domain": instanceFilterInverse,
"host": instanceFilter,
"-host": instanceFilterInverse,
"filter": miscFilter, "filter": miscFilter,
"-filter": miscFilterInverse, "-filter": miscFilterInverse,
"has": attachmentFilter, "has": attachmentFilter,
@ -131,12 +133,14 @@ function afterFilter(query: SelectQueryBuilder<any>, filter: string) {
query.andWhere('note.createdAt > :after', { after: filter }); query.andWhere('note.createdAt > :after', { after: filter });
} }
function domainFilter(query: SelectQueryBuilder<any>, filter: string) { function instanceFilter(query: SelectQueryBuilder<any>, filter: string, id: number) {
query.andWhere('note.userHost = :domain', { domain: filter }); query.andWhere(`note.userHost = :instance_${id}`);
query.setParameter(`instance_${id}`, filter);
} }
function domainFilterInverse(query: SelectQueryBuilder<any>, filter: string) { function instanceFilterInverse(query: SelectQueryBuilder<any>, filter: string, id: number) {
query.andWhere('note.userHost <> :domain', { domain: filter }); query.andWhere(`note.userHost <> :instance_${id}`);
query.setParameter(`instance_${id}`, filter);
} }
function miscFilter(query: SelectQueryBuilder<any>, filter: string) { function miscFilter(query: SelectQueryBuilder<any>, filter: string) {
@ -180,9 +184,13 @@ function miscFilterInverse(query: SelectQueryBuilder<any>, filter: string) {
function attachmentFilter(query: SelectQueryBuilder<any>, filter: string) { function attachmentFilter(query: SelectQueryBuilder<any>, filter: string) {
switch(filter) { switch(filter) {
case 'image': case 'image':
query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%image/%'`);
break;
case 'video': case 'video':
query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%video/%'`);
break;
case 'audio': case 'audio':
query.andWhere(`note."attachedFileTypes"::varchar ILIKE :type`, { type: `%${sqlLikeEscape(filter)}/%` }); query.andWhere(`note."attachedFileTypes"::varchar ILIKE '%audio/%'`);
break; break;
case 'file': case 'file':
query.andWhere(`note."attachedFileTypes" <> '{}'`); query.andWhere(`note."attachedFileTypes" <> '{}'`);