2018-07-03 22:21:30 -06:00
|
|
|
import $ from 'cafy';
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Note from '../../../../models/note';
|
|
|
|
import { ILocalUser } from '../../../../models/user';
|
2018-10-03 09:39:11 -06:00
|
|
|
import { packMany } from '../../../../models/note';
|
2018-07-03 22:21:30 -06:00
|
|
|
import es from '../../../../db/elasticsearch';
|
|
|
|
|
2018-07-05 11:58:29 -06:00
|
|
|
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
|
2018-07-03 22:21:30 -06:00
|
|
|
// Get 'query' parameter
|
|
|
|
const [query, queryError] = $.str.get(params.query);
|
|
|
|
if (queryError) return rej('invalid query param');
|
|
|
|
|
|
|
|
// Get 'offset' parameter
|
2018-07-05 08:36:07 -06:00
|
|
|
const [offset = 0, offsetErr] = $.num.optional.min(0).get(params.offset);
|
2018-07-03 22:21:30 -06:00
|
|
|
if (offsetErr) return rej('invalid offset param');
|
|
|
|
|
|
|
|
// Get 'limit' parameter
|
2018-07-05 08:36:07 -06:00
|
|
|
const [limit = 10, limitErr] = $.num.optional.range(1, 30).get(params.limit);
|
2018-07-03 22:21:30 -06:00
|
|
|
if (limitErr) return rej('invalid limit param');
|
|
|
|
|
2018-07-18 17:24:03 -06:00
|
|
|
if (es == null) return rej('searching not available');
|
|
|
|
|
2018-07-03 22:21:30 -06:00
|
|
|
es.search({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'note',
|
|
|
|
body: {
|
|
|
|
size: limit,
|
|
|
|
from: offset,
|
|
|
|
query: {
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
|
|
|
query: query,
|
|
|
|
default_operator: 'and'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sort: [
|
|
|
|
{ _doc: 'desc' }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}, async (error, response) => {
|
|
|
|
if (error) {
|
|
|
|
console.error(error);
|
|
|
|
return res(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.hits.total === 0) {
|
|
|
|
return res([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id));
|
|
|
|
|
|
|
|
// Fetch found notes
|
|
|
|
const notes = await Note.find({
|
|
|
|
_id: {
|
|
|
|
$in: hits
|
|
|
|
}
|
|
|
|
}, {
|
2018-07-18 17:24:03 -06:00
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2018-10-03 09:39:11 -06:00
|
|
|
res(await packMany(notes, me));
|
2018-07-03 22:21:30 -06:00
|
|
|
});
|
|
|
|
});
|