2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { ClipsRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { ClipEntityService } from '@/core/entities/ClipEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-14 09:14:24 +02:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['clips'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: true,
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
kind: 'write:account',
|
|
|
|
|
2021-03-06 15:34:11 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 16:58:30 +02:00
|
|
|
ref: 'Clip',
|
|
|
|
},
|
2023-01-14 09:14:24 +02:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
tooManyClips: {
|
|
|
|
message: 'You cannot create clip any more.',
|
|
|
|
code: 'TOO_MANY_CLIPS',
|
|
|
|
id: '920f7c2d-6208-4b76-8082-e632020f5883',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string', minLength: 1, maxLength: 100 },
|
2022-04-03 07:57:26 +03:00
|
|
|
isPublic: { type: 'boolean', default: false },
|
2022-02-19 07:05:32 +02:00
|
|
|
description: { type: 'string', nullable: true, minLength: 1, maxLength: 2048 },
|
|
|
|
},
|
|
|
|
required: ['name'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 21:27:08 +03:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.clipsRepository)
|
|
|
|
private clipsRepository: ClipsRepository,
|
|
|
|
|
|
|
|
private clipEntityService: ClipEntityService,
|
2023-01-14 09:14:24 +02:00
|
|
|
private roleService: RoleService,
|
2022-09-17 21:27:08 +03:00
|
|
|
private idService: IdService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-01-14 09:14:24 +02:00
|
|
|
const currentCount = await this.clipsRepository.countBy({
|
|
|
|
userId: me.id,
|
|
|
|
});
|
2023-01-15 13:52:53 +02:00
|
|
|
if (currentCount > (await this.roleService.getUserPolicies(me.id)).clipLimit) {
|
2023-01-14 09:14:24 +02:00
|
|
|
throw new ApiError(meta.errors.tooManyClips);
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const clip = await this.clipsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: me.id,
|
|
|
|
name: ps.name,
|
|
|
|
isPublic: ps.isPublic,
|
|
|
|
description: ps.description,
|
|
|
|
}).then(x => this.clipsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
return await this.clipEntityService.pack(clip);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|