enhance: クリップおよびクリップ内のノートの作成可能数を設定可能に

This commit is contained in:
syuilo 2023-01-14 16:14:24 +09:00
parent 46d2a8726e
commit c2009acb2d
7 changed files with 90 additions and 0 deletions

View file

@ -4,6 +4,8 @@ import { IdService } from '@/core/IdService.js';
import type { ClipsRepository } from '@/models/index.js';
import { ClipEntityService } from '@/core/entities/ClipEntityService.js';
import { DI } from '@/di-symbols.js';
import { RoleService } from '@/core/RoleService.js';
import { ApiError } from '@/server/api/error.js';
export const meta = {
tags: ['clips'],
@ -17,6 +19,14 @@ export const meta = {
optional: false, nullable: false,
ref: 'Clip',
},
errors: {
tooManyClips: {
message: 'You cannot create clip any more.',
code: 'TOO_MANY_CLIPS',
id: '920f7c2d-6208-4b76-8082-e632020f5883',
},
},
} as const;
export const paramDef = {
@ -37,9 +47,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private clipsRepository: ClipsRepository,
private clipEntityService: ClipEntityService,
private roleService: RoleService,
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
const currentCount = await this.clipsRepository.countBy({
userId: me.id,
});
if (currentCount > (await this.roleService.getUserRoleOptions(me.id)).clipLimit) {
throw new ApiError(meta.errors.tooManyClips);
}
const clip = await this.clipsRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),