summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/users/show.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/api/endpoints/users/show.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/users/show.ts108
1 files changed, 62 insertions, 46 deletions
diff --git a/packages/backend/src/server/api/endpoints/users/show.ts b/packages/backend/src/server/api/endpoints/users/show.ts
index 846d83b49f..1e15025bf4 100644
--- a/packages/backend/src/server/api/endpoints/users/show.ts
+++ b/packages/backend/src/server/api/endpoints/users/show.ts
@@ -1,10 +1,14 @@
-import { FindOptionsWhere, In, IsNull } from 'typeorm';
-import { resolveUser } from '@/remote/resolve-user.js';
-import { Users } from '@/models/index.js';
-import { User } from '@/models/entities/user.js';
-import define from '../../define.js';
-import { apiLogger } from '../../logger.js';
+import { In, IsNull } from 'typeorm';
+import { Inject, Injectable } from '@nestjs/common';
+import type { UsersRepository } from '@/models/index.js';
+import type { User } from '@/models/entities/User.js';
+import { Endpoint } from '@/server/api/endpoint-base.js';
+import { UserEntityService } from '@/core/entities/UserEntityService.js';
+import { ResolveUserService } from '@/core/remote/ResolveUserService.js';
+import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
+import { ApiLoggerService } from '../../ApiLoggerService.js';
+import type { FindOptionsWhere } from 'typeorm';
export const meta = {
tags: ['users'],
@@ -78,53 +82,65 @@ export const paramDef = {
} as const;
// eslint-disable-next-line import/no-default-export
-export default define(meta, paramDef, async (ps, me) => {
- let user;
+@Injectable()
+export default class extends Endpoint<typeof meta, typeof paramDef> {
+ constructor(
+ @Inject(DI.usersRepository)
+ private usersRepository: UsersRepository,
- const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
+ private userEntityService: UserEntityService,
+ private resolveUserService: ResolveUserService,
+ private apiLoggerService: ApiLoggerService,
+ ) {
+ super(meta, paramDef, async (ps, me) => {
+ let user;
- if (ps.userIds) {
- if (ps.userIds.length === 0) {
- return [];
- }
+ const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
- const users = await Users.findBy(isAdminOrModerator ? {
- id: In(ps.userIds),
- } : {
- id: In(ps.userIds),
- isSuspended: false,
- });
+ if (ps.userIds) {
+ if (ps.userIds.length === 0) {
+ return [];
+ }
+
+ const users = await this.usersRepository.findBy(isAdminOrModerator ? {
+ id: In(ps.userIds),
+ } : {
+ id: In(ps.userIds),
+ isSuspended: false,
+ });
- // リクエストされた通りに並べ替え
- const _users: User[] = [];
- for (const id of ps.userIds) {
- _users.push(users.find(x => x.id === id)!);
- }
+ // リクエストされた通りに並べ替え
+ const _users: User[] = [];
+ for (const id of ps.userIds) {
+ _users.push(users.find(x => x.id === id)!);
+ }
- return await Promise.all(_users.map(u => Users.pack(u, me, {
- detail: true,
- })));
- } else {
- // Lookup user
- if (typeof ps.host === 'string' && typeof ps.username === 'string') {
- user = await resolveUser(ps.username, ps.host).catch(e => {
- apiLogger.warn(`failed to resolve remote user: ${e}`);
- throw new ApiError(meta.errors.failedToResolveRemoteUser);
- });
- } else {
- const q: FindOptionsWhere<User> = ps.userId != null
- ? { id: ps.userId }
- : { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
+ return await Promise.all(_users.map(u => this.userEntityService.pack(u, me, {
+ detail: true,
+ })));
+ } else {
+ // Lookup user
+ if (typeof ps.host === 'string' && typeof ps.username === 'string') {
+ user = await this.resolveUserService.resolveUser(ps.username, ps.host).catch(err => {
+ this.apiLoggerService.logger.warn(`failed to resolve remote user: ${err}`);
+ throw new ApiError(meta.errors.failedToResolveRemoteUser);
+ });
+ } else {
+ const q: FindOptionsWhere<User> = ps.userId != null
+ ? { id: ps.userId }
+ : { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
- user = await Users.findOneBy(q);
- }
+ user = await this.usersRepository.findOneBy(q);
+ }
- if (user == null || (!isAdminOrModerator && user.isSuspended)) {
- throw new ApiError(meta.errors.noSuchUser);
- }
+ if (user == null || (!isAdminOrModerator && user.isSuspended)) {
+ throw new ApiError(meta.errors.noSuchUser);
+ }
- return await Users.pack(user, me, {
- detail: true,
+ return await this.userEntityService.pack(user, me, {
+ detail: true,
+ });
+ }
});
}
-});
+}