2019-01-11 08:09:44 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import User, { pack } from '../../../../models/user';
|
|
|
|
import define from '../../define';
|
2019-02-17 21:40:53 +09:00
|
|
|
import { fallback } from '../../../../prelude/symbol';
|
2019-01-11 08:09:44 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2019-01-11 08:09:44 +09:00
|
|
|
requireCredential: true,
|
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2019-01-11 08:09:44 +09:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.min(0),
|
2019-01-11 08:09:44 +09:00
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
sort: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 08:09:44 +09:00
|
|
|
'+follower',
|
|
|
|
'-follower',
|
|
|
|
'+createdAt',
|
|
|
|
'-createdAt',
|
|
|
|
'+updatedAt',
|
|
|
|
'-updatedAt',
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
|
|
|
|
state: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 08:09:44 +09:00
|
|
|
'all',
|
|
|
|
'admin',
|
|
|
|
'moderator',
|
|
|
|
'adminOrModerator',
|
|
|
|
'verified',
|
2019-02-10 19:43:46 +09:00
|
|
|
'silenced',
|
2019-01-11 08:09:44 +09:00
|
|
|
'suspended',
|
|
|
|
]),
|
|
|
|
default: 'all'
|
|
|
|
},
|
|
|
|
|
|
|
|
origin: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 08:09:44 +09:00
|
|
|
'combined',
|
|
|
|
'local',
|
|
|
|
'remote',
|
|
|
|
]),
|
|
|
|
default: 'local'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-17 21:40:53 +09:00
|
|
|
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 }
|
|
|
|
};
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-01-11 08:09:44 +09:00
|
|
|
const q = {
|
|
|
|
$and: []
|
|
|
|
} as any;
|
|
|
|
|
|
|
|
// state
|
|
|
|
q.$and.push(
|
|
|
|
ps.state == 'admin' ? { isAdmin: true } :
|
|
|
|
ps.state == 'moderator' ? { isModerator: true } :
|
|
|
|
ps.state == 'adminOrModerator' ? {
|
|
|
|
$or: [{
|
|
|
|
isAdmin: true
|
|
|
|
}, {
|
|
|
|
isModerator: true
|
|
|
|
}]
|
|
|
|
} :
|
|
|
|
ps.state == 'verified' ? { isVerified: true } :
|
2019-02-10 19:43:46 +09:00
|
|
|
ps.state == 'silenced' ? { isSilenced: true } :
|
2019-01-11 08:09:44 +09:00
|
|
|
ps.state == 'suspended' ? { isSuspended: true } :
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
// origin
|
|
|
|
q.$and.push(
|
|
|
|
ps.origin == 'local' ? { host: null } :
|
|
|
|
ps.origin == 'remote' ? { host: { $ne: null } } :
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
const users = await User
|
|
|
|
.find(q, {
|
|
|
|
limit: ps.limit,
|
2019-02-17 21:40:53 +09:00
|
|
|
sort: sort[ps.sort] || sort[fallback],
|
2019-01-11 08:09:44 +09:00
|
|
|
skip: ps.offset
|
|
|
|
});
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
return await Promise.all(users.map(user => pack(user, me, { detail: true })));
|
|
|
|
});
|