2017-03-08 18:50:09 +00:00
|
|
|
import $ from 'cafy';
|
2018-11-02 04:47:44 +00:00
|
|
|
import define from '../define';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Users } from '../../../models';
|
2020-07-28 00:38:41 +00:00
|
|
|
import { generateMutedUserQueryForUsers } from '../common/generate-muted-user-query';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: false as const,
|
2018-11-02 03:49:08 +00:00
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 03:49:08 +00:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-02 03:49:08 +00:00
|
|
|
default: 0
|
|
|
|
},
|
2017-03-03 19:28:38 +00:00
|
|
|
|
2018-11-02 03:49:08 +00:00
|
|
|
sort: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.str.or([
|
2018-11-22 23:01:14 +00:00
|
|
|
'+follower',
|
|
|
|
'-follower',
|
|
|
|
'+createdAt',
|
|
|
|
'-createdAt',
|
|
|
|
'+updatedAt',
|
|
|
|
'-updatedAt',
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
|
2019-02-15 21:50:58 +00:00
|
|
|
state: {
|
|
|
|
validator: $.optional.str.or([
|
|
|
|
'all',
|
|
|
|
'admin',
|
|
|
|
'moderator',
|
|
|
|
'adminOrModerator',
|
|
|
|
'alive'
|
|
|
|
]),
|
|
|
|
default: 'all'
|
|
|
|
},
|
|
|
|
|
2018-11-22 23:01:14 +00:00
|
|
|
origin: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.str.or([
|
2018-11-22 23:01:14 +00:00
|
|
|
'combined',
|
|
|
|
'local',
|
|
|
|
'remote',
|
|
|
|
]),
|
|
|
|
default: 'local'
|
2018-11-02 03:49:08 +00:00
|
|
|
}
|
2019-02-24 10:42:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 10:42:26 +00:00
|
|
|
items: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'User',
|
2019-02-24 10:42:26 +00:00
|
|
|
}
|
|
|
|
},
|
2018-11-02 03:49:08 +00:00
|
|
|
};
|
2017-03-03 19:28:38 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
export default define(meta, async (ps, me) => {
|
|
|
|
const query = Users.createQueryBuilder('user');
|
2020-12-11 12:16:20 +00:00
|
|
|
query.where('user.isExplorable = TRUE');
|
2019-02-15 21:50:58 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
switch (ps.state) {
|
2020-12-11 12:16:20 +00:00
|
|
|
case 'admin': query.andWhere('user.isAdmin = TRUE'); break;
|
|
|
|
case 'moderator': query.andWhere('user.isModerator = TRUE'); break;
|
2021-03-06 13:34:26 +00:00
|
|
|
case 'adminOrModerator': query.andWhere('user.isAdmin = TRUE OR user.isModerator = TRUE'); break;
|
2020-12-11 12:16:20 +00:00
|
|
|
case 'alive': query.andWhere('user.updatedAt > :date', { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) }); break;
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2019-02-15 21:50:58 +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;
|
|
|
|
}
|
2018-11-22 23:01:14 +00:00
|
|
|
|
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;
|
2019-05-06 08:28:55 +00:00
|
|
|
case '+updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'DESC'); break;
|
|
|
|
case '-updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'ASC'); break;
|
2019-04-07 12:50:36 +00:00
|
|
|
default: query.orderBy('user.id', 'ASC'); break;
|
|
|
|
}
|
|
|
|
|
2020-07-28 00:38:41 +00:00
|
|
|
if (me) generateMutedUserQueryForUsers(query, me);
|
2019-04-07 12:50:36 +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);
|
|
|
|
|
|
|
|
const users = await query.getMany();
|
|
|
|
|
|
|
|
return await Users.packMany(users, me, { detail: true });
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|