This commit is contained in:
こぴなたみぽ 2017-12-21 07:35:16 +09:00
parent 143a30295d
commit 40f5e67ff0
3 changed files with 131 additions and 35 deletions

View file

@ -34,13 +34,17 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$; const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$;
if (followingErr) return rej('invalid following param'); if (followingErr) return rej('invalid following param');
// Get 'include_replies' parameter // Get 'reply' parameter
const [includeReplies = true, includeRepliesErr] = $(params.include_replies).optional.boolean().$; const [reply = null, replyErr] = $(params.reply).optional.nullable.boolean().$;
if (includeRepliesErr) return rej('invalid include_replies param'); if (replyErr) return rej('invalid reply param');
// Get 'with_media' parameter // Get 'repost' parameter
const [withMedia = false, withMediaErr] = $(params.with_media).optional.boolean().$; const [repost = null, repostErr] = $(params.repost).optional.nullable.boolean().$;
if (withMediaErr) return rej('invalid with_media param'); 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');
// Get 'since_date' parameter // Get 'since_date' parameter
const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$; const [sinceDate, sinceDateErr] = $(params.since_date).optional.number().$;
@ -72,53 +76,119 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
// If Elasticsearch is available, search by it // If Elasticsearch is available, search by it
// If not, search by MongoDB // If not, search by MongoDB
(config.elasticsearch.enable ? byElasticsearch : byNative) (config.elasticsearch.enable ? byElasticsearch : byNative)
(res, rej, me, text, user, following, includeReplies, withMedia, sinceDate, untilDate, offset, limit); (res, rej, me, text, user, following, reply, repost, media, sinceDate, untilDate, offset, limit);
}); });
// Search by MongoDB // Search by MongoDB
async function byNative(res, rej, me, text, userId, following, includeReplies, withMedia, sinceDate, untilDate, offset, max) { async function byNative(res, rej, me, text, userId, following, reply, repost, media, sinceDate, untilDate, offset, max) {
const q: any = {}; const q: any = {
$and: []
};
const push = q.$and.push;
if (text) { if (text) {
q.$and = text.split(' ').map(x => ({ push({
text: new RegExp(escapeRegexp(x)) $and: text.split(' ').map(x => ({
})); text: new RegExp(escapeRegexp(x))
}))
});
} }
if (userId) { if (userId) {
q.user_id = userId; push({
user_id: userId
});
} }
if (following != null) { if (following != null) {
const ids = await getFriends(me._id, false); const ids = await getFriends(me._id, false);
q.user_id = {}; push({
if (following) { user_id: following ? {
q.user_id.$in = ids; $in: ids
} : {
$nin: ids
}
});
}
if (reply != null) {
if (reply) {
push({
reply_id: {
$exists: true,
$ne: null
}
});
} else { } else {
q.user_id.$nin = ids; push({
$or: [{
reply_id: {
$exists: false
}
}, {
reply_id: null
}]
});
} }
} }
if (!includeReplies) { if (repost != null) {
q.reply_id = null; if (repost) {
push({
repost_id: {
$exists: true,
$ne: null
}
});
} else {
push({
$or: [{
repost_id: {
$exists: false
}
}, {
repost_id: null
}]
});
}
} }
if (withMedia) { if (media != null) {
q.media_ids = { if (media) {
$exists: true, push({
$ne: null media_ids: {
}; $exists: true,
$ne: null
}
});
} else {
push({
$or: [{
media_ids: {
$exists: false
}
}, {
media_ids: null
}]
});
}
} }
if (sinceDate) { if (sinceDate) {
q.created_at = { push({
$gt: new Date(sinceDate) created_at: {
}; $gt: new Date(sinceDate)
}
});
} }
if (untilDate) { if (untilDate) {
if (q.created_at == undefined) q.created_at = {}; push({
q.created_at.$lt = new Date(untilDate); created_at: {
$lt: new Date(untilDate)
}
});
} }
// Search posts // Search posts
@ -137,7 +207,7 @@ async function byNative(res, rej, me, text, userId, following, includeReplies, w
} }
// Search by Elasticsearch // Search by Elasticsearch
async function byElasticsearch(res, rej, me, text, userId, following, includeReplies, withMedia, sinceDate, untilDate, offset, max) { async function byElasticsearch(res, rej, me, text, userId, following, reply, repost, media, sinceDate, untilDate, offset, max) {
const es = require('../../db/elasticsearch'); const es = require('../../db/elasticsearch');
es.search({ es.search({

View file

@ -14,10 +14,13 @@ export default function(qs: string) {
q['following'] = value == 'null' ? null : value == 'true'; q['following'] = value == 'null' ? null : value == 'true';
break; break;
case 'reply': case 'reply':
q['include_replies'] = value == 'true'; q['reply'] = value == 'null' ? null : value == 'true';
break;
case 'repost':
q['repost'] = value == 'null' ? null : value == 'true';
break; break;
case 'media': case 'media':
q['with_media'] = value == 'true'; q['media'] = value == 'null' ? null : value == 'true';
break; break;
case 'until': case 'until':
case 'since': case 'since':

View file

@ -23,13 +23,36 @@ section
td ユーザー名。投稿者を限定します。 td ユーザー名。投稿者を限定します。
tr tr
td follow td follow
td フォローしているユーザーのみに限定。(trueかfalse) td
| true ... フォローしているユーザーに限定。
br
| false ... フォローしていないユーザーに限定。
br
| null ... 特に限定しない(デフォルト)
tr tr
td reply td reply
td 返信を含めるか否か。(trueかfalse) td
| true ... 返信に限定。
br
| false ... 返信でない投稿に限定。
br
| null ... 特に限定しない(デフォルト)
tr
td repost
td
| true ... Repostに限定。
br
| false ... Repostでない投稿に限定。
br
| null ... 特に限定しない(デフォルト)
tr tr
td media td media
td メディアが添付されているか。(trueかfalse) td
| true ... メディアが添付されている投稿に限定。
br
| false ... メディアが添付されていない投稿に限定。
br
| null ... 特に限定しない(デフォルト)
tr tr
td until td until
td 上限の日時。(YYYY-MM-DD) td 上限の日時。(YYYY-MM-DD)