rudeshark.net/src/server/api/endpoints/users.ts
syuilo 52774bbe64
Introduce OpenAPI specs (#4351)
* wip

* wip

* wip

* Update index.ts

* Update gen-openapi-spec.ts

* Update api.ja-JP.md

* Fix

* Improve doc

* Update gen-openapi-spec.ts

* Update redoc.html

* Improve doc

* Update gen-openapi-spec.ts

* Improve doc

* Update CHANGELOG.md
2019-02-23 11:20:58 +09:00

109 lines
2.2 KiB
TypeScript

import $ from 'cafy';
import User, { pack } from '../../../models/user';
import define from '../define';
import { fallback } from '../../../prelude/symbol';
import { getHideUserIds } from '../common/get-hide-users';
const nonnull = { $ne: null as any };
export const meta = {
tags: ['users'],
requireCredential: false,
params: {
limit: {
validator: $.optional.num.range(1, 100),
default: 10
},
offset: {
validator: $.optional.num.min(0),
default: 0
},
sort: {
validator: $.optional.str.or([
'+follower',
'-follower',
'+createdAt',
'-createdAt',
'+updatedAt',
'-updatedAt',
]),
},
state: {
validator: $.optional.str.or([
'all',
'admin',
'moderator',
'adminOrModerator',
'verified',
'alive'
]),
default: 'all'
},
origin: {
validator: $.optional.str.or([
'combined',
'local',
'remote',
]),
default: 'local'
}
}
};
const state: any = { // < https://github.com/Microsoft/TypeScript/issues/1863
'admin': { isAdmin: true },
'moderator': { isModerator: true },
'adminOrModerator': {
$or: [
{ isAdmin: true },
{ isModerator: true }
]
},
'verified': { isVerified: true },
'alive': {
updatedAt: { $gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) }
},
[fallback]: {}
};
const origin: any = { // < https://github.com/Microsoft/TypeScript/issues/1863
'local': { host: null },
'remote': { host: nonnull },
[fallback]: {}
};
const sort: any = { // < https://github.com/Microsoft/TypeScript/issues/1863
'+follower': { followersCount: -1 },
'-follower': { followersCount: 1 },
'+createdAt': { createdAt: -1 },
'-createdAt': { createdAt: 1 },
'+updatedAt': { updatedAt: -1 },
'-updatedAt': { updatedAt: 1 },
[fallback]: { _id: -1 }
};
export default define(meta, async (ps, me) => {
const hideUserIds = await getHideUserIds(me);
const users = await User
.find({
$and: [
state[ps.state] || state[fallback],
origin[ps.origin] || origin[fallback]
],
...(hideUserIds && hideUserIds.length > 0 ? { _id: { $nin: hideUserIds } } : {})
}, {
limit: ps.limit,
sort: sort[ps.sort] || sort[fallback],
skip: ps.offset
});
return await Promise.all(users.map(user => pack(user, me, { detail: true })));
});