summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/admin/system-webhook/test.ts
blob: fb2ddf4b446d76e476603c539e83fc3786b25c42 (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
68
69
70
71
72
73
74
75
76
77
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Injectable } from '@nestjs/common';
import ms from 'ms';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { WebhookTestService } from '@/core/WebhookTestService.js';
import { ApiError } from '@/server/api/error.js';
import { systemWebhookEventTypes } from '@/models/SystemWebhook.js';

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

	requireCredential: true,
	requireModerator: true,
	secure: true,
	kind: 'read:admin:system-webhook',

	limit: {
		duration: ms('15min'),
		max: 60,
	},

	errors: {
		noSuchWebhook: {
			message: 'No such webhook.',
			code: 'NO_SUCH_WEBHOOK',
			id: '0c52149c-e913-18f8-5dc7-74870bfe0cf9',
		},
	},
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		webhookId: {
			type: 'string',
			format: 'misskey:id',
		},
		type: {
			type: 'string',
			enum: systemWebhookEventTypes,
		},
		override: {
			type: 'object',
			properties: {
				url: { type: 'string', nullable: false },
				secret: { type: 'string', nullable: false },
			},
		},
	},
	required: ['webhookId', 'type'],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
	constructor(
		private webhookTestService: WebhookTestService,
	) {
		super(meta, paramDef, async (ps) => {
			try {
				await this.webhookTestService.testSystemWebhook({
					webhookId: ps.webhookId,
					type: ps.type,
					override: ps.override,
				});
			} catch (e) {
				if (e instanceof WebhookTestService.NoSuchWebhookError) {
					throw new ApiError(meta.errors.noSuchWebhook);
				}
				throw e;
			}
		});
	}
}