rudeshark.net/src/server/api/endpoints/posts/search.ts

365 lines
7.7 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
2017-03-08 19:50:09 +01:00
import $ from 'cafy';
2017-01-20 10:08:08 +01:00
const escapeRegexp = require('escape-regexp');
2016-12-28 23:49:51 +01:00
import Post from '../../models/post';
2017-12-20 20:01:44 +01:00
import User from '../../models/user';
2017-12-21 22:38:48 +01:00
import Mute from '../../models/mute';
2017-12-20 22:31:56 +01:00
import getFriends from '../../common/get-friends';
2018-02-02 00:21:30 +01:00
import { pack } from '../../models/post';
2016-12-28 23:49:51 +01:00
/**
* Search a post
*
2017-03-01 09:37:01 +01:00
* @param {any} params
* @param {any} me
* @return {Promise<any>}
2016-12-28 23:49:51 +01:00
*/
2017-03-03 20:28:38 +01:00
module.exports = (params, me) => new Promise(async (res, rej) => {
2017-12-20 20:01:44 +01:00
// Get 'text' parameter
const [text, textError] = $(params.text).optional.string().$;
if (textError) return rej('invalid text param');
2017-12-22 23:21:52 +01:00
// Get 'include_user_ids' parameter
const [includeUserIds = [], includeUserIdsErr] = $(params.include_user_ids).optional.array('id').$;
if (includeUserIdsErr) return rej('invalid include_user_ids param');
2017-12-20 20:01:44 +01:00
2017-12-22 23:21:52 +01:00
// Get 'exclude_user_ids' parameter
const [excludeUserIds = [], excludeUserIdsErr] = $(params.exclude_user_ids).optional.array('id').$;
if (excludeUserIdsErr) return rej('invalid exclude_user_ids param');
// Get 'include_user_usernames' parameter
const [includeUserUsernames = [], includeUserUsernamesErr] = $(params.include_user_usernames).optional.array('string').$;
if (includeUserUsernamesErr) return rej('invalid include_user_usernames param');
// Get 'exclude_user_usernames' parameter
const [excludeUserUsernames = [], excludeUserUsernamesErr] = $(params.exclude_user_usernames).optional.array('string').$;
if (excludeUserUsernamesErr) return rej('invalid exclude_user_usernames param');
2017-12-20 20:01:44 +01:00
2017-12-20 22:31:56 +01:00
// Get 'following' parameter
const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$;
if (followingErr) return rej('invalid following param');
2017-12-21 22:38:48 +01:00
// Get 'mute' parameter
const [mute = 'mute_all', muteErr] = $(params.mute).optional.string().$;
if (muteErr) return rej('invalid mute param');
2017-12-20 23:35:16 +01:00
// Get 'reply' parameter
const [reply = null, replyErr] = $(params.reply).optional.nullable.boolean().$;
if (replyErr) return rej('invalid reply param');
2017-12-20 20:01:44 +01:00
2017-12-20 23:35:16 +01: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 20:01:44 +01:00
2017-12-20 23:57:31 +01:00
// Get 'poll' parameter
const [poll = null, pollErr] = $(params.poll).optional.nullable.boolean().$;
if (pollErr) return rej('invalid poll param');
2017-12-20 20:01:44 +01: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 23:49:51 +01:00
// Get 'offset' parameter
2017-03-08 19:50:09 +01:00
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
2017-03-02 22:48:26 +01:00
if (offsetErr) return rej('invalid offset param');
2016-12-28 23:49:51 +01:00
2017-12-20 20:01:44 +01:00
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 30).$;
if (limitErr) return rej('invalid limit param');
2017-12-22 23:21:52 +01:00
let includeUsers = includeUserIds;
if (includeUserUsernames != null) {
const ids = (await Promise.all(includeUserUsernames.map(async (username) => {
const _user = await User.findOne({
username_lower: username.toLowerCase()
});
return _user ? _user._id : null;
}))).filter(id => id != null);
includeUsers = includeUsers.concat(ids);
}
2017-12-20 20:01:44 +01:00
2017-12-22 23:21:52 +01:00
let excludeUsers = excludeUserIds;
if (excludeUserUsernames != null) {
const ids = (await Promise.all(excludeUserUsernames.map(async (username) => {
const _user = await User.findOne({
username_lower: username.toLowerCase()
});
return _user ? _user._id : null;
}))).filter(id => id != null);
excludeUsers = excludeUsers.concat(ids);
2017-12-20 20:01:44 +01:00
}
2016-12-28 23:49:51 +01:00
2017-12-22 23:21:52 +01:00
search(res, rej, me, text, includeUsers, excludeUsers, following,
mute, reply, repost, media, poll, sinceDate, untilDate, offset, limit);
2016-12-28 23:49:51 +01:00
});
2017-12-22 23:21:52 +01:00
async function search(
res, rej, me, text, includeUserIds, excludeUserIds, following,
mute, reply, repost, media, poll, sinceDate, untilDate, offset, max) {
2017-12-21 07:37:26 +01:00
let q: any = {
2017-12-20 23:35:16 +01:00
$and: []
};
2017-12-20 23:45:45 +01:00
const push = x => q.$and.push(x);
2017-12-20 20:01:44 +01:00
if (text) {
2017-12-22 20:38:56 +01:00
// 完全一致検索
if (/"""(.+?)"""/.test(text)) {
const x = text.match(/"""(.+?)"""/)[1];
push({
text: x
});
} else {
2018-02-25 17:06:21 +01:00
const tags = text.split(' ').filter(x => x[0] == '#');
if (tags) {
push({
$and: tags.map(x => ({
tags: x
}))
});
}
2017-12-22 20:38:56 +01:00
push({
$and: text.split(' ').map(x => ({
// キーワードが-で始まる場合そのキーワードを除外する
text: x[0] == '-' ? {
$not: new RegExp(escapeRegexp(x.substr(1)))
} : new RegExp(escapeRegexp(x))
}))
});
}
2017-12-20 20:01:44 +01:00
}
2017-12-22 23:21:52 +01:00
if (includeUserIds && includeUserIds.length != 0) {
2017-12-20 23:35:16 +01:00
push({
2017-12-22 23:21:52 +01:00
user_id: {
$in: includeUserIds
}
});
} else if (excludeUserIds && excludeUserIds.length != 0) {
push({
user_id: {
$nin: excludeUserIds
}
2017-12-20 23:35:16 +01:00
});
2017-12-20 20:01:44 +01:00
}
2017-12-21 07:28:50 +01:00
if (following != null && me != null) {
2017-12-20 22:31:56 +01:00
const ids = await getFriends(me._id, false);
2017-12-20 23:35:16 +01:00
push({
user_id: following ? {
$in: ids
} : {
2017-12-21 07:30:15 +01:00
$nin: ids.concat(me._id)
2017-12-20 23:35:16 +01:00
}
});
}
2017-12-21 22:38:48 +01:00
if (me != null) {
const mutes = await Mute.find({
muter_id: me._id,
deleted_at: { $exists: false }
});
const mutedUserIds = mutes.map(m => m.mutee_id);
switch (mute) {
case 'mute_all':
push({
user_id: {
$nin: mutedUserIds
},
'_reply.user_id': {
$nin: mutedUserIds
},
'_repost.user_id': {
$nin: mutedUserIds
}
});
break;
case 'mute_related':
push({
'_reply.user_id': {
$nin: mutedUserIds
},
'_repost.user_id': {
$nin: mutedUserIds
}
});
break;
case 'mute_direct':
push({
user_id: {
$nin: mutedUserIds
}
});
break;
case 'direct_only':
push({
user_id: {
$in: mutedUserIds
}
});
break;
case 'related_only':
push({
$or: [{
'_reply.user_id': {
$in: mutedUserIds
}
}, {
'_repost.user_id': {
$in: mutedUserIds
}
}]
});
break;
case 'all_only':
push({
$or: [{
user_id: {
$in: mutedUserIds
}
}, {
'_reply.user_id': {
$in: mutedUserIds
}
}, {
'_repost.user_id': {
$in: mutedUserIds
}
}]
});
break;
}
}
2017-12-20 23:35:16 +01:00
if (reply != null) {
if (reply) {
push({
reply_id: {
$exists: true,
$ne: null
}
});
2017-12-20 22:31:56 +01:00
} else {
2017-12-20 23:35:16 +01:00
push({
$or: [{
reply_id: {
$exists: false
}
}, {
reply_id: null
}]
});
2017-12-20 22:31:56 +01:00
}
}
2017-12-20 23:35:16 +01: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 20:01:44 +01:00
}
2017-12-20 23:35:16 +01: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 20:01:44 +01:00
}
2017-12-20 23:57:31 +01:00
if (poll != null) {
if (poll) {
push({
poll: {
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
poll: {
$exists: false
}
}, {
poll: null
}]
});
}
}
2017-12-20 20:01:44 +01:00
if (sinceDate) {
2017-12-20 23:35:16 +01:00
push({
created_at: {
$gt: new Date(sinceDate)
}
});
2017-12-20 20:01:44 +01:00
}
if (untilDate) {
2017-12-20 23:35:16 +01:00
push({
created_at: {
$lt: new Date(untilDate)
}
});
2017-12-20 20:01:44 +01:00
}
2016-12-28 23:49:51 +01:00
2017-12-21 07:37:26 +01:00
if (q.$and.length == 0) {
q = {};
}
2016-12-28 23:49:51 +01:00
// Search posts
const posts = await Post
2017-12-20 20:01:44 +01:00
.find(q, {
2016-12-28 23:49:51 +01:00
sort: {
_id: -1
},
limit: max,
skip: offset
2017-01-17 03:11:22 +01:00
});
2016-12-28 23:49:51 +01:00
// Serialize
res(await Promise.all(posts.map(async post =>
2018-02-02 00:21:30 +01:00
await pack(post, me))));
2016-12-28 23:49:51 +01:00
}