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