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