2019-02-04 20:48:08 -06:00
|
|
|
import $ from 'cafy';
|
2019-04-07 09:06:07 -05:00
|
|
|
import { resolveUser } from '../../../../remote/resolve-user';
|
2018-11-01 23:47:44 -05:00
|
|
|
import define from '../../define';
|
2019-02-03 03:16:57 -06:00
|
|
|
import { apiLogger } from '../../logger';
|
2019-02-21 20:46:58 -06:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
|
|
|
import { Users } from '../../../../models';
|
|
|
|
import { In } from 'typeorm';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '指定したユーザーの情報を取得します。'
|
|
|
|
},
|
|
|
|
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 06:33:32 -06:00
|
|
|
requireCredential: false as const,
|
2018-03-27 02:51:12 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
params: {
|
|
|
|
userId: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 13:32:24 -05:00
|
|
|
desc: {
|
2018-11-03 08:49:36 -05:00
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
2018-11-01 13:32:24 -05:00
|
|
|
}
|
|
|
|
},
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
userIds: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.arr($.type(ID)).unique(),
|
2018-11-01 13:32:24 -05:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ユーザーID (配列)'
|
|
|
|
}
|
|
|
|
},
|
2018-04-25 08:37:08 -05:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
username: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.str
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
host: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.nullable.str
|
2018-11-01 13:32:24 -05:00
|
|
|
}
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
|
|
|
|
2019-02-22 20:20:58 -06:00
|
|
|
res: {
|
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-22 20:20:58 -06:00
|
|
|
},
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
errors: {
|
|
|
|
failedToResolveRemoteUser: {
|
|
|
|
message: 'Failed to resolve remote user.',
|
|
|
|
code: 'FAILED_TO_RESOLVE_REMOTE_USER',
|
|
|
|
id: 'ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c',
|
2019-06-28 02:38:48 -05:00
|
|
|
kind: 'server' as const
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: '4362f8dc-731f-4ad8-a694-be5a88922a24'
|
|
|
|
},
|
2018-11-01 13:32:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
export default define(meta, async (ps, me) => {
|
2018-11-01 13:32:24 -05:00
|
|
|
let user;
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2020-01-01 11:47:20 -06:00
|
|
|
const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
if (ps.userIds) {
|
2019-04-14 22:54:42 -05:00
|
|
|
if (ps.userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-01 11:47:20 -06:00
|
|
|
const users = await Users.find(isAdminOrModerator ? {
|
2019-04-07 07:50:36 -05:00
|
|
|
id: In(ps.userIds)
|
2020-01-01 11:47:20 -06:00
|
|
|
} : {
|
|
|
|
id: In(ps.userIds),
|
|
|
|
isSuspended: false
|
2018-04-25 08:37:08 -05:00
|
|
|
});
|
2017-02-21 22:08:33 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
return await Promise.all(users.map(u => Users.pack(u, me, {
|
2018-04-25 08:37:08 -05:00
|
|
|
detail: true
|
2019-02-21 20:46:58 -06:00
|
|
|
})));
|
2018-03-27 02:51:12 -05:00
|
|
|
} else {
|
2018-04-25 08:37:08 -05:00
|
|
|
// Lookup user
|
2019-04-12 11:43:22 -05:00
|
|
|
if (typeof ps.host === 'string' && typeof ps.username === 'string') {
|
2019-04-07 09:06:07 -05:00
|
|
|
user = await resolveUser(ps.username, ps.host).catch(e => {
|
2019-02-03 03:16:57 -06:00
|
|
|
apiLogger.warn(`failed to resolve remote user: ${e}`);
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.failedToResolveRemoteUser);
|
|
|
|
});
|
2018-04-25 08:37:08 -05:00
|
|
|
} else {
|
2018-11-01 13:32:24 -05:00
|
|
|
const q: any = ps.userId != null
|
2019-04-07 07:50:36 -05:00
|
|
|
? { id: ps.userId }
|
2019-04-12 11:43:22 -05:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: null };
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
user = await Users.findOne(q);
|
2019-02-15 08:43:49 -06:00
|
|
|
}
|
2018-03-27 02:51:12 -05:00
|
|
|
|
2020-01-01 11:47:20 -06:00
|
|
|
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2018-03-27 02:51:12 -05:00
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
return await Users.pack(user, me, {
|
2019-02-21 20:46:58 -06:00
|
|
|
detail: true
|
|
|
|
});
|
2018-04-25 08:37:08 -05:00
|
|
|
}
|
2019-02-21 20:46:58 -06:00
|
|
|
});
|