2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { UsersRepository } from '@/models/_.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 23:20:31 +00:00
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
2023-01-08 23:58:16 +00:00
|
|
|
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
|
2023-01-12 12:02:26 +00:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2019-01-10 23:09:44 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['admin'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2019-01-10 23:09:44 +00:00
|
|
|
requireModerator: true,
|
2023-12-27 06:08:59 +00:00
|
|
|
kind: 'read:admin:show-users',
|
2023-12-18 03:32:26 +00:00
|
|
|
|
2021-03-06 13:34:11 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
nullable: false, optional: false,
|
2021-03-06 13:34:11 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
nullable: false, optional: false,
|
|
|
|
ref: 'UserDetailed',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
2022-12-26 00:22:10 +00:00
|
|
|
sort: { type: 'string', enum: ['+follower', '-follower', '+createdAt', '-createdAt', '+updatedAt', '-updatedAt', '+lastActiveDate', '-lastActiveDate'] },
|
2023-01-12 12:02:26 +00:00
|
|
|
state: { type: 'string', enum: ['all', 'alive', 'available', 'admin', 'moderator', 'adminOrModerator', 'suspended'], default: 'all' },
|
2022-07-07 10:08:18 +00:00
|
|
|
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'combined' },
|
2022-04-03 04:57:26 +00: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 05:05:32 +00:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
2023-08-17 12:20:58 +00:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2022-09-21 23:20:31 +00:00
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2023-01-12 12:02:26 +00:00
|
|
|
private roleService: RoleService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = this.usersRepository.createQueryBuilder('user');
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
switch (ps.state) {
|
|
|
|
case 'available': query.where('user.isSuspended = FALSE'); break;
|
|
|
|
case 'alive': query.where('user.updatedAt > :date', { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) }); break;
|
|
|
|
case 'suspended': query.where('user.isSuspended = TRUE'); break;
|
2023-01-12 12:02:26 +00:00
|
|
|
case 'admin': {
|
|
|
|
const adminIds = await this.roleService.getAdministratorIds();
|
|
|
|
if (adminIds.length === 0) return [];
|
|
|
|
query.where('user.id IN (:...adminIds)', { adminIds: adminIds });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'moderator': {
|
|
|
|
const moderatorIds = await this.roleService.getModeratorIds(false);
|
|
|
|
if (moderatorIds.length === 0) return [];
|
|
|
|
query.where('user.id IN (:...moderatorIds)', { moderatorIds: moderatorIds });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'adminOrModerator': {
|
|
|
|
const adminOrModeratorIds = await this.roleService.getModeratorIds();
|
|
|
|
if (adminOrModeratorIds.length === 0) return [];
|
|
|
|
query.where('user.id IN (:...adminOrModeratorIds)', { adminOrModeratorIds: adminOrModeratorIds });
|
|
|
|
break;
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2022-09-17 18:27:08 +00: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 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.username) {
|
2023-01-08 11:32:17 +00:00
|
|
|
query.andWhere('user.usernameLower like :username', { username: sqlLikeEscape(ps.username.toLowerCase()) + '%' });
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2019-07-17 09:59:10 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.hostname) {
|
|
|
|
query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() });
|
|
|
|
}
|
2019-07-17 09:59:10 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
switch (ps.sort) {
|
|
|
|
case '+follower': query.orderBy('user.followersCount', 'DESC'); break;
|
|
|
|
case '-follower': query.orderBy('user.followersCount', 'ASC'); break;
|
2023-10-16 03:00:56 +00:00
|
|
|
case '+createdAt': query.orderBy('user.id', 'DESC'); break;
|
|
|
|
case '-createdAt': query.orderBy('user.id', 'ASC'); break;
|
2022-09-17 18:27:08 +00:00
|
|
|
case '+updatedAt': query.orderBy('user.updatedAt', 'DESC', 'NULLS LAST'); break;
|
|
|
|
case '-updatedAt': query.orderBy('user.updatedAt', 'ASC', 'NULLS FIRST'); break;
|
2022-12-26 00:22:10 +00:00
|
|
|
case '+lastActiveDate': query.orderBy('user.lastActiveDate', 'DESC', 'NULLS LAST'); break;
|
|
|
|
case '-lastActiveDate': query.orderBy('user.lastActiveDate', 'ASC', 'NULLS FIRST'); break;
|
2022-09-17 18:27:08 +00:00
|
|
|
default: query.orderBy('user.id', 'ASC'); break;
|
|
|
|
}
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2023-07-08 07:53:07 +00:00
|
|
|
query.limit(ps.limit);
|
2023-07-25 10:21:50 +00:00
|
|
|
query.offset(ps.offset);
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const users = await query.getMany();
|
2019-01-10 23:09:44 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return await this.userEntityService.packMany(users, me, { detail: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|