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