2019-01-10 23:09:44 +00:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Users } from '../../../../models';
|
2019-01-10 23:09:44 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2019-01-10 23:09:44 +00:00
|
|
|
requireCredential: true,
|
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2019-01-10 23:09:44 +00:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.min(0),
|
2019-01-10 23:09:44 +00:00
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
sort: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.str.or([
|
2019-01-10 23:09:44 +00:00
|
|
|
'+follower',
|
|
|
|
'-follower',
|
|
|
|
'+createdAt',
|
|
|
|
'-createdAt',
|
|
|
|
'+updatedAt',
|
|
|
|
'-updatedAt',
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
|
|
|
|
state: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.str.or([
|
2019-01-10 23:09:44 +00:00
|
|
|
'all',
|
2019-09-21 12:35:22 +00:00
|
|
|
'available',
|
2019-01-10 23:09:44 +00:00
|
|
|
'admin',
|
|
|
|
'moderator',
|
|
|
|
'adminOrModerator',
|
2019-02-10 10:43:46 +00:00
|
|
|
'silenced',
|
2019-01-10 23:09:44 +00:00
|
|
|
'suspended',
|
|
|
|
]),
|
|
|
|
default: 'all'
|
|
|
|
},
|
|
|
|
|
|
|
|
origin: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.str.or([
|
2019-01-10 23:09:44 +00:00
|
|
|
'combined',
|
|
|
|
'local',
|
|
|
|
'remote',
|
|
|
|
]),
|
|
|
|
default: 'local'
|
2019-07-17 09:59:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
username: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
|
|
|
|
hostname: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
default: null
|
2019-01-10 23:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const query = Users.createQueryBuilder('user');
|
|
|
|
|
|
|
|
switch (ps.state) {
|
2019-09-21 12:35:22 +00:00
|
|
|
case 'available': query.where('user.isSuspended = FALSE'); break;
|
2019-04-07 12:50:36 +00:00
|
|
|
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 '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-10 23:09:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
switch (ps.origin) {
|
|
|
|
case 'local': query.andWhere('user.host IS NULL'); break;
|
|
|
|
case 'remote': query.andWhere('user.host IS NOT NULL'); break;
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:59:10 +00:00
|
|
|
if (ps.username) {
|
|
|
|
query.andWhere('user.usernameLower like :username', { username: ps.username.toLowerCase() + '%' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.hostname) {
|
|
|
|
query.andWhere('user.host like :hostname', { hostname: '%' + ps.hostname.toLowerCase() + '%' });
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
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-10 23:09:44 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
query.take(ps.limit!);
|
2019-04-07 12:50:36 +00:00
|
|
|
query.skip(ps.offset);
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const users = await query.getMany();
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Users.packMany(users, me, { detail: true });
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|