1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-28 05:29:00 +09:00
misskey/src/server/api/endpoints/users/show.ts

105 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-02-05 11:48:08 +09:00
import $ from 'cafy';
import ID, { transform, transformMany } from '../../../../misc/cafy-id';
2018-11-02 13:47:44 +09:00
import User, { pack, isRemoteUser } from '../../../../models/user';
2018-04-02 04:15:27 +09:00
import resolveRemoteUser from '../../../../remote/resolve-user';
2018-11-02 13:47:44 +09:00
import define from '../../define';
import { apiLogger } from '../../logger';
import { ApiError } from '../../error';
2018-03-27 16:51:12 +09:00
2018-03-31 19:55:00 +09:00
const cursorOption = { fields: { data: false } };
2016-12-29 07:49:51 +09:00
2018-11-02 03:32:24 +09:00
export const meta = {
desc: {
'ja-JP': '指定したユーザーの情報を取得します。'
},
requireCredential: false,
2018-03-27 16:51:12 +09:00
2018-11-02 03:32:24 +09:00
params: {
userId: {
2019-02-13 16:33:07 +09:00
validator: $.optional.type(ID),
2018-11-02 03:32:24 +09:00
transform: transform,
desc: {
2018-11-03 22:49:36 +09:00
'ja-JP': '対象のユーザーのID',
'en-US': 'Target user ID'
2018-11-02 03:32:24 +09:00
}
},
2016-12-29 07:49:51 +09:00
2018-11-02 03:32:24 +09:00
userIds: {
2019-02-13 16:33:07 +09:00
validator: $.optional.arr($.type(ID)).unique(),
2018-11-02 03:32:24 +09:00
transform: transformMany,
desc: {
'ja-JP': 'ユーザーID (配列)'
}
},
2018-04-25 22:37:08 +09:00
2018-11-02 03:32:24 +09:00
username: {
2019-02-13 16:33:07 +09:00
validator: $.optional.str
2018-11-02 03:32:24 +09:00
},
2016-12-29 07:49:51 +09:00
2018-11-02 03:32:24 +09:00
host: {
2019-02-13 16:33:07 +09:00
validator: $.optional.nullable.str
2018-11-02 03:32:24 +09:00
}
},
errors: {
failedToResolveRemoteUser: {
message: 'Failed to resolve remote user.',
code: 'FAILED_TO_RESOLVE_REMOTE_USER',
id: 'ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c',
kind: 'server' as 'server'
},
noSuchUser: {
message: 'No such user.',
code: 'NO_SUCH_USER',
id: '4362f8dc-731f-4ad8-a694-be5a88922a24'
},
2018-11-02 03:32:24 +09:00
}
};
export default define(meta, async (ps, me) => {
2018-11-02 03:32:24 +09:00
let user;
2016-12-29 07:49:51 +09:00
2018-11-02 03:32:24 +09:00
if (ps.userIds) {
2018-04-25 22:37:08 +09:00
const users = await User.find({
_id: {
2018-11-02 03:32:24 +09:00
$in: ps.userIds
2018-04-25 22:37:08 +09:00
}
});
2017-02-22 13:08:33 +09:00
return await Promise.all(users.map(u => pack(u, me, {
2018-04-25 22:37:08 +09:00
detail: true
})));
2018-03-27 16:51:12 +09:00
} else {
2018-04-25 22:37:08 +09:00
// Lookup user
2018-11-02 03:32:24 +09:00
if (typeof ps.host === 'string') {
user = await resolveRemoteUser(ps.username, ps.host, cursorOption).catch(e => {
apiLogger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.failedToResolveRemoteUser);
});
2018-04-25 22:37:08 +09:00
} else {
2018-11-02 03:32:24 +09:00
const q: any = ps.userId != null
? { _id: ps.userId }
: { usernameLower: ps.username.toLowerCase(), host: null };
2016-12-29 07:49:51 +09:00
2018-04-25 22:37:08 +09:00
user = await User.findOne(q, cursorOption);
2019-02-15 23:43:49 +09:00
}
2018-03-27 16:51:12 +09:00
2019-02-15 23:43:49 +09:00
if (user === null) {
throw new ApiError(meta.errors.noSuchUser);
2018-03-27 16:51:12 +09:00
}
2016-12-29 07:49:51 +09:00
// ユーザー情報更新
if (isRemoteUser(user)) {
if (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
2018-11-02 03:32:24 +09:00
resolveRemoteUser(ps.username, ps.host, { }, true);
}
}
return await pack(user, me, {
detail: true
});
2018-04-25 22:37:08 +09:00
}
});