2018-07-07 12:19:00 +02:00
|
|
|
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
|
2018-10-30 18:16:13 +01:00
|
|
|
import User, { pack, ILocalUser, isRemoteUser } from '../../../../models/user';
|
2018-04-01 21:15:27 +02:00
|
|
|
import resolveRemoteUser from '../../../../remote/resolve-user';
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-03-31 12:55:00 +02:00
|
|
|
const cursorOption = { fields: { data: false } };
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
/**
|
2018-04-25 15:37:08 +02:00
|
|
|
* Show user(s)
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
2018-07-05 19:58:29 +02:00
|
|
|
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
|
2018-03-27 09:51:12 +02:00
|
|
|
let user;
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'userId' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [userId, userIdErr] = $.type(ID).optional.get(params.userId);
|
2018-03-29 07:48:47 +02:00
|
|
|
if (userIdErr) return rej('invalid userId param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
// Get 'userIds' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [userIds, userIdsErr] = $.arr($.type(ID)).optional.get(params.userIds);
|
2018-04-25 15:37:08 +02:00
|
|
|
if (userIdsErr) return rej('invalid userIds param');
|
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
// Get 'username' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [username, usernameErr] = $.str.optional.get(params.username);
|
2017-03-02 23:47:14 +01:00
|
|
|
if (usernameErr) return rej('invalid username param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
// Get 'host' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [host, hostErr] = $.str.optional.nullable.get(params.host);
|
2018-03-29 14:45:43 +02:00
|
|
|
if (hostErr) return rej('invalid host param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
if (userIds) {
|
|
|
|
const users = await User.find({
|
|
|
|
_id: {
|
|
|
|
$in: userIds
|
|
|
|
}
|
|
|
|
});
|
2017-02-22 05:08:33 +01:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
res(await Promise.all(users.map(u => pack(u, me, {
|
|
|
|
detail: true
|
|
|
|
}))));
|
2018-03-27 09:51:12 +02:00
|
|
|
} else {
|
2018-04-25 15:37:08 +02:00
|
|
|
// Lookup user
|
|
|
|
if (typeof host === 'string') {
|
|
|
|
try {
|
|
|
|
user = await resolveRemoteUser(username, host, cursorOption);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(`failed to resolve remote user: ${e}`);
|
|
|
|
return rej('failed to resolve remote user');
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-18 02:54:53 +02:00
|
|
|
const q: any = userId !== undefined
|
2018-04-25 15:37:08 +02:00
|
|
|
? { _id: userId }
|
|
|
|
: { usernameLower: username.toLowerCase(), host: null };
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
user = await User.findOne(q, cursorOption);
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-04-25 15:37:08 +02:00
|
|
|
// Send response
|
|
|
|
res(await pack(user, me, {
|
|
|
|
detail: true
|
|
|
|
}));
|
2018-10-30 18:16:13 +01:00
|
|
|
|
|
|
|
if (isRemoteUser(user)) {
|
|
|
|
if (user.updatedAt == null || Date.now() - user.updatedAt.getTime() > 1000 * 60 * 60 * 24) {
|
|
|
|
resolveRemoteUser(username, host, { }, true);
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 15:37:08 +02:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|