paricafe/src/server/api/endpoints/users/show.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
/**
* Module dependencies
*/
2017-03-08 12:50:09 -06:00
import $ from 'cafy';
2018-03-31 05:55:00 -05:00
import User, { pack } from '../../../../models/user';
import resolveRemoteUser from '../../../../common/remote/resolve-user';
2018-03-27 02:51:12 -05:00
2018-03-31 05:55:00 -05:00
const cursorOption = { fields: { data: false } };
2016-12-28 16:49:51 -06:00
/**
* Show a user
*
2017-03-01 02:37:01 -06:00
* @param {any} params
* @param {any} me
* @return {Promise<any>}
2016-12-28 16:49:51 -06:00
*/
2017-03-03 13:28:38 -06:00
module.exports = (params, me) => new Promise(async (res, rej) => {
2018-03-27 02:51:12 -05:00
let user;
2018-03-29 00:48:47 -05:00
// Get 'userId' parameter
const [userId, userIdErr] = $(params.userId).optional.id().$;
if (userIdErr) return rej('invalid userId param');
2016-12-28 16:49:51 -06:00
// Get 'username' parameter
2017-03-08 12:50:09 -06:00
const [username, usernameErr] = $(params.username).optional.string().$;
2017-03-02 16:47:14 -06:00
if (usernameErr) return rej('invalid username param');
2016-12-28 16:49:51 -06:00
2018-03-27 02:51:12 -05:00
// Get 'host' parameter
const [host, hostErr] = $(params.host).nullable.optional.string().$;
2018-03-29 07:45:43 -05:00
if (hostErr) return rej('invalid host param');
2016-12-28 16:49:51 -06:00
2018-03-27 02:51:12 -05:00
if (userId === undefined && typeof username !== 'string') {
2018-03-29 00:48:47 -05:00
return rej('userId or pair of username and host is required');
2018-03-27 02:51:12 -05:00
}
2017-02-21 22:08:33 -06:00
2016-12-28 16:49:51 -06:00
// Lookup user
2018-03-27 02:51:12 -05:00
if (typeof host === 'string') {
2018-03-31 05:55:00 -05:00
try {
user = await resolveRemoteUser(username, host, cursorOption);
} catch (exception) {
return rej('failed to resolve remote user');
2017-02-21 22:08:33 -06:00
}
2018-03-27 02:51:12 -05:00
} else {
const q = userId !== undefined
? { _id: userId }
2018-03-29 00:48:47 -05:00
: { usernameLower: username.toLowerCase(), host: null };
2016-12-28 16:49:51 -06:00
2018-03-31 05:55:00 -05:00
user = await User.findOne(q, cursorOption);
2018-03-27 02:51:12 -05:00
if (user === null) {
return rej('user not found');
}
2016-12-28 16:49:51 -06:00
}
// Send response
2018-02-01 17:21:30 -06:00
res(await pack(user, me, {
2016-12-28 16:49:51 -06:00
detail: true
}));
});