Sharkey/packages/backend/src/server/api/endpoints/clips/remove-note.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-09-17 21:27:08 +03:00
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
2022-09-20 23:33:11 +03:00
import type { ClipNotesRepository, ClipsRepository } from '@/models/index.js';
2022-09-17 21:27:08 +03:00
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
2022-09-24 01:15:16 +03:00
import { GetterService } from '@/server/api/GetterService.js';
export const meta = {
tags: ['account', 'notes', 'clips'],
requireCredential: true,
kind: 'write:account',
errors: {
noSuchClip: {
message: 'No such clip.',
code: 'NO_SUCH_CLIP',
id: 'b80525c6-97f7-49d7-a42d-ebccd49cfd52',
},
noSuchNote: {
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: 'aff017de-190e-434b-893e-33a9ff5049d8',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {
clipId: { type: 'string', format: 'misskey:id' },
noteId: { type: 'string', format: 'misskey:id' },
},
required: ['clipId', 'noteId'],
} as const;
// 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,
@Inject(DI.clipNotesRepository)
private clipNotesRepository: ClipNotesRepository,
private getterService: GetterService,
) {
super(meta, paramDef, async (ps, me) => {
const clip = await this.clipsRepository.findOneBy({
id: ps.clipId,
userId: me.id,
});
2022-09-17 21:27:08 +03:00
if (clip == null) {
throw new ApiError(meta.errors.noSuchClip);
}
2022-09-17 21:27:08 +03:00
const note = await this.getterService.getNote(ps.noteId).catch(err => {
if (err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
throw err;
});
await this.clipNotesRepository.delete({
noteId: note.id,
clipId: clip.id,
});
});
}
}