mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-14 11:43:08 +02:00
f6154dc0af
Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com> Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
138 lines
2.7 KiB
TypeScript
138 lines
2.7 KiB
TypeScript
import $ from 'cafy';
|
|
import es from '../../../../db/elasticsearch';
|
|
import define from '../../define';
|
|
import { Notes } from '../../../../models';
|
|
import { In } from 'typeorm';
|
|
import { ID } from '../../../../misc/cafy-id';
|
|
import config from '../../../../config';
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
import { generateVisibilityQuery } from '../../common/generate-visibility-query';
|
|
import { generateMuteQuery } from '../../common/generate-mute-query';
|
|
|
|
export const meta = {
|
|
desc: {
|
|
'ja-JP': '投稿を検索します。',
|
|
'en-US': 'Search notes.'
|
|
},
|
|
|
|
tags: ['notes'],
|
|
|
|
requireCredential: false,
|
|
|
|
params: {
|
|
query: {
|
|
validator: $.str
|
|
},
|
|
|
|
sinceId: {
|
|
validator: $.optional.type(ID),
|
|
},
|
|
|
|
untilId: {
|
|
validator: $.optional.type(ID),
|
|
},
|
|
|
|
limit: {
|
|
validator: $.optional.num.range(1, 100),
|
|
default: 10
|
|
},
|
|
|
|
host: {
|
|
validator: $.optional.nullable.str,
|
|
default: undefined
|
|
},
|
|
|
|
userId: {
|
|
validator: $.optional.nullable.type(ID),
|
|
default: null
|
|
},
|
|
},
|
|
|
|
res: {
|
|
type: 'array' as const,
|
|
optional: false as const, nullable: false as const,
|
|
items: {
|
|
type: 'object' as const,
|
|
optional: false as const, nullable: false as const,
|
|
ref: 'Note',
|
|
}
|
|
},
|
|
|
|
errors: {
|
|
}
|
|
};
|
|
|
|
export default define(meta, async (ps, me) => {
|
|
if (es == null) {
|
|
const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId)
|
|
.andWhere('note.text ILIKE :q', { q: `%${ps.query}%` })
|
|
.leftJoinAndSelect('note.user', 'user');
|
|
|
|
generateVisibilityQuery(query, me);
|
|
if (me) generateMuteQuery(query, me);
|
|
|
|
const notes = await query.take(ps.limit!).getMany();
|
|
|
|
return await Notes.packMany(notes, me);
|
|
} else {
|
|
const userQuery = ps.userId != null ? [{
|
|
term: {
|
|
userId: ps.userId
|
|
}
|
|
}] : [];
|
|
|
|
const hostQuery = ps.userId == null ?
|
|
ps.host === null ? [{
|
|
bool: {
|
|
must_not: {
|
|
exists: {
|
|
field: 'userHost'
|
|
}
|
|
}
|
|
}
|
|
}] : ps.host !== undefined ? [{
|
|
term: {
|
|
userHost: ps.host
|
|
}
|
|
}] : []
|
|
: [];
|
|
|
|
const result = await es.search({
|
|
index: config.elasticsearch.index || 'misskey_note',
|
|
body: {
|
|
size: ps.limit!,
|
|
from: ps.offset,
|
|
query: {
|
|
bool: {
|
|
must: [{
|
|
simple_query_string: {
|
|
fields: ['text'],
|
|
query: ps.query.toLowerCase(),
|
|
default_operator: 'and'
|
|
},
|
|
}, ...hostQuery, ...userQuery]
|
|
}
|
|
},
|
|
sort: [{
|
|
_doc: 'desc'
|
|
}]
|
|
}
|
|
});
|
|
|
|
const hits = result.body.hits.hits.map((hit: any) => hit._id);
|
|
|
|
if (hits.length === 0) return [];
|
|
|
|
// Fetch found notes
|
|
const notes = await Notes.find({
|
|
where: {
|
|
id: In(hits)
|
|
},
|
|
order: {
|
|
id: -1
|
|
}
|
|
});
|
|
|
|
return await Notes.packMany(notes, me);
|
|
}
|
|
});
|