2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-12 02:37:45 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 04:50:05 +00:00
|
|
|
import * as Redis from 'ioredis';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { WebhooksRepository } from '@/models/_.js';
|
2023-09-20 02:33:36 +00:00
|
|
|
import type { MiWebhook } from '@/models/Webhook.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-09-29 02:29:54 +00:00
|
|
|
import type { GlobalEvents } from '@/core/GlobalEventService.js';
|
2023-01-13 23:27:23 +00:00
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class WebhookService implements OnApplicationShutdown {
|
2022-09-18 18:11:50 +00:00
|
|
|
private webhooksFetched = false;
|
2023-08-16 08:51:28 +00:00
|
|
|
private webhooks: MiWebhook[] = [];
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
2023-04-09 08:09:27 +00:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Inject(DI.webhooksRepository)
|
|
|
|
private webhooksRepository: WebhooksRepository,
|
|
|
|
) {
|
2022-12-04 06:03:09 +00:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2023-04-09 08:09:27 +00:00
|
|
|
this.redisForSub.on('message', this.onMessage);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async getActiveWebhooks() {
|
2022-09-18 18:11:50 +00:00
|
|
|
if (!this.webhooksFetched) {
|
|
|
|
this.webhooks = await this.webhooksRepository.findBy({
|
2022-09-17 18:27:08 +00:00
|
|
|
active: true,
|
|
|
|
});
|
2022-09-18 18:11:50 +00:00
|
|
|
this.webhooksFetched = true;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-07-07 22:08:16 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
return this.webhooks;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-23 22:12:11 +00:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-17 18:27:08 +00:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-09-29 02:29:54 +00:00
|
|
|
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
|
2022-09-17 18:27:08 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'webhookCreated':
|
|
|
|
if (body.active) {
|
2024-01-22 08:44:03 +00:00
|
|
|
this.webhooks.push({ // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
|
2023-02-01 11:15:11 +00:00
|
|
|
...body,
|
2023-02-28 07:46:25 +00:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2024-01-22 08:44:03 +00:00
|
|
|
user: null, // joinなカラムは通常取ってこないので
|
2023-02-01 11:15:11 +00:00
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookUpdated':
|
|
|
|
if (body.active) {
|
2022-09-18 18:11:50 +00:00
|
|
|
const i = this.webhooks.findIndex(a => a.id === body.id);
|
2022-09-17 18:27:08 +00:00
|
|
|
if (i > -1) {
|
2024-01-22 08:44:03 +00:00
|
|
|
this.webhooks[i] = { // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
|
2023-02-01 11:15:11 +00:00
|
|
|
...body,
|
2023-02-28 07:46:25 +00:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2024-01-22 08:44:03 +00:00
|
|
|
user: null, // joinなカラムは通常取ってこないので
|
2023-02-01 11:15:11 +00:00
|
|
|
};
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
2024-01-22 08:44:03 +00:00
|
|
|
this.webhooks.push({ // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
|
2023-02-01 11:15:11 +00:00
|
|
|
...body,
|
2023-02-28 07:46:25 +00:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2024-01-22 08:44:03 +00:00
|
|
|
user: null, // joinなカラムは通常取ってこないので
|
2023-02-01 11:15:11 +00:00
|
|
|
});
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookDeleted':
|
2022-09-18 18:11:50 +00:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-17 18:27:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-05-29 04:21:26 +00:00
|
|
|
public dispose(): void {
|
2023-04-09 08:09:27 +00:00
|
|
|
this.redisForSub.off('message', this.onMessage);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
2023-05-29 04:21:26 +00:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|