jormungandr-bite/src/server/api/endpoints/users.ts
2018-04-27 19:12:15 +09:00

54 lines
1.1 KiB
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack } from '../../../models/user';
/**
* Lists all users
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).get();
if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).get();
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort, sortError] = $(params.sort).optional.string().or('+follower|-follower').get();
if (sortError) return rej('invalid sort param');
// Construct query
let _sort;
if (sort) {
if (sort == '+follower') {
_sort = {
followersCount: -1
};
} else if (sort == '-follower') {
_sort = {
followersCount: 1
};
}
} else {
_sort = {
_id: -1
};
}
// Issue query
const users = await User
.find({
host: null
}, {
limit: limit,
sort: _sort,
skip: offset
});
// Serialize
res(await Promise.all(users.map(async user =>
await pack(user, me))));
});