Skip to content

Commit

Permalink
feat: UserWebhook/SystemWebhookのテスト送信機能を追加 (#14489)
Browse files Browse the repository at this point in the history
* feat: UserWebhook/SystemWebhookのテスト送信機能を追加

* fix CHANGELOG.md

* 一部設定をパラメータから上書き出来るように修正

* remove async

* regenerate autogen
  • Loading branch information
samunohito committed Sep 19, 2024
1 parent ceb4640 commit 4ac8aad
Show file tree
Hide file tree
Showing 27 changed files with 1,477 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Unreleased

### General
-
- UserWebhookとSystemWebhookのテスト送信機能を追加 ( #14445 )

### Client
- Feat: ノート単体・ユーザーのノート・クリップのノートの埋め込み機能
Expand Down
4 changes: 4 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9477,6 +9477,10 @@ export interface Locale extends ILocale {
* Webhookを削除しますか?
*/
"deleteConfirm": string;
/**
* スイッチの右にあるボタンをクリックするとダミーのデータを使用したテスト用Webhookを送信できます。
*/
"testRemarks": string;
};
"_abuseReport": {
"_notificationRecipient": {
Expand Down
1 change: 1 addition & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,7 @@ _webhookSettings:
abuseReportResolved: "ユーザーからの通報を処理したとき"
userCreated: "ユーザーが作成されたとき"
deleteConfirm: "Webhookを削除しますか?"
testRemarks: "スイッチの右にあるボタンをクリックするとダミーのデータを使用したテスト用Webhookを送信できます。"

_abuseReport:
_notificationRecipient:
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/core/CoreModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { AbuseReportNotificationService } from '@/core/AbuseReportNotificationService.js';
import { SystemWebhookService } from '@/core/SystemWebhookService.js';
import { UserSearchService } from '@/core/UserSearchService.js';
import { WebhookTestService } from '@/core/WebhookTestService.js';
import { AccountMoveService } from './AccountMoveService.js';
import { AccountUpdateService } from './AccountUpdateService.js';
import { AiService } from './AiService.js';
Expand Down Expand Up @@ -211,6 +212,7 @@ const $UserAuthService: Provider = { provide: 'UserAuthService', useExisting: Us
const $VideoProcessingService: Provider = { provide: 'VideoProcessingService', useExisting: VideoProcessingService };
const $UserWebhookService: Provider = { provide: 'UserWebhookService', useExisting: UserWebhookService };
const $SystemWebhookService: Provider = { provide: 'SystemWebhookService', useExisting: SystemWebhookService };
const $WebhookTestService: Provider = { provide: 'WebhookTestService', useExisting: WebhookTestService };
const $UtilityService: Provider = { provide: 'UtilityService', useExisting: UtilityService };
const $FileInfoService: Provider = { provide: 'FileInfoService', useExisting: FileInfoService };
const $SearchService: Provider = { provide: 'SearchService', useExisting: SearchService };
Expand Down Expand Up @@ -359,6 +361,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
VideoProcessingService,
UserWebhookService,
SystemWebhookService,
WebhookTestService,
UtilityService,
FileInfoService,
SearchService,
Expand Down Expand Up @@ -503,6 +506,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$VideoProcessingService,
$UserWebhookService,
$SystemWebhookService,
$WebhookTestService,
$UtilityService,
$FileInfoService,
$SearchService,
Expand Down Expand Up @@ -648,6 +652,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
VideoProcessingService,
UserWebhookService,
SystemWebhookService,
WebhookTestService,
UtilityService,
FileInfoService,
SearchService,
Expand Down Expand Up @@ -791,6 +796,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$VideoProcessingService,
$UserWebhookService,
$SystemWebhookService,
$WebhookTestService,
$UtilityService,
$FileInfoService,
$SearchService,
Expand Down
22 changes: 16 additions & 6 deletions packages/backend/src/core/QueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,15 @@ export class QueueService {

/**
* @see UserWebhookDeliverJobData
* @see WebhookDeliverProcessorService
* @see UserWebhookDeliverProcessorService
*/
@bindThis
public userWebhookDeliver(webhook: MiWebhook, type: typeof webhookEventTypes[number], content: unknown) {
public userWebhookDeliver(
webhook: MiWebhook,
type: typeof webhookEventTypes[number],
content: unknown,
opts?: { attempts?: number },
) {
const data: UserWebhookDeliverJobData = {
type,
content,
Expand All @@ -468,7 +473,7 @@ export class QueueService {
};

return this.userWebhookDeliverQueue.add(webhook.id, data, {
attempts: 4,
attempts: opts?.attempts ?? 4,
backoff: {
type: 'custom',
},
Expand All @@ -479,10 +484,15 @@ export class QueueService {

/**
* @see SystemWebhookDeliverJobData
* @see WebhookDeliverProcessorService
* @see SystemWebhookDeliverProcessorService
*/
@bindThis
public systemWebhookDeliver(webhook: MiSystemWebhook, type: SystemWebhookEventType, content: unknown) {
public systemWebhookDeliver(
webhook: MiSystemWebhook,
type: SystemWebhookEventType,
content: unknown,
opts?: { attempts?: number },
) {
const data: SystemWebhookDeliverJobData = {
type,
content,
Expand All @@ -494,7 +504,7 @@ export class QueueService {
};

return this.systemWebhookDeliverQueue.add(webhook.id, data, {
attempts: 4,
attempts: opts?.attempts ?? 4,
backoff: {
type: 'custom',
},
Expand Down
13 changes: 9 additions & 4 deletions packages/backend/src/core/SystemWebhookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class SystemWebhookService implements OnApplicationShutdown {
* SystemWebhook の一覧を取得する.
*/
@bindThis
public async fetchSystemWebhooks(params?: {
public fetchSystemWebhooks(params?: {
ids?: MiSystemWebhook['id'][];
isActive?: MiSystemWebhook['isActive'];
on?: MiSystemWebhook['on'];
Expand Down Expand Up @@ -165,19 +165,24 @@ export class SystemWebhookService implements OnApplicationShutdown {
/**
* SystemWebhook をWebhook配送キューに追加する
* @see QueueService.systemWebhookDeliver
* // TODO: contentの型を厳格化する
*/
@bindThis
public async enqueueSystemWebhook(webhook: MiSystemWebhook | MiSystemWebhook['id'], type: SystemWebhookEventType, content: unknown) {
public async enqueueSystemWebhook<T extends SystemWebhookEventType>(
webhook: MiSystemWebhook | MiSystemWebhook['id'],
type: T,
content: unknown,
) {
const webhookEntity = typeof webhook === 'string'
? (await this.fetchActiveSystemWebhooks()).find(a => a.id === webhook)
: webhook;
if (!webhookEntity || !webhookEntity.isActive) {
this.logger.info(`Webhook is not active or not found : ${webhook}`);
this.logger.info(`SystemWebhook is not active or not found : ${webhook}`);
return;
}

if (!webhookEntity.on.includes(type)) {
this.logger.info(`Webhook ${webhookEntity.id} is not listening to ${type}`);
this.logger.info(`SystemWebhook ${webhookEntity.id} is not listening to ${type}`);
return;
}

Expand Down
29 changes: 27 additions & 2 deletions packages/backend/src/core/UserWebhookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import type { WebhooksRepository } from '@/models/_.js';
import type { MiWebhook } from '@/models/Webhook.js';
import { type WebhooksRepository } from '@/models/_.js';
import { MiWebhook } from '@/models/Webhook.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { GlobalEvents } from '@/core/GlobalEventService.js';
Expand Down Expand Up @@ -38,6 +38,31 @@ export class UserWebhookService implements OnApplicationShutdown {
return this.activeWebhooks;
}

/**
* UserWebhook の一覧を取得する.
*/
@bindThis
public fetchWebhooks(params?: {
ids?: MiWebhook['id'][];
isActive?: MiWebhook['active'];
on?: MiWebhook['on'];
}): Promise<MiWebhook[]> {
const query = this.webhooksRepository.createQueryBuilder('webhook');
if (params) {
if (params.ids && params.ids.length > 0) {
query.andWhere('webhook.id IN (:...ids)', { ids: params.ids });
}
if (params.isActive !== undefined) {
query.andWhere('webhook.active = :isActive', { isActive: params.isActive });
}
if (params.on && params.on.length > 0) {
query.andWhere(':on <@ webhook.on', { on: params.on });
}
}

return query.getMany();
}

@bindThis
private async onMessage(_: string, data: string): Promise<void> {
const obj = JSON.parse(data);
Expand Down
Loading

1 comment on commit 4ac8aad

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromatic detects changes. Please review the changes on Chromatic.

Please sign in to comment.