1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-21 10:08:24 +09:00
misskey/src/server/api/endpoints/users.ts
2018-04-16 15:42:34 +09:00

59 lines
1.2 KiB
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
import User, { pack } from '../../../models/user';
/**
* Lists all users
*
* @param {any} params
* @param {any} me
* @return {Promise<any>}
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort, sortError] = $(params.sort).optional.string().or('+follower|-follower').$;
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))));
});