Webhookの作成可能数を設定可能に

This commit is contained in:
syuilo
2023-01-14 10:48:11 +09:00
parent f45059b7b1
commit bcb5182e86
6 changed files with 46 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import type { WebhooksRepository } from '@/models/index.js';
import { webhookEventTypes } from '@/models/entities/Webhook.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
import { RoleService } from '@/core/RoleService.js';
export const meta = {
tags: ['webhooks'],
@@ -12,6 +13,14 @@ export const meta = {
requireCredential: true,
kind: 'write:account',
errors: {
tooManyWebhooks: {
message: 'You cannot create webhook any more.',
code: 'TOO_MANY_WEBHOOKS',
id: '87a9bb19-111e-4e37-81d3-a3e7426453b0',
},
},
} as const;
export const paramDef = {
@@ -38,8 +47,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private idService: IdService,
private globalEventService: GlobalEventService,
private roleService: RoleService,
) {
super(meta, paramDef, async (ps, me) => {
const currentWebhooksCount = await this.webhooksRepository.countBy({
userId: me.id,
});
if (currentWebhooksCount > (await this.roleService.getUserRoleOptions(me.id)).webhookLimit) {
throw new ApiError(meta.errors.tooManyWebhooks);
}
const webhook = await this.webhooksRepository.insert({
id: this.idService.genId(),
createdAt: new Date(),