2023-07-27 08:31:52 +03:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-14 07:50:05 +03:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { WebhooksRepository } from '@/models/index.js';
|
2023-08-16 11:51:28 +03:00
|
|
|
import type { MiWebhook } from '@/models/entities/Webhook.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 08:03:09 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-14 01:27:23 +02:00
|
|
|
import { StreamMessages } from '@/server/api/stream/types.js';
|
|
|
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class WebhookService implements OnApplicationShutdown {
|
2022-09-18 21:11:50 +03:00
|
|
|
private webhooksFetched = false;
|
2023-08-16 11:51:28 +03:00
|
|
|
private webhooks: MiWebhook[] = [];
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
constructor(
|
2023-04-09 11:09:27 +03:00
|
|
|
@Inject(DI.redisForSub)
|
|
|
|
private redisForSub: Redis.Redis,
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Inject(DI.webhooksRepository)
|
|
|
|
private webhooksRepository: WebhooksRepository,
|
|
|
|
) {
|
2022-12-04 08:03:09 +02:00
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
2023-04-09 11:09:27 +03:00
|
|
|
this.redisForSub.on('message', this.onMessage);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-17 21:27:08 +03:00
|
|
|
public async getActiveWebhooks() {
|
2022-09-18 21:11:50 +03:00
|
|
|
if (!this.webhooksFetched) {
|
|
|
|
this.webhooks = await this.webhooksRepository.findBy({
|
2022-09-17 21:27:08 +03:00
|
|
|
active: true,
|
|
|
|
});
|
2022-09-18 21:11:50 +03:00
|
|
|
this.webhooksFetched = true;
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
2023-07-08 01:08:16 +03:00
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
return this.webhooks;
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-24 01:12:11 +03:00
|
|
|
private async onMessage(_: string, data: string): Promise<void> {
|
2022-09-17 21:27:08 +03:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
2023-01-14 01:27:23 +02:00
|
|
|
const { type, body } = obj.message as StreamMessages['internal']['payload'];
|
2022-09-17 21:27:08 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'webhookCreated':
|
|
|
|
if (body.active) {
|
2023-02-01 13:15:11 +02:00
|
|
|
this.webhooks.push({
|
|
|
|
...body,
|
|
|
|
createdAt: new Date(body.createdAt),
|
2023-02-28 09:46:25 +02:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2023-02-01 13:15:11 +02:00
|
|
|
});
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookUpdated':
|
|
|
|
if (body.active) {
|
2022-09-18 21:11:50 +03:00
|
|
|
const i = this.webhooks.findIndex(a => a.id === body.id);
|
2022-09-17 21:27:08 +03:00
|
|
|
if (i > -1) {
|
2023-02-01 13:15:11 +02:00
|
|
|
this.webhooks[i] = {
|
|
|
|
...body,
|
|
|
|
createdAt: new Date(body.createdAt),
|
2023-02-28 09:46:25 +02:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2023-02-01 13:15:11 +02:00
|
|
|
};
|
2022-09-17 21:27:08 +03:00
|
|
|
} else {
|
2023-02-01 13:15:11 +02:00
|
|
|
this.webhooks.push({
|
|
|
|
...body,
|
|
|
|
createdAt: new Date(body.createdAt),
|
2023-02-28 09:46:25 +02:00
|
|
|
latestSentAt: body.latestSentAt ? new Date(body.latestSentAt) : null,
|
2023-02-01 13:15:11 +02:00
|
|
|
});
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-18 21:11:50 +03:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookDeleted':
|
2022-09-18 21:11:50 +03:00
|
|
|
this.webhooks = this.webhooks.filter(a => a.id !== body.id);
|
2022-09-17 21:27:08 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2023-05-29 07:21:26 +03:00
|
|
|
public dispose(): void {
|
2023-04-09 11:09:27 +03:00
|
|
|
this.redisForSub.off('message', this.onMessage);
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
2023-05-29 07:21:26 +03:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|