mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-13 16:53:08 +02:00
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
/**
|
|
* Module dependencies
|
|
*/
|
|
import $ from 'cafy'; import ID from '../../../../cafy-id';
|
|
import User, { pack } from '../../../../models/user';
|
|
import resolveRemoteUser from '../../../../remote/resolve-user';
|
|
|
|
const cursorOption = { fields: { data: false } };
|
|
|
|
/**
|
|
* Show user(s)
|
|
*/
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
|
let user;
|
|
|
|
// Get 'userId' parameter
|
|
const [userId, userIdErr] = $(params.userId).optional.type(ID).get();
|
|
if (userIdErr) return rej('invalid userId param');
|
|
|
|
// Get 'userIds' parameter
|
|
const [userIds, userIdsErr] = $(params.userIds).optional.array($().type(ID)).get();
|
|
if (userIdsErr) return rej('invalid userIds param');
|
|
|
|
// Get 'username' parameter
|
|
const [username, usernameErr] = $(params.username).optional.string().get();
|
|
if (usernameErr) return rej('invalid username param');
|
|
|
|
// Get 'host' parameter
|
|
const [host, hostErr] = $(params.host).nullable.optional.string().get();
|
|
if (hostErr) return rej('invalid host param');
|
|
|
|
if (userIds) {
|
|
const users = await User.find({
|
|
_id: {
|
|
$in: userIds
|
|
}
|
|
});
|
|
|
|
res(await Promise.all(users.map(u => pack(u, me, {
|
|
detail: true
|
|
}))));
|
|
} else {
|
|
// 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 };
|
|
|
|
user = await User.findOne(q, cursorOption);
|
|
|
|
if (user === null) {
|
|
return rej('user not found');
|
|
}
|
|
}
|
|
|
|
// Send response
|
|
res(await pack(user, me, {
|
|
detail: true
|
|
}));
|
|
}
|
|
});
|