Skip to content

Commit

Permalink
検索で特定のユーザーがフォローしているユーザーフィルタ
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jan 8, 2020
1 parent 2056d83 commit 54acf79
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/server/api/endpoints/notes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { toDbHost, isSelfHost } from '../../../../misc/convert-host';
import Following from '../../../../models/following';
import { concat } from '../../../../prelude/array';
import { getHideUserIds } from '../../common/get-hide-users';
import { getFriends } from '../../common/get-friends';
import { getFriends, getFriendIds } from '../../common/get-friends';
import NoteWatching from '../../../../models/note-watching';
const escapeRegexp = require('escape-regexp');

Expand Down Expand Up @@ -109,6 +109,7 @@ async function searchInternal(me: ILocalUser, query: string, limit: number, offs
const tokens = query.trim().split(/\s+/);
const words: string[] = [];
let from: IUser = null;
let followFrom: IUser = null;
let since: Date = null;
let until: Date = null;
let types: string[] = [];
Expand All @@ -134,6 +135,20 @@ async function searchInternal(me: ILocalUser, query: string, limit: number, offs
continue;
}

// followers
const matchFollow = token.match(/^follow:@?([\w-]+)(?:@([\w.-]+))?$/);
if (matchFollow) {
followFrom = await User.findOne({
usernameLower: matchFollow[1].toLowerCase(),
host: toDbHost(matchFollow[2]),
});

if (followFrom == null) return [];

filtered = true;
continue;
}

// Date
const matchSince = token.match(/^since:(\d{4}-\d{1,2}-\d{1,2}.*)/);
if (matchSince) {
Expand Down Expand Up @@ -271,6 +286,25 @@ async function searchInternal(me: ILocalUser, query: string, limit: number, offs
}];
}
}
} else if (followFrom != null) {
const myFollowingIds = me ? await getFriendIds(me._id) : [];

visibleQuery = [{
visibility: {
$in: ['public', 'home'],
},
}, {
$and: [
{ visibility: 'followers' },
{ userId: { $in: myFollowingIds } }
]
}, {
// myself (for specified/private)
userId: me._id
}, {
// to me (for specified)
visibleUserIds: { $in: [ me._id ] }
}];
} else {
// フォローを取得
const followings = await getFriends(me._id);
Expand Down Expand Up @@ -321,6 +355,11 @@ async function searchInternal(me: ILocalUser, query: string, limit: number, offs
noteQuery.userId = from._id;
}

if (followFrom != null) {
const targetFollowingIds = await getFriendIds(followFrom._id);
noteQuery.userId = { $in: targetFollowingIds };
}

// Date
if (since) {
noteQuery.$and.push({ createdAt: { $gt: since } });
Expand Down

0 comments on commit 54acf79

Please sign in to comment.