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