2018-07-04 04:21:30 +00:00
|
|
|
import $ from 'cafy';
|
|
|
|
import es from '../../../../db/elasticsearch';
|
2018-11-02 04:47:44 +00:00
|
|
|
import define from '../../define';
|
2019-02-22 02:46:58 +00:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Notes } from '../../../../models';
|
|
|
|
import { In } from 'typeorm';
|
2019-04-23 13:35:26 +00:00
|
|
|
import { types, bool } from '../../../../misc/schema';
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿を検索します。',
|
|
|
|
'en-US': 'Search notes.'
|
|
|
|
},
|
|
|
|
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
query: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
limit: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 03:49:08 +00:00
|
|
|
default: 10
|
|
|
|
},
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
offset: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-02 03:49:08 +00:00
|
|
|
default: 0
|
|
|
|
}
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-23 02:20:58 +00:00
|
|
|
res: {
|
2019-04-23 13:35:26 +00:00
|
|
|
type: types.array,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
2019-02-23 02:20:58 +00:00
|
|
|
items: {
|
2019-04-23 13:35:26 +00:00
|
|
|
type: types.object,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
ref: 'Note',
|
|
|
|
}
|
2019-02-23 02:20:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
searchingNotAvailable: {
|
|
|
|
message: 'Searching not available.',
|
|
|
|
code: 'SEARCHING_NOT_AVAILABLE',
|
|
|
|
id: '7ee9c119-16a1-479f-a6fd-6fab00ed946f'
|
|
|
|
}
|
2018-11-02 03:49:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, me) => {
|
|
|
|
if (es == null) throw new ApiError(meta.errors.searchingNotAvailable);
|
2018-07-18 23:24:03 +00:00
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
const response = await es.search({
|
2018-07-04 04:21:30 +00:00
|
|
|
index: 'misskey',
|
|
|
|
type: 'note',
|
|
|
|
body: {
|
2019-04-12 16:43:22 +00:00
|
|
|
size: ps.limit!,
|
2018-11-02 03:49:08 +00:00
|
|
|
from: ps.offset,
|
2018-07-04 04:21:30 +00:00
|
|
|
query: {
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
2018-11-02 03:49:08 +00:00
|
|
|
query: ps.query,
|
2018-07-04 04:21:30 +00:00
|
|
|
default_operator: 'and'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sort: [
|
|
|
|
{ _doc: 'desc' }
|
|
|
|
]
|
|
|
|
}
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
if (response.hits.total === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const hits = response.hits.hits.map((hit: any) => hit.id);
|
|
|
|
|
|
|
|
if (hits.length === 0) return [];
|
2018-07-04 04:21:30 +00:00
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
// Fetch found notes
|
2019-04-07 12:50:36 +00:00
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
id: In(hits)
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: -1
|
2019-02-22 02:46:58 +00:00
|
|
|
}
|
2018-07-04 04:21:30 +00:00
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Notes.packMany(notes, me);
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|