2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import Redis from 'ioredis';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { WebhooksRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import type { Webhook } from '@/models/entities/Webhook.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-14 08:27:23 +09:00
|
|
|
import { StreamMessages } from '@/server/api/stream/types.js';
|
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class WebhookService implements OnApplicationShutdown {
|
2022-09-19 03:11:50 +09:00
|
|
|
private webhooksFetched = false;
|
|
|
|
private webhooks: Webhook[] = [];
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.redisSubscriber)
|
|
|
|
private redisSubscriber: Redis.Redis,
|
|
|
|
|
|
|
|
@Inject(DI.webhooksRepository)
|
|
|
|
private webhooksRepository: WebhooksRepository,
|
|
|
|
) {
|
2022-12-04 15:03:09 +09:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2022-09-18 03:27:08 +09:00
|
|
|
this.redisSubscriber.on('message', this.onMessage);
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async getActiveWebhooks() {
|
2022-09-19 03:11:50 +09:00
|
|
|
if (!this.webhooksFetched) {
|
|
|
|
this.webhooks = await this.webhooksRepository.findBy({
|
2022-09-18 03:27:08 +09:00
|
|
|
active: true,
|
|
|
|
});
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooksFetched = true;
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-09-19 03:11:50 +09:00
|
|
|
return this.webhooks;
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-24 07:12:11 +09:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-18 03:27:08 +09:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-01-14 08:27:23 +09:00
|
|
|
const { type, body } = obj.message as StreamMessages['internal']['payload'];
|
2022-09-18 03:27:08 +09:00
|
|
|
switch (type) {
|
|
|
|
case 'webhookCreated':
|
|
|
|
if (body.active) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooks.push(body);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookUpdated':
|
|
|
|
if (body.active) {
|
2022-09-19 03:11:50 +09:00
|
|
|
const i = this.webhooks.findIndex(a => a.id === body.id);
|
2022-09-18 03:27:08 +09:00
|
|
|
if (i > -1) {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooks[i] = body;
|
2022-09-18 03:27:08 +09:00
|
|
|
} else {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooks.push(body);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookDeleted':
|
2022-09-19 03:11:50 +09:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-18 03:27:08 +09:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public onApplicationShutdown(signal?: string | undefined) {
|
|
|
|
this.redisSubscriber.off('message', this.onMessage);
|
|
|
|
}
|
|
|
|
}
|