Sharkey/packages/backend/src/server/api/endpoints/i/webhooks/update.ts

82 lines
2.1 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
2022-09-18 03:27:08 +09:00
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { WebhooksRepository } from '@/models/_.js';
import { webhookEventTypes } from '@/models/Webhook.js';
2022-09-18 03:27:08 +09:00
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../../error.js';
export const meta = {
tags: ['webhooks'],
requireCredential: true,
kind: 'write:account',
errors: {
noSuchWebhook: {
message: 'No such webhook.',
code: 'NO_SUCH_WEBHOOK',
id: 'fb0fea69-da18-45b1-828d-bd4fd1612518',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {
webhookId: { type: 'string', format: 'misskey:id' },
name: { type: 'string', minLength: 1, maxLength: 100 },
url: { type: 'string', minLength: 1, maxLength: 1024 },
secret: { type: 'string', nullable: true, maxLength: 1024 },
on: { type: 'array', items: {
type: 'string', enum: webhookEventTypes,
} },
active: { type: 'boolean' },
},
required: ['webhookId'],
} as const;
2022-09-18 03:27:08 +09:00
// TODO: ロジックをサービスに切り出す
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
2022-09-18 03:27:08 +09:00
constructor(
@Inject(DI.webhooksRepository)
private webhooksRepository: WebhooksRepository,
private globalEventService: GlobalEventService,
) {
super(meta, paramDef, async (ps, me) => {
const webhook = await this.webhooksRepository.findOneBy({
id: ps.webhookId,
userId: me.id,
});
2022-09-18 03:27:08 +09:00
if (webhook == null) {
throw new ApiError(meta.errors.noSuchWebhook);
}
2022-09-18 03:27:08 +09:00
await this.webhooksRepository.update(webhook.id, {
name: ps.name,
url: ps.url,
secret: ps.secret === null ? '' : ps.secret,
2022-09-18 03:27:08 +09:00
on: ps.on,
active: ps.active,
});
2023-01-13 17:42:12 +09:00
const updated = await this.webhooksRepository.findOneByOrFail({
id: ps.webhookId,
});
this.globalEventService.publishInternalEvent('webhookUpdated', updated);
2022-09-18 03:27:08 +09:00
});
}
}