Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: UserWebhook/SystemWebhookのテスト送信機能を追加 #14489

Merged
merged 7 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
-
Expand Down
4 changes: 4 additions & 0 deletions locales/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9461,6 +9461,10 @@ export interface Locale extends ILocale {
* Webhookを削除しますか?
*/
"deleteConfirm": string;
/**
* スイッチの右にあるボタンをクリックするとダミーのデータを使用したテスト用Webhookを送信できます。テスト用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 @@ -2508,6 +2508,7 @@ _webhookSettings:
abuseReportResolved: "ユーザーからの通報を処理したとき"
userCreated: "ユーザーが作成されたとき"
deleteConfirm: "Webhookを削除しますか?"
testRemarks: "スイッチの右にあるボタンをクリックするとダミーのデータを使用したテスト用Webhookを送信できます。テスト用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
18 changes: 14 additions & 4 deletions packages/backend/src/core/QueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,12 @@ export class QueueService {
* @see WebhookDeliverProcessorService
*/
@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 @@ -482,7 +487,12 @@ export class QueueService {
* @see WebhookDeliverProcessorService
*/
@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
16 changes: 12 additions & 4 deletions packages/backend/src/core/SystemWebhookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,31 @@ 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,
opts?: {
attempts?: number;
},
) {
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;
}

return this.queueService.systemWebhookDeliver(webhookEntity, type, content);
return this.queueService.systemWebhookDeliver(webhookEntity, type, content, opts);
}

@bindThis
Expand Down
41 changes: 39 additions & 2 deletions packages/backend/src/core/UserWebhookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

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, WebhookEventTypes } from '@/models/Webhook.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { GlobalEvents } from '@/core/GlobalEventService.js';
import Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { QueueService } from '@/core/QueueService.js';
import type { OnApplicationShutdown } from '@nestjs/common';

@Injectable()
export class UserWebhookService implements OnApplicationShutdown {
private logger: Logger;
private activeWebhooksFetched = false;
private activeWebhooks: MiWebhook[] = [];

Expand All @@ -22,8 +26,11 @@ export class UserWebhookService implements OnApplicationShutdown {
private redisForSub: Redis.Redis,
@Inject(DI.webhooksRepository)
private webhooksRepository: WebhooksRepository,
private loggerService: LoggerService,
private queueService: QueueService,
) {
this.redisForSub.on('message', this.onMessage);
this.logger = this.loggerService.getLogger('webhook');
}

@bindThis
Expand All @@ -38,6 +45,36 @@ export class UserWebhookService implements OnApplicationShutdown {
return this.activeWebhooks;
}

/**
* UserWebhook をWebhook配送キューに追加する
* @see QueueService.systemWebhookDeliver
* // TODO: contentの型を厳格化する
*/
@bindThis
public async enqueueUserWebhook<T extends WebhookEventTypes>(
webhook: MiWebhook | MiWebhook['id'],
type: T,
content: unknown,
opts?: {
attempts?: number;
},
) {
const webhookEntity = typeof webhook === 'string'
? (await this.getActiveWebhooks()).find(a => a.id === webhook)
: webhook;
if (!webhookEntity || !webhookEntity.active) {
this.logger.debug(`UserWebhook is not active or not found : ${webhook}`);
return;
}

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

return this.queueService.userWebhookDeliver(webhookEntity, type, content, opts);
}

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