2022-04-02 08:28:49 +02:00
|
|
|
import { Webhooks } from '@/models/index.js';
|
|
|
|
import { Webhook } from '@/models/entities/webhook.js';
|
2022-08-04 10:20:48 +02:00
|
|
|
import { subscriber } from '@/db/redis.js';
|
2022-04-02 08:28:49 +02:00
|
|
|
|
|
|
|
let webhooksFetched = false;
|
|
|
|
let webhooks: Webhook[] = [];
|
|
|
|
|
|
|
|
export async function getActiveWebhooks() {
|
|
|
|
if (!webhooksFetched) {
|
|
|
|
webhooks = await Webhooks.findBy({
|
|
|
|
active: true,
|
|
|
|
});
|
|
|
|
webhooksFetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhooks;
|
|
|
|
}
|
|
|
|
|
2022-08-04 10:20:48 +02:00
|
|
|
subscriber.on('message', async (_, data) => {
|
2022-04-02 08:28:49 +02:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
|
|
|
const { type, body } = obj.message;
|
|
|
|
switch (type) {
|
|
|
|
case 'webhookCreated':
|
|
|
|
if (body.active) {
|
|
|
|
webhooks.push(body);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookUpdated':
|
|
|
|
if (body.active) {
|
|
|
|
const i = webhooks.findIndex(a => a.id === body.id);
|
|
|
|
if (i > -1) {
|
|
|
|
webhooks[i] = body;
|
|
|
|
} else {
|
|
|
|
webhooks.push(body);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
webhooks = webhooks.filter(a => a.id !== body.id);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'webhookDeleted':
|
|
|
|
webhooks = webhooks.filter(a => a.id !== body.id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|