upd: use Meilisearch for file type searching

This commit is contained in:
Mar0xy 2023-11-21 22:23:01 +01:00
parent 733c9a8c00
commit 3f86903cae
No known key found for this signature in database
GPG Key ID: 56569BBE47D2C828
2 changed files with 42 additions and 2 deletions

View File

@ -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',

View File

@ -75,7 +75,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
host: ps.host,
filetype: ps.filetype,
order: ps.order,
disableMeili: ps.filetype ? true : false,
}, {
untilId: ps.untilId,
sinceId: ps.sinceId,