2019-01-11 08:09:44 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Users } from '../../../../models';
|
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-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const query = Users.createQueryBuilder('user');
|
|
|
|
|
|
|
|
switch (ps.state) {
|
|
|
|
case 'admin': query.where('user.isAdmin = TRUE'); break;
|
|
|
|
case 'moderator': query.where('user.isModerator = TRUE'); break;
|
|
|
|
case 'adminOrModerator': query.where('user.isAdmin = TRUE OR isModerator = TRUE'); break;
|
|
|
|
case 'verified': query.where('user.isVerified = TRUE'); break;
|
|
|
|
case 'alive': query.where('user.updatedAt > :date', { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) }); break;
|
|
|
|
case 'silenced': query.where('user.isSilenced = TRUE'); break;
|
|
|
|
case 'suspended': query.where('user.isSuspended = TRUE'); break;
|
|
|
|
}
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
switch (ps.origin) {
|
|
|
|
case 'local': query.andWhere('user.host IS NULL'); break;
|
|
|
|
case 'remote': query.andWhere('user.host IS NOT NULL'); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ps.sort) {
|
|
|
|
case '+follower': query.orderBy('user.followersCount', 'DESC'); break;
|
|
|
|
case '-follower': query.orderBy('user.followersCount', 'ASC'); break;
|
|
|
|
case '+createdAt': query.orderBy('user.createdAt', 'DESC'); break;
|
|
|
|
case '-createdAt': query.orderBy('user.createdAt', 'ASC'); break;
|
|
|
|
case '+updatedAt': query.orderBy('user.updatedAt', 'DESC'); break;
|
|
|
|
case '-updatedAt': query.orderBy('user.updatedAt', 'ASC'); break;
|
|
|
|
default: query.orderBy('user.id', 'ASC'); break;
|
|
|
|
}
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
query.take(ps.limit!);
|
2019-04-07 21:50:36 +09:00
|
|
|
query.skip(ps.offset);
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const users = await query.getMany();
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Users.packMany(users, me, { detail: true });
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|