Sharkey/packages/backend/src/server/api/endpoints/clips/unfavorite.ts

66 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-03-16 10:24:49 +02:00
import { Inject, Injectable } from '@nestjs/common';
import type { ClipsRepository, ClipFavoritesRepository } from '@/models/index.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
export const meta = {
tags: ['clip'],
requireCredential: true,
kind: 'write:clip-favorite',
errors: {
noSuchClip: {
message: 'No such clip.',
code: 'NO_SUCH_CLIP',
id: '2603966e-b865-426c-94a7-af4a01241dc1',
},
notFavorited: {
message: 'You have not favorited the clip.',
code: 'NOT_FAVORITED',
id: '90c3a9e8-b321-4dae-bf57-2bf79bbcc187',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {
clipId: { type: 'string', format: 'misskey:id' },
},
required: ['clipId'],
} as const;
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.clipsRepository)
private clipsRepository: ClipsRepository,
@Inject(DI.clipFavoritesRepository)
private clipFavoritesRepository: ClipFavoritesRepository,
) {
super(meta, paramDef, async (ps, me) => {
const clip = await this.clipsRepository.findOneBy({ id: ps.clipId });
if (clip == null) {
throw new ApiError(meta.errors.noSuchClip);
}
const exist = await this.clipFavoritesRepository.findOneBy({
clipId: clip.id,
userId: me.id,
});
if (exist == null) {
throw new ApiError(meta.errors.notFavorited);
}
await this.clipFavoritesRepository.delete(exist.id);
});
}
}