2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { UsersRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2019-01-11 08:09:44 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2019-01-11 08:09:44 +09:00
|
|
|
requireModerator: true,
|
|
|
|
|
2021-03-06 22:34:11 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
nullable: false, optional: false,
|
2021-03-06 22:34:11 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
nullable: false, optional: false,
|
|
|
|
ref: 'UserDetailed',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09: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-04-17 20:44:21 +09:00
|
|
|
state: { type: 'string', enum: ['all', 'alive', 'available', 'admin', 'moderator', 'adminOrModerator', 'silenced', 'suspended'], default: 'all' },
|
2022-07-07 19:08:18 +09:00
|
|
|
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'combined' },
|
2022-04-03 13:57:26 +09:00
|
|
|
username: { type: 'string', nullable: true, default: null },
|
|
|
|
hostname: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
default: null,
|
|
|
|
description: 'The local host is represented with `null`.',
|
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = this.usersRepository.createQueryBuilder('user');
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
switch (ps.state) {
|
|
|
|
case 'available': query.where('user.isSuspended = FALSE'); break;
|
|
|
|
case 'admin': query.where('user.isAdmin = TRUE'); break;
|
|
|
|
case 'moderator': query.where('user.isModerator = TRUE'); break;
|
|
|
|
case 'adminOrModerator': query.where('user.isAdmin = TRUE OR user.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 08:09:44 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
switch (ps.origin) {
|
|
|
|
case 'local': query.andWhere('user.host IS NULL'); break;
|
|
|
|
case 'remote': query.andWhere('user.host IS NOT NULL'); break;
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (ps.username) {
|
|
|
|
query.andWhere('user.usernameLower like :username', { username: ps.username.toLowerCase() + '%' });
|
|
|
|
}
|
2019-07-17 18:59:10 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (ps.hostname) {
|
|
|
|
query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() });
|
|
|
|
}
|
2019-07-17 18:59:10 +09:00
|
|
|
|
2022-09-18 03:27:08 +09: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', 'NULLS LAST'); break;
|
|
|
|
case '-updatedAt': query.orderBy('user.updatedAt', 'ASC', 'NULLS FIRST'); break;
|
|
|
|
default: query.orderBy('user.id', 'ASC'); break;
|
|
|
|
}
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
query.take(ps.limit);
|
|
|
|
query.skip(ps.offset);
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const users = await query.getMany();
|
2019-01-11 08:09:44 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return await this.userEntityService.packMany(users, me, { detail: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|