なんかもうめっちゃ変えた

This commit is contained in:
syuilo 2022-09-18 03:27:08 +09:00 committed by GitHub
parent d9ab03f086
commit b75184ec8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
946 changed files with 41219 additions and 28839 deletions

View file

@ -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 { 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: User[] = [];
for (const id of ps.userIds) {
_users.push(users.find(x => x.id === id)!);
}
const users = await this.usersRepository.findBy(isAdminOrModerator ? {
id: In(ps.userIds),
} : {
id: In(ps.userIds),
isSuspended: false,
});
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() };
// リクエストされた通りに並べ替え
const _users: User[] = [];
for (const id of ps.userIds) {
_users.push(users.find(x => x.id === id)!);
}
user = await Users.findOneBy(q);
}
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() };
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
throw new ApiError(meta.errors.noSuchUser);
}
user = await this.usersRepository.findOneBy(q);
}
return await Users.pack(user, me, {
detail: true,
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
throw new ApiError(meta.errors.noSuchUser);
}
return await this.userEntityService.pack(user, me, {
detail: true,
});
}
});
}
});
}