yumechi-no-kuni/src/api/endpoints/posts/search.ts

271 lines
5.4 KiB
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
2017-03-08 12:50:09 -06:00
import $ from 'cafy';
2017-01-20 03:08:08 -06:00
const escapeRegexp = require('escape-regexp');
2016-12-28 16:49:51 -06:00
import Post from '../../models/post';
2017-12-20 13:01:44 -06:00
import User from '../../models/user';
2017-12-20 15:31:56 -06:00
import getFriends from '../../common/get-friends';
2016-12-28 16:49:51 -06:00
import serialize from '../../serializers/post';
2017-01-20 03:08:08 -06:00
import config from '../../../conf';
2016-12-28 16:49:51 -06:00
/**
* Search a post
*
2017-03-01 02:37:01 -06:00
* @param {any} params
* @param {any} me
* @return {Promise<any>}
2016-12-28 16:49:51 -06:00
*/
2017-03-03 13:28:38 -06:00
module.exports = (params, me) => new Promise(async (res, rej) => {
2017-12-20 13:01:44 -06:00
// Get 'text' parameter
const [text, textError] = $(params.text).optional.string().$;
if (textError) return rej('invalid text param');
// Get 'user_id' parameter
const [userId, userIdErr] = $(params.user_id).optional.id().$;
if (userIdErr) return rej('invalid user_id param');
// Get 'username' parameter
const [username, usernameErr] = $(params.username).optional.string().$;
if (usernameErr) return rej('invalid username param');
2017-12-20 15:31:56 -06:00
// Get 'following' parameter
const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$;
if (followingErr) return rej('invalid following param');
2017-12-20 16:35:16 -06:00
// Get 'reply' parameter
const [reply = null, replyErr] = $(params.reply).optional.nullable.boolean().$;
if (replyErr) return rej('invalid reply param');
2017-12-20 13:01:44 -06:00
2017-12-20 16:35:16 -06:00
// Get 'repost' parameter
const [repost = null, repostErr] = $(params.repost).optional.nullable.boolean().$;
if (repostErr) return rej('invalid repost param');
// Get 'media' parameter
const [media = null, mediaErr] = $(params.media).optional.nullable.boolean().$;
if (mediaErr) return rej('invalid media param');
2017-12-20 13:01:44 -06:00
// Get 'since_date' parameter
const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$;
if (sinceDateErr) throw 'invalid since_date param';
// Get 'until_date' parameter
const [untilDate, untilDateErr] = $(params.until_date).optional.number().$;
if (untilDateErr) throw 'invalid until_date param';
2016-12-28 16:49:51 -06:00
// Get 'offset' parameter
2017-03-08 12:50:09 -06:00
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
2017-03-02 15:48:26 -06:00
if (offsetErr) return rej('invalid offset param');
2016-12-28 16:49:51 -06:00
2017-12-20 13:01:44 -06:00
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 30).$;
if (limitErr) return rej('invalid limit param');
let user = userId;
if (user == null && username != null) {
const _user = await User.findOne({
username_lower: username.toLowerCase()
});
if (_user) {
user = _user._id;
}
}
2016-12-28 16:49:51 -06:00
2017-12-20 13:01:44 -06:00
// If Elasticsearch is available, search by it
2016-12-28 16:49:51 -06:00
// If not, search by MongoDB
(config.elasticsearch.enable ? byElasticsearch : byNative)
2017-12-20 16:35:16 -06:00
(res, rej, me, text, user, following, reply, repost, media, sinceDate, untilDate, offset, limit);
2016-12-28 16:49:51 -06:00
});
// Search by MongoDB
2017-12-20 16:35:16 -06:00
async function byNative(res, rej, me, text, userId, following, reply, repost, media, sinceDate, untilDate, offset, max) {
const q: any = {
$and: []
};
const push = q.$and.push;
2017-12-20 13:01:44 -06:00
if (text) {
2017-12-20 16:35:16 -06:00
push({
$and: text.split(' ').map(x => ({
text: new RegExp(escapeRegexp(x))
}))
});
2017-12-20 13:01:44 -06:00
}
if (userId) {
2017-12-20 16:35:16 -06:00
push({
user_id: userId
});
2017-12-20 13:01:44 -06:00
}
2017-12-20 15:31:56 -06:00
if (following != null) {
const ids = await getFriends(me._id, false);
2017-12-20 16:35:16 -06:00
push({
user_id: following ? {
$in: ids
} : {
$nin: ids
}
});
}
if (reply != null) {
if (reply) {
push({
reply_id: {
$exists: true,
$ne: null
}
});
2017-12-20 15:31:56 -06:00
} else {
2017-12-20 16:35:16 -06:00
push({
$or: [{
reply_id: {
$exists: false
}
}, {
reply_id: null
}]
});
2017-12-20 15:31:56 -06:00
}
}
2017-12-20 16:35:16 -06:00
if (repost != null) {
if (repost) {
push({
repost_id: {
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
repost_id: {
$exists: false
}
}, {
repost_id: null
}]
});
}
2017-12-20 13:01:44 -06:00
}
2017-12-20 16:35:16 -06:00
if (media != null) {
if (media) {
push({
media_ids: {
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
media_ids: {
$exists: false
}
}, {
media_ids: null
}]
});
}
2017-12-20 13:01:44 -06:00
}
if (sinceDate) {
2017-12-20 16:35:16 -06:00
push({
created_at: {
$gt: new Date(sinceDate)
}
});
2017-12-20 13:01:44 -06:00
}
if (untilDate) {
2017-12-20 16:35:16 -06:00
push({
created_at: {
$lt: new Date(untilDate)
}
});
2017-12-20 13:01:44 -06:00
}
2016-12-28 16:49:51 -06:00
// Search posts
const posts = await Post
2017-12-20 13:01:44 -06:00
.find(q, {
2016-12-28 16:49:51 -06:00
sort: {
_id: -1
},
limit: max,
skip: offset
2017-01-16 20:11:22 -06:00
});
2016-12-28 16:49:51 -06:00
// Serialize
res(await Promise.all(posts.map(async post =>
await serialize(post, me))));
}
// Search by Elasticsearch
2017-12-20 16:35:16 -06:00
async function byElasticsearch(res, rej, me, text, userId, following, reply, repost, media, sinceDate, untilDate, offset, max) {
2016-12-28 16:49:51 -06:00
const es = require('../../db/elasticsearch');
es.search({
index: 'misskey',
type: 'post',
body: {
size: max,
from: offset,
query: {
simple_query_string: {
fields: ['text'],
2017-12-20 13:01:44 -06:00
query: text,
2016-12-28 16:49:51 -06:00
default_operator: 'and'
}
},
sort: [
{ _doc: 'desc' }
],
highlight: {
pre_tags: ['<mark>'],
post_tags: ['</mark>'],
encoder: 'html',
fields: {
text: {}
}
}
}
}, 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));
2017-02-27 01:54:20 -06:00
// Fetch found posts
2016-12-28 16:49:51 -06:00
const posts = await Post
.find({
_id: {
$in: hits
}
2017-01-16 20:11:22 -06:00
}, {
2016-12-28 16:49:51 -06:00
sort: {
_id: -1
}
2017-01-16 20:11:22 -06:00
});
2016-12-28 16:49:51 -06:00
posts.map(post => {
post._highlight = response.hits.hits.filter(hit => post._id.equals(hit._id))[0].highlight.text[0];
});
// Serialize
res(await Promise.all(posts.map(async post =>
await serialize(post, me))));
});
}