なんかもうめっちゃ変えた
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import ms from 'ms';
|
||||
import create from '@/services/blocking/create.js';
|
||||
import define from '../../define.js';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||
import { UsersRepository, BlockingsRepository } from '@/models/index.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
import { UserBlockingService } from '@/core/UserBlockingService.js';
|
||||
import { DI } from '@/di-symbols.js';
|
||||
import { ApiError } from '../../error.js';
|
||||
import { getUser } from '../../common/getters.js';
|
||||
import { Blockings, NoteWatchings, Users } from '@/models/index.js';
|
||||
import { GetterService } from '../../common/GetterService.js';
|
||||
|
||||
export const meta = {
|
||||
tags: ['account'],
|
||||
@@ -53,38 +56,48 @@ export const paramDef = {
|
||||
} as const;
|
||||
|
||||
// eslint-disable-next-line import/no-default-export
|
||||
export default define(meta, paramDef, async (ps, user) => {
|
||||
const blocker = await Users.findOneByOrFail({ id: user.id });
|
||||
@Injectable()
|
||||
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
||||
constructor(
|
||||
@Inject(DI.usersRepository)
|
||||
private usersRepository: UsersRepository,
|
||||
|
||||
// 自分自身
|
||||
if (user.id === ps.userId) {
|
||||
throw new ApiError(meta.errors.blockeeIsYourself);
|
||||
@Inject(DI.blockingsRepository)
|
||||
private blockingsRepository: BlockingsRepository,
|
||||
|
||||
private userEntityService: UserEntityService,
|
||||
private getterService: GetterService,
|
||||
private userBlockingService: UserBlockingService,
|
||||
) {
|
||||
super(meta, paramDef, async (ps, me) => {
|
||||
const blocker = await this.usersRepository.findOneByOrFail({ id: me.id });
|
||||
|
||||
// 自分自身
|
||||
if (me.id === ps.userId) {
|
||||
throw new ApiError(meta.errors.blockeeIsYourself);
|
||||
}
|
||||
|
||||
// Get blockee
|
||||
const blockee = await this.getterService.getUser(ps.userId).catch(err => {
|
||||
if (err.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Check if already blocking
|
||||
const exist = await this.blockingsRepository.findOneBy({
|
||||
blockerId: blocker.id,
|
||||
blockeeId: blockee.id,
|
||||
});
|
||||
|
||||
if (exist != null) {
|
||||
throw new ApiError(meta.errors.alreadyBlocking);
|
||||
}
|
||||
|
||||
await this.userBlockingService.block(blocker, blockee);
|
||||
|
||||
return await this.userEntityService.pack(blockee.id, blocker, {
|
||||
detail: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Get blockee
|
||||
const blockee = await getUser(ps.userId).catch(e => {
|
||||
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
||||
throw e;
|
||||
});
|
||||
|
||||
// Check if already blocking
|
||||
const exist = await Blockings.findOneBy({
|
||||
blockerId: blocker.id,
|
||||
blockeeId: blockee.id,
|
||||
});
|
||||
|
||||
if (exist != null) {
|
||||
throw new ApiError(meta.errors.alreadyBlocking);
|
||||
}
|
||||
|
||||
await create(blocker, blockee);
|
||||
|
||||
NoteWatchings.delete({
|
||||
userId: blocker.id,
|
||||
noteUserId: blockee.id,
|
||||
});
|
||||
|
||||
return await Users.pack(blockee.id, blocker, {
|
||||
detail: true,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user