diff --git a/src/api/endpoints/posts/search.ts b/src/api/endpoints/posts/search.ts index dba7a53b5f..88cdd32dac 100644 --- a/src/api/endpoints/posts/search.ts +++ b/src/api/endpoints/posts/search.ts @@ -6,6 +6,7 @@ import $ from 'cafy'; const escapeRegexp = require('escape-regexp'); import Post from '../../models/post'; import User from '../../models/user'; +import getFriends from '../../common/get-friends'; import serialize from '../../serializers/post'; import config from '../../../conf'; @@ -29,6 +30,10 @@ module.exports = (params, me) => new Promise(async (res, rej) => { const [username, usernameErr] = $(params.username).optional.string().$; if (usernameErr) return rej('invalid username param'); + // Get 'following' parameter + const [following = null, followingErr] = $(params.following).optional.nullable.boolean().$; + if (followingErr) return rej('invalid following param'); + // Get 'include_replies' parameter const [includeReplies = true, includeRepliesErr] = $(params.include_replies).optional.boolean().$; if (includeRepliesErr) return rej('invalid include_replies param'); @@ -67,11 +72,11 @@ module.exports = (params, me) => new Promise(async (res, rej) => { // If Elasticsearch is available, search by it // If not, search by MongoDB (config.elasticsearch.enable ? byElasticsearch : byNative) - (res, rej, me, text, user, includeReplies, withMedia, sinceDate, untilDate, offset, limit); + (res, rej, me, text, user, following, includeReplies, withMedia, sinceDate, untilDate, offset, limit); }); // Search by MongoDB -async function byNative(res, rej, me, text, userId, includeReplies, withMedia, sinceDate, untilDate, offset, max) { +async function byNative(res, rej, me, text, userId, following, includeReplies, withMedia, sinceDate, untilDate, offset, max) { const q: any = {}; if (text) { @@ -84,6 +89,16 @@ async function byNative(res, rej, me, text, userId, includeReplies, withMedia, s q.user_id = userId; } + if (following != null) { + const ids = await getFriends(me._id, false); + q.user_id = {}; + if (following) { + q.user_id.$in = ids; + } else { + q.user_id.$nin = ids; + } + } + if (!includeReplies) { q.reply_id = null; } @@ -122,7 +137,7 @@ async function byNative(res, rej, me, text, userId, includeReplies, withMedia, s } // Search by Elasticsearch -async function byElasticsearch(res, rej, me, text, userId, includeReplies, withMedia, sinceDate, untilDate, offset, max) { +async function byElasticsearch(res, rej, me, text, userId, following, includeReplies, withMedia, sinceDate, untilDate, offset, max) { const es = require('../../db/elasticsearch'); es.search({ diff --git a/src/web/app/common/scripts/parse-search-query.ts b/src/web/app/common/scripts/parse-search-query.ts index adcbfbb8fe..62b2cf51b1 100644 --- a/src/web/app/common/scripts/parse-search-query.ts +++ b/src/web/app/common/scripts/parse-search-query.ts @@ -10,6 +10,9 @@ export default function(qs: string) { case 'user': q['username'] = value; break; + case 'follow': + q['following'] = value == 'null' ? null : value == 'true'; + break; case 'reply': q['include_replies'] = value == 'true'; break; diff --git a/src/web/docs/search.ja.pug b/src/web/docs/search.ja.pug index f7ec9519f5..7d4d23fb6a 100644 --- a/src/web/docs/search.ja.pug +++ b/src/web/docs/search.ja.pug @@ -21,6 +21,9 @@ section tr td user td ユーザー名。投稿者を限定します。 + tr + td follow + td フォローしているユーザーのみに限定。(trueかfalse) tr td reply td 返信を含めるか否か。(trueかfalse)