2017-03-08 11:50:09 -07:00
|
|
|
import $ from 'cafy';
|
2018-11-01 22:47:44 -06:00
|
|
|
import define from '../define';
|
2019-04-07 06:50:36 -06:00
|
|
|
import { Users } from '../../../models';
|
|
|
|
import { generateMuteQueryForUsers } from '../common/generate-mute-query';
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
export const meta = {
|
2019-02-22 19:20:58 -07:00
|
|
|
tags: ['users'],
|
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-01 21:49:08 -06:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-01 21:49:08 -06:00
|
|
|
default: 0
|
|
|
|
},
|
2017-03-03 12:28:38 -07:00
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
sort: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.str.or([
|
2018-11-22 16:01:14 -07:00
|
|
|
'+follower',
|
|
|
|
'-follower',
|
|
|
|
'+createdAt',
|
|
|
|
'-createdAt',
|
|
|
|
'+updatedAt',
|
|
|
|
'-updatedAt',
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
|
2019-02-15 14:50:58 -07:00
|
|
|
state: {
|
|
|
|
validator: $.optional.str.or([
|
|
|
|
'all',
|
|
|
|
'admin',
|
|
|
|
'moderator',
|
|
|
|
'adminOrModerator',
|
|
|
|
'verified',
|
|
|
|
'alive'
|
|
|
|
]),
|
|
|
|
default: 'all'
|
|
|
|
},
|
|
|
|
|
2018-11-22 16:01:14 -07:00
|
|
|
origin: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.str.or([
|
2018-11-22 16:01:14 -07:00
|
|
|
'combined',
|
|
|
|
'local',
|
|
|
|
'remote',
|
|
|
|
]),
|
|
|
|
default: 'local'
|
2018-11-01 21:49:08 -06:00
|
|
|
}
|
2019-02-24 03:42:26 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'User',
|
|
|
|
}
|
|
|
|
},
|
2018-11-01 21:49:08 -06:00
|
|
|
};
|
2017-03-03 12:28:38 -07:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
export default define(meta, async (ps, me) => {
|
|
|
|
const query = Users.createQueryBuilder('user');
|
2019-02-15 14:50:58 -07:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
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;
|
|
|
|
}
|
2019-02-15 14:50:58 -07:00
|
|
|
|
2019-04-07 06:50:36 -06:00
|
|
|
switch (ps.origin) {
|
|
|
|
case 'local': query.andWhere('user.host IS NULL'); break;
|
|
|
|
case 'remote': query.andWhere('user.host IS NOT NULL'); break;
|
|
|
|
}
|
2018-11-22 16:01:14 -07:00
|
|
|
|
2019-04-07 06:50:36 -06: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me) generateMuteQueryForUsers(query, me);
|
|
|
|
|
|
|
|
query.take(ps.limit);
|
|
|
|
query.skip(ps.offset);
|
|
|
|
|
|
|
|
const users = await query.getMany();
|
|
|
|
|
|
|
|
return await Users.packMany(users, me, { detail: true });
|
2019-02-21 19:46:58 -07:00
|
|
|
});
|