2016-12-29 00:49:51 +02:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2018-04-24 12:13:06 +03:00
|
|
|
import $ from 'cafy'; import ID from '../../../../cafy-id';
|
2018-03-31 13:55:00 +03:00
|
|
|
import User, { pack } from '../../../../models/user';
|
2018-04-01 22:15:27 +03:00
|
|
|
import resolveRemoteUser from '../../../../remote/resolve-user';
|
2018-03-27 10:51:12 +03:00
|
|
|
|
2018-03-31 13:55:00 +03:00
|
|
|
const cursorOption = { fields: { data: false } };
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-25 16:37:08 +03:00
|
|
|
* Show user(s)
|
2016-12-29 00:49:51 +02:00
|
|
|
*/
|
2017-03-03 21:28:38 +02:00
|
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
2018-03-27 10:51:12 +03:00
|
|
|
let user;
|
|
|
|
|
2018-03-29 08:48:47 +03:00
|
|
|
// Get 'userId' parameter
|
2018-04-27 13:12:15 +03:00
|
|
|
const [userId, userIdErr] = $(params.userId).optional.type(ID).get();
|
2018-03-29 08:48:47 +03:00
|
|
|
if (userIdErr) return rej('invalid userId param');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
// Get 'userIds' parameter
|
2018-04-27 13:12:15 +03:00
|
|
|
const [userIds, userIdsErr] = $(params.userIds).optional.array($().type(ID)).get();
|
2018-04-25 16:37:08 +03:00
|
|
|
if (userIdsErr) return rej('invalid userIds param');
|
|
|
|
|
2016-12-29 00:49:51 +02:00
|
|
|
// Get 'username' parameter
|
2018-04-27 13:12:15 +03:00
|
|
|
const [username, usernameErr] = $(params.username).optional.string().get();
|
2017-03-03 00:47:14 +02:00
|
|
|
if (usernameErr) return rej('invalid username param');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-03-27 10:51:12 +03:00
|
|
|
// Get 'host' parameter
|
2018-04-27 13:12:15 +03:00
|
|
|
const [host, hostErr] = $(params.host).nullable.optional.string().get();
|
2018-03-29 15:45:43 +03:00
|
|
|
if (hostErr) return rej('invalid host param');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
if (userIds) {
|
|
|
|
const users = await User.find({
|
|
|
|
_id: {
|
|
|
|
$in: userIds
|
|
|
|
}
|
|
|
|
});
|
2017-02-22 06:08:33 +02:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
res(await Promise.all(users.map(u => pack(u, me, {
|
|
|
|
detail: true
|
|
|
|
}))));
|
2018-03-27 10:51:12 +03:00
|
|
|
} else {
|
2018-04-25 16:37:08 +03: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 {
|
|
|
|
const q = userId !== undefined
|
|
|
|
? { _id: userId }
|
|
|
|
: { usernameLower: username.toLowerCase(), host: null };
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
user = await User.findOne(q, cursorOption);
|
2018-03-27 10:51:12 +03:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
2018-03-27 10:51:12 +03:00
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-04-25 16:37:08 +03:00
|
|
|
// Send response
|
|
|
|
res(await pack(user, me, {
|
|
|
|
detail: true
|
|
|
|
}));
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|