yumechi-no-kuni/src/server/api/endpoints/notes/search.ts

365 lines
7.6 KiB
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
/**
* Module dependencies
*/
2017-03-08 12:50:09 -06:00
import $ from 'cafy';
2017-01-20 03:08:08 -06:00
const escapeRegexp = require('escape-regexp');
2018-04-07 12:30:37 -05:00
import Note from '../../../../models/note';
2018-03-29 06:32:18 -05:00
import User from '../../../../models/user';
import Mute from '../../../../models/mute';
2017-12-20 15:31:56 -06:00
import getFriends from '../../common/get-friends';
2018-04-07 12:30:37 -05:00
import { pack } from '../../../../models/note';
2016-12-28 16:49:51 -06:00
/**
2018-04-07 12:30:37 -05:00
* Search a note
2016-12-28 16:49:51 -06:00
*
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');
2018-03-29 00:48:47 -05:00
// Get 'includeUserIds' parameter
const [includeUserIds = [], includeUserIdsErr] = $(params.includeUserIds).optional.array('id').$;
if (includeUserIdsErr) return rej('invalid includeUserIds param');
2017-12-20 13:01:44 -06:00
2018-03-29 00:48:47 -05:00
// Get 'excludeUserIds' parameter
const [excludeUserIds = [], excludeUserIdsErr] = $(params.excludeUserIds).optional.array('id').$;
if (excludeUserIdsErr) return rej('invalid excludeUserIds param');
2017-12-22 16:21:52 -06:00
2018-03-29 00:48:47 -05:00
// Get 'includeUserUsernames' parameter
const [includeUserUsernames = [], includeUserUsernamesErr] = $(params.includeUserUsernames).optional.array('string').$;
if (includeUserUsernamesErr) return rej('invalid includeUserUsernames param');
2017-12-22 16:21:52 -06:00
2018-03-29 00:48:47 -05:00
// Get 'excludeUserUsernames' parameter
const [excludeUserUsernames = [], excludeUserUsernamesErr] = $(params.excludeUserUsernames).optional.array('string').$;
if (excludeUserUsernamesErr) return rej('invalid excludeUserUsernames param');
2017-12-20 13:01:44 -06:00
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-21 15:38:48 -06:00
// Get 'mute' parameter
const [mute = 'mute_all', muteErr] = $(params.mute).optional.string().$;
if (muteErr) return rej('invalid mute 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
2018-04-07 12:30:37 -05:00
// Get 'renote' parameter
const [renote = null, renoteErr] = $(params.renote).optional.nullable.boolean().$;
if (renoteErr) return rej('invalid renote param');
2017-12-20 16:35:16 -06:00
// 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
2017-12-20 16:57:31 -06:00
// Get 'poll' parameter
const [poll = null, pollErr] = $(params.poll).optional.nullable.boolean().$;
if (pollErr) return rej('invalid poll param');
2018-03-29 00:48:47 -05:00
// Get 'sinceDate' parameter
const [sinceDate, sinceDateErr] = $(params.sinceDate).optional.number().$;
if (sinceDateErr) throw 'invalid sinceDate param';
2017-12-20 13:01:44 -06:00
2018-03-29 00:48:47 -05:00
// Get 'untilDate' parameter
const [untilDate, untilDateErr] = $(params.untilDate).optional.number().$;
if (untilDateErr) throw 'invalid untilDate 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');
2017-12-22 16:21:52 -06:00
let includeUsers = includeUserIds;
if (includeUserUsernames != null) {
const ids = (await Promise.all(includeUserUsernames.map(async (username) => {
const _user = await User.findOne({
2018-03-29 00:48:47 -05:00
usernameLower: username.toLowerCase()
2017-12-22 16:21:52 -06:00
});
return _user ? _user._id : null;
}))).filter(id => id != null);
includeUsers = includeUsers.concat(ids);
}
2017-12-20 13:01:44 -06:00
2017-12-22 16:21:52 -06:00
let excludeUsers = excludeUserIds;
if (excludeUserUsernames != null) {
const ids = (await Promise.all(excludeUserUsernames.map(async (username) => {
const _user = await User.findOne({
2018-03-29 00:48:47 -05:00
usernameLower: username.toLowerCase()
2017-12-22 16:21:52 -06:00
});
return _user ? _user._id : null;
}))).filter(id => id != null);
excludeUsers = excludeUsers.concat(ids);
2017-12-20 13:01:44 -06:00
}
2016-12-28 16:49:51 -06:00
2017-12-22 16:21:52 -06:00
search(res, rej, me, text, includeUsers, excludeUsers, following,
2018-04-07 12:30:37 -05:00
mute, reply, renote, media, poll, sinceDate, untilDate, offset, limit);
2016-12-28 16:49:51 -06:00
});
2017-12-22 16:21:52 -06:00
async function search(
res, rej, me, text, includeUserIds, excludeUserIds, following,
2018-04-07 12:30:37 -05:00
mute, reply, renote, media, poll, sinceDate, untilDate, offset, max) {
2017-12-22 16:21:52 -06:00
2017-12-21 00:37:26 -06:00
let q: any = {
2017-12-20 16:35:16 -06:00
$and: []
};
2017-12-20 16:45:45 -06:00
const push = x => q.$and.push(x);
2017-12-20 13:01:44 -06:00
if (text) {
2017-12-22 13:38:56 -06:00
// 完全一致検索
if (/"""(.+?)"""/.test(text)) {
const x = text.match(/"""(.+?)"""/)[1];
push({
text: x
});
} else {
2018-02-25 10:06:21 -06:00
const tags = text.split(' ').filter(x => x[0] == '#');
if (tags) {
push({
$and: tags.map(x => ({
tags: x
}))
});
}
2017-12-22 13:38:56 -06:00
push({
$and: text.split(' ').map(x => ({
// キーワードが-で始まる場合そのキーワードを除外する
text: x[0] == '-' ? {
$not: new RegExp(escapeRegexp(x.substr(1)))
} : new RegExp(escapeRegexp(x))
}))
});
}
2017-12-20 13:01:44 -06:00
}
2017-12-22 16:21:52 -06:00
if (includeUserIds && includeUserIds.length != 0) {
2017-12-20 16:35:16 -06:00
push({
2018-03-29 00:48:47 -05:00
userId: {
2017-12-22 16:21:52 -06:00
$in: includeUserIds
}
});
} else if (excludeUserIds && excludeUserIds.length != 0) {
push({
2018-03-29 00:48:47 -05:00
userId: {
2017-12-22 16:21:52 -06:00
$nin: excludeUserIds
}
2017-12-20 16:35:16 -06:00
});
2017-12-20 13:01:44 -06:00
}
2017-12-21 00:28:50 -06:00
if (following != null && me != null) {
2017-12-20 15:31:56 -06:00
const ids = await getFriends(me._id, false);
2017-12-20 16:35:16 -06:00
push({
2018-03-29 00:48:47 -05:00
userId: following ? {
2017-12-20 16:35:16 -06:00
$in: ids
} : {
2017-12-21 00:30:15 -06:00
$nin: ids.concat(me._id)
2017-12-20 16:35:16 -06:00
}
});
}
2017-12-21 15:38:48 -06:00
if (me != null) {
const mutes = await Mute.find({
2018-03-29 00:48:47 -05:00
muterId: me._id,
deletedAt: { $exists: false }
2017-12-21 15:38:48 -06:00
});
2018-03-29 00:48:47 -05:00
const mutedUserIds = mutes.map(m => m.muteeId);
2017-12-21 15:38:48 -06:00
switch (mute) {
case 'mute_all':
push({
2018-03-29 00:48:47 -05:00
userId: {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
},
2018-03-29 00:48:47 -05:00
'_reply.userId': {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
},
2018-04-07 12:30:37 -05:00
'_renote.userId': {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
}
});
break;
case 'mute_related':
push({
2018-03-29 00:48:47 -05:00
'_reply.userId': {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
},
2018-04-07 12:30:37 -05:00
'_renote.userId': {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
}
});
break;
case 'mute_direct':
push({
2018-03-29 00:48:47 -05:00
userId: {
2017-12-21 15:38:48 -06:00
$nin: mutedUserIds
}
});
break;
case 'direct_only':
push({
2018-03-29 00:48:47 -05:00
userId: {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
});
break;
case 'related_only':
push({
$or: [{
2018-03-29 00:48:47 -05:00
'_reply.userId': {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
}, {
2018-04-07 12:30:37 -05:00
'_renote.userId': {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
}]
});
break;
case 'all_only':
push({
$or: [{
2018-03-29 00:48:47 -05:00
userId: {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
}, {
2018-03-29 00:48:47 -05:00
'_reply.userId': {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
}, {
2018-04-07 12:30:37 -05:00
'_renote.userId': {
2017-12-21 15:38:48 -06:00
$in: mutedUserIds
}
}]
});
break;
}
}
2017-12-20 16:35:16 -06:00
if (reply != null) {
if (reply) {
push({
2018-03-29 00:48:47 -05:00
replyId: {
2017-12-20 16:35:16 -06:00
$exists: true,
$ne: null
}
});
2017-12-20 15:31:56 -06:00
} else {
2017-12-20 16:35:16 -06:00
push({
$or: [{
2018-03-29 00:48:47 -05:00
replyId: {
2017-12-20 16:35:16 -06:00
$exists: false
}
}, {
2018-03-29 00:48:47 -05:00
replyId: null
2017-12-20 16:35:16 -06:00
}]
});
2017-12-20 15:31:56 -06:00
}
}
2018-04-07 12:30:37 -05:00
if (renote != null) {
if (renote) {
2017-12-20 16:35:16 -06:00
push({
2018-04-07 12:30:37 -05:00
renoteId: {
2017-12-20 16:35:16 -06:00
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
2018-04-07 12:30:37 -05:00
renoteId: {
2017-12-20 16:35:16 -06:00
$exists: false
}
}, {
2018-04-07 12:30:37 -05:00
renoteId: null
2017-12-20 16:35:16 -06:00
}]
});
}
2017-12-20 13:01:44 -06:00
}
2017-12-20 16:35:16 -06:00
if (media != null) {
if (media) {
push({
2018-03-29 00:48:47 -05:00
mediaIds: {
2017-12-20 16:35:16 -06:00
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
2018-03-29 00:48:47 -05:00
mediaIds: {
2017-12-20 16:35:16 -06:00
$exists: false
}
}, {
2018-03-29 00:48:47 -05:00
mediaIds: null
2017-12-20 16:35:16 -06:00
}]
});
}
2017-12-20 13:01:44 -06:00
}
2017-12-20 16:57:31 -06:00
if (poll != null) {
if (poll) {
push({
poll: {
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
poll: {
$exists: false
}
}, {
poll: null
}]
});
}
}
2017-12-20 13:01:44 -06:00
if (sinceDate) {
2017-12-20 16:35:16 -06:00
push({
2018-03-29 00:48:47 -05:00
createdAt: {
2017-12-20 16:35:16 -06:00
$gt: new Date(sinceDate)
}
});
2017-12-20 13:01:44 -06:00
}
if (untilDate) {
2017-12-20 16:35:16 -06:00
push({
2018-03-29 00:48:47 -05:00
createdAt: {
2017-12-20 16:35:16 -06:00
$lt: new Date(untilDate)
}
});
2017-12-20 13:01:44 -06:00
}
2016-12-28 16:49:51 -06:00
2017-12-21 00:37:26 -06:00
if (q.$and.length == 0) {
q = {};
}
2018-04-07 12:30:37 -05:00
// Search notes
const notes = await Note
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
2018-04-07 12:30:37 -05:00
res(await Promise.all(notes.map(async note =>
await pack(note, me))));
2016-12-28 16:49:51 -06:00
}