2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../define';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Users } from '../../../../models';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
2019-04-23 15:35:26 +02:00
|
|
|
import { bool, types } from '../../../../misc/schema';
|
2018-08-06 11:28:27 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'ユーザーを検索します。'
|
2018-08-06 11:28:27 +02:00
|
|
|
},
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2018-08-06 11:28:27 +02:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
2018-11-01 19:32:24 +01:00
|
|
|
query: {
|
|
|
|
validator: $.str,
|
2018-08-06 11:28:27 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'クエリ'
|
2018-08-06 11:28:27 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
offset: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.num.min(0),
|
2018-08-06 11:28:27 +02:00
|
|
|
default: 0,
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'オフセット'
|
2018-08-06 11:28:27 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
limit: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-08-06 11:28:27 +02:00
|
|
|
default: 10,
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': '取得する数'
|
2018-08-06 11:28:27 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
localOnly: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.bool,
|
2018-08-06 11:28:27 +02:00
|
|
|
default: false,
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'ローカルユーザーのみ検索対象にするか否か'
|
2018-08-06 11:28:27 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-12-01 22:44:18 +01:00
|
|
|
|
|
|
|
detail: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.bool,
|
2018-12-01 22:44:18 +01:00
|
|
|
default: true,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '詳細なユーザー情報を含めるか否か'
|
|
|
|
}
|
|
|
|
},
|
2018-08-06 11:28:27 +02:00
|
|
|
},
|
2019-02-24 11:42:26 +01:00
|
|
|
|
|
|
|
res: {
|
2019-04-23 15:35:26 +02:00
|
|
|
type: types.array,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
2019-02-24 11:42:26 +01:00
|
|
|
items: {
|
2019-04-23 15:35:26 +02:00
|
|
|
type: types.object,
|
|
|
|
optional: bool.false, nullable: bool.false,
|
|
|
|
ref: 'User',
|
2019-02-24 11:42:26 +01:00
|
|
|
}
|
|
|
|
},
|
2018-08-06 11:28:27 +02:00
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const isUsername = Users.validateUsername(ps.query.replace('@', ''), !ps.localOnly);
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
let users: User[] = [];
|
2018-08-06 11:28:27 +02:00
|
|
|
|
|
|
|
if (isUsername) {
|
2019-04-07 14:50:36 +02:00
|
|
|
users = await Users.createQueryBuilder('user')
|
|
|
|
.where('user.host IS NULL')
|
2019-04-08 09:39:02 +02:00
|
|
|
.andWhere('user.isSuspended = FALSE')
|
|
|
|
.andWhere('user.usernameLower like :username', { username: ps.query.replace('@', '').toLowerCase() + '%' })
|
2019-04-12 18:43:22 +02:00
|
|
|
.take(ps.limit!)
|
2019-04-07 14:50:36 +02:00
|
|
|
.skip(ps.offset)
|
|
|
|
.getMany();
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
if (users.length < ps.limit! && !ps.localOnly) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const otherUsers = await Users.createQueryBuilder('user')
|
|
|
|
.where('user.host IS NOT NULL')
|
2019-04-08 09:39:02 +02:00
|
|
|
.andWhere('user.isSuspended = FALSE')
|
|
|
|
.andWhere('user.usernameLower like :username', { username: ps.query.replace('@', '').toLowerCase() + '%' })
|
2019-04-12 18:43:22 +02:00
|
|
|
.take(ps.limit! - users.length)
|
2019-04-07 14:50:36 +02:00
|
|
|
.getMany();
|
2018-08-06 11:28:27 +02:00
|
|
|
|
|
|
|
users = users.concat(otherUsers);
|
|
|
|
}
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
return await Users.packMany(users, me, { detail: ps.detail });
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|