Skip to content

Commit

Permalink
Resolve #544
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jan 4, 2020
1 parent 9651987 commit 01bdf15
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/server/api/endpoints/users/search.ts
Expand Up @@ -3,6 +3,8 @@ import * as escapeRegexp from 'escape-regexp';
import User, { pack, validateUsername, IUser } from '../../../../models/user';
import define from '../../define';
import { toDbHost } from '../../../../misc/convert-host';
import Instance from '../../../../models/instance';
import { concat } from '../../../../prelude/array';

export const meta = {
desc: {
Expand Down Expand Up @@ -69,9 +71,27 @@ export default define(meta, async (ps, me) => {

let users: IUser[] = [];

// 隠すインスタンス
const hideInstances = await Instance.find({
$or: [
{ isMarkedAsClosed: true },
{ isBlocked: true }
]
}, {
fields: {
host: true
}
});

// 隠すホスト
const hideHosts = hideInstances.map(x => toDbHost(x.host));
const hideHostsForRemote = concat([hideHosts, [null]]);

// 表示名
if (isName) {
const name = ps.query.replace(/^-/, '');

// local
users = await User
.find({
host: null,
Expand All @@ -83,9 +103,10 @@ export default define(meta, async (ps, me) => {
});

if (users.length < ps.limit && !ps.localOnly) {
// try remote
const otherUsers = await User
.find({
host: { $ne: null },
host: { $nin: hideHostsForRemote },
name: new RegExp('^' + escapeRegexp(name), 'i'),
isSuspended: { $ne: true }
}, {
Expand All @@ -94,10 +115,13 @@ export default define(meta, async (ps, me) => {

users = users.concat(otherUsers);
}
// ユーザー名
} else if (isUsername) {
// まず、username (local/remote) の完全一致でアクティブ順
if (users.length < ps.limit && !ps.localOnly) {
users = await User
.find({
host: { $nin: hideHosts },
usernameLower: ps.query.replace('@', '').toLowerCase(),
isSuspended: { $ne: true }
}, {
Expand All @@ -109,6 +133,7 @@ export default define(meta, async (ps, me) => {

const ids = users.map(user => user._id);

// 足りなかったら、username (local) の前方一致でid順
if (users.length < ps.limit) {
const otherUsers = await User
.find({
Expand All @@ -122,13 +147,14 @@ export default define(meta, async (ps, me) => {
});

users = users.concat(otherUsers);
}
}

// 足りなかったら、username (remote) の前方一致でid順
if (users.length < ps.limit && !ps.localOnly) {
const otherUsers = await User
.find({
_id: { $nin: ids },
host: { $ne: null },
host: { $nin: hideHostsForRemote },
usernameLower: new RegExp('^' + escapeRegexp(ps.query.replace('@', '').toLowerCase())),
isSuspended: { $ne: true }
}, {
Expand All @@ -138,6 +164,7 @@ export default define(meta, async (ps, me) => {

users = users.concat(otherUsers);
}
// ホスト名
} else if (isHostname) {
users = await User
.find({
Expand Down

0 comments on commit 01bdf15

Please sign in to comment.