2018-07-03 23:21:30 -05:00
|
|
|
import $ from 'cafy';
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Note from '../../../../models/note';
|
|
|
|
import { ILocalUser } from '../../../../models/user';
|
2018-10-03 10:39:11 -05:00
|
|
|
import { packMany } from '../../../../models/note';
|
2018-07-03 23:21:30 -05:00
|
|
|
import es from '../../../../db/elasticsearch';
|
2018-11-01 22:49:08 -05:00
|
|
|
import getParams from '../../get-params';
|
2018-07-03 23:21:30 -05:00
|
|
|
|
2018-11-01 22:49:08 -05:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿を検索します。',
|
|
|
|
'en-US': 'Search notes.'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
query: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2018-07-03 23:21:30 -05:00
|
|
|
|
2018-11-01 22:49:08 -05:00
|
|
|
limit: {
|
|
|
|
validator: $.num.optional.range(1, 100),
|
|
|
|
default: 10
|
|
|
|
},
|
2018-07-03 23:21:30 -05:00
|
|
|
|
2018-11-01 22:49:08 -05:00
|
|
|
offset: {
|
|
|
|
validator: $.num.optional.min(0),
|
|
|
|
default: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
|
|
|
|
const [ps, psErr] = getParams(meta, params);
|
|
|
|
if (psErr) return rej(psErr);
|
2018-07-03 23:21:30 -05:00
|
|
|
|
2018-07-18 18:24:03 -05:00
|
|
|
if (es == null) return rej('searching not available');
|
|
|
|
|
2018-07-03 23:21:30 -05:00
|
|
|
es.search({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'note',
|
|
|
|
body: {
|
2018-11-01 22:49:08 -05:00
|
|
|
size: ps.limit,
|
|
|
|
from: ps.offset,
|
2018-07-03 23:21:30 -05:00
|
|
|
query: {
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
2018-11-01 22:49:08 -05:00
|
|
|
query: ps.query,
|
2018-07-03 23:21:30 -05:00
|
|
|
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 18:24:03 -05:00
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
2018-07-03 23:21:30 -05:00
|
|
|
|
2018-10-03 10:39:11 -05:00
|
|
|
res(await packMany(notes, me));
|
2018-07-03 23:21:30 -05:00
|
|
|
});
|
|
|
|
});
|