2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { ClipNotesRepository, ClipsRepository } from '@/models/index.js';
|
2022-09-24 07:15:16 +09:00
|
|
|
import { GetterService } from '@/server/api/GetterService.js';
|
2023-01-14 16:14:24 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2023-01-14 15:59:15 +09:00
|
|
|
import { ApiError } from '../../error.js';
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-06-05 07:16:21 +00:00
|
|
|
export default class extends Endpoint<'clips/add-note'> {
|
|
|
|
|
name = 'clips/add-note' as const;
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.clipsRepository)
|
|
|
|
|
private clipsRepository: ClipsRepository,
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.clipNotesRepository)
|
|
|
|
|
private clipNotesRepository: ClipNotesRepository,
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private idService: IdService,
|
2023-01-14 16:14:24 +09:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private getterService: GetterService,
|
|
|
|
|
) {
|
2023-06-05 07:16:21 +00:00
|
|
|
super(async (ps, me) => {
|
2022-09-18 03:27:08 +09:00
|
|
|
const clip = await this.clipsRepository.findOneBy({
|
|
|
|
|
id: ps.clipId,
|
|
|
|
|
userId: me.id,
|
|
|
|
|
});
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (clip == null) {
|
2023-06-05 07:16:21 +00:00
|
|
|
throw new ApiError(this.meta.errors.noSuchClip);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const note = await this.getterService.getNote(ps.noteId).catch(e => {
|
2023-06-05 07:16:21 +00:00
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(this.meta.errors.noSuchNote);
|
2022-09-18 03:27:08 +09:00
|
|
|
throw e;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const exist = await this.clipNotesRepository.findOneBy({
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
clipId: clip.id,
|
|
|
|
|
});
|
2020-11-15 12:04:54 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (exist != null) {
|
2023-06-05 07:16:21 +00:00
|
|
|
throw new ApiError(this.meta.errors.alreadyClipped);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
|
2023-01-14 16:14:24 +09:00
|
|
|
const currentCount = await this.clipNotesRepository.countBy({
|
|
|
|
|
clipId: clip.id,
|
|
|
|
|
});
|
2023-01-15 20:52:53 +09:00
|
|
|
if (currentCount > (await this.roleService.getUserPolicies(me.id)).noteEachClipsLimit) {
|
2023-06-05 07:16:21 +00:00
|
|
|
throw new ApiError(this.meta.errors.tooManyClipNotes);
|
2023-01-14 16:14:24 +09:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
await this.clipNotesRepository.insert({
|
|
|
|
|
id: this.idService.genId(),
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
clipId: clip.id,
|
|
|
|
|
});
|
2023-03-16 17:24:49 +09:00
|
|
|
|
|
|
|
|
await this.clipsRepository.update(clip.id, {
|
|
|
|
|
lastClippedAt: new Date(),
|
|
|
|
|
});
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|