From 3f86903caef495270119cfe7dd6a180a2d1b30e1 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Tue, 21 Nov 2023 22:23:01 +0100 Subject: [PATCH] upd: use Meilisearch for file type searching --- packages/backend/src/core/SearchService.ts | 43 ++++++++++++++++++- .../src/server/api/endpoints/notes/search.ts | 1 - 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/SearchService.ts b/packages/backend/src/core/SearchService.ts index 6b9a6d546..2a241d2e3 100644 --- a/packages/backend/src/core/SearchService.ts +++ b/packages/backend/src/core/SearchService.ts @@ -29,7 +29,8 @@ type Q = { op: 'is not null', k: K} | { op: 'and', qs: Q[] } | { op: 'or', qs: Q[] } | - { op: 'not', q: Q }; + { op: 'not', q: Q } | + { op: 'in', qs: Q[] }; function compileValue(value: V): string { if (typeof value === 'string') { @@ -93,6 +94,7 @@ export class SearchService { 'userHost', 'channelId', 'tags', + 'attachedFileTypes', ], typoTolerance: { enabled: false, @@ -138,6 +140,7 @@ export class SearchService { cw: note.cw, text: note.text, tags: note.tags, + attachedFileTypes: note.attachedFileTypes, }], { primaryKey: 'id', }); @@ -182,6 +185,44 @@ export class SearchService { filter.qs.push({ op: '=', k: 'userHost', v: opts.host }); } } + if (opts.filetype) { + if (opts.filetype === 'image') { + filter.qs.push({ op: 'or', qs: [ + { op: '=', k: 'attachedFileTypes', v: 'image/webp' }, + { op: '=', k: 'attachedFileTypes', v: 'image/png' }, + { op: '=', k: 'attachedFileTypes', v: 'image/jpeg' }, + { op: '=', k: 'attachedFileTypes', v: 'image/avif' }, + { op: '=', k: 'attachedFileTypes', v: 'image/apng' }, + { op: '=', k: 'attachedFileTypes', v: 'image/gif' }, + ] }); + } else if (opts.filetype === 'video') { + filter.qs.push({ op: 'or', qs: [ + { op: '=', k: 'attachedFileTypes', v: 'video/mp4' }, + { op: '=', k: 'attachedFileTypes', v: 'video/webm' }, + { op: '=', k: 'attachedFileTypes', v: 'video/mpeg' }, + { op: '=', k: 'attachedFileTypes', v: 'video/x-m4v' }, + ] }); + } else if (opts.filetype === 'audio') { + filter.qs.push({ op: 'or', qs: [ + { op: '=', k: 'attachedFileTypes', v: 'audio/mpeg' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/flac' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/wav' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/aac' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/webm' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/opus' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/ogg' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/x-m4a' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/mod' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/s3m' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/xm' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/it' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/x-mod' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/x-s3m' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/x-xm' }, + { op: '=', k: 'attachedFileTypes', v: 'audio/x-it' }, + ] }); + } + } const res = await this.meilisearchNoteIndex!.search(q, { sort: [`createdAt:${opts.order ? opts.order : 'desc'}`], matchingStrategy: 'all', diff --git a/packages/backend/src/server/api/endpoints/notes/search.ts b/packages/backend/src/server/api/endpoints/notes/search.ts index f75249bfb..06efa3d95 100644 --- a/packages/backend/src/server/api/endpoints/notes/search.ts +++ b/packages/backend/src/server/api/endpoints/notes/search.ts @@ -75,7 +75,6 @@ export default class extends Endpoint { // eslint- host: ps.host, filetype: ps.filetype, order: ps.order, - disableMeili: ps.filetype ? true : false, }, { untilId: ps.untilId, sinceId: ps.sinceId,