summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/notes/schedule/delete.ts
blob: 628fd899266be5375782918f5847ec1c713c129f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * SPDX-FileCopyrightText: syuilo and other misskey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import ms from 'ms';
import { Inject, Injectable } from '@nestjs/common';
import type { NoteScheduleRepository } from '@/models/_.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '@/server/api/error.js';
import { QueueService } from '@/core/QueueService.js';

export const meta = {
	tags: ['notes'],

	requireCredential: true,
	kind: 'write:notes-schedule',

	limit: {
		duration: ms('1hour'),
		max: 300,
	},

	errors: {
		noSuchNote: {
			message: 'No such note.',
			code: 'NO_SUCH_NOTE',
			id: 'a58056ba-8ba1-4323-8ebf-e0b585bc244f',
		},
		permissionDenied: {
			message: 'Permission denied.',
			code: 'PERMISSION_DENIED',
			id: 'c0da2fed-8f61-4c47-a41d-431992607b5c',
			httpStatusCode: 403,
		},
	},
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		noteId: { type: 'string', format: 'misskey:id' },
	},
	required: ['noteId'],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
	constructor(
		@Inject(DI.noteScheduleRepository)
		private noteScheduleRepository: NoteScheduleRepository,
		private queueService: QueueService,
	) {
		super(meta, paramDef, async (ps, me) => {
			const note = await this.noteScheduleRepository.findOneBy({ id: ps.noteId });
			if (note === null) {
				throw new ApiError(meta.errors.noSuchNote);
			}
			if (note.userId !== me.id) {
				throw new ApiError(meta.errors.permissionDenied);
			}
			await this.noteScheduleRepository.delete({ id: ps.noteId });
			await this.queueService.ScheduleNotePostQueue.remove(`schedNote:${ps.noteId}`);
		});
	}
}