2023-01-13 05:40:33 +01:00
|
|
|
import { Webhooks } from "@/models/index.js";
|
|
|
|
import type { Webhook } from "@/models/entities/webhook.js";
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
subscriber.on("message", async (_, data) => {
|
2022-04-02 08:28:49 +02:00
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (obj.channel === "internal") {
|
2022-04-02 08:28:49 +02:00
|
|
|
const { type, body } = obj.message;
|
|
|
|
switch (type) {
|
2023-01-13 05:40:33 +01:00
|
|
|
case "webhookCreated":
|
2022-04-02 08:28:49 +02:00
|
|
|
if (body.active) {
|
|
|
|
webhooks.push(body);
|
|
|
|
}
|
|
|
|
break;
|
2023-01-13 05:40:33 +01:00
|
|
|
case "webhookUpdated":
|
2022-04-02 08:28:49 +02:00
|
|
|
if (body.active) {
|
2023-01-13 05:40:33 +01:00
|
|
|
const i = webhooks.findIndex((a) => a.id === body.id);
|
2022-04-02 08:28:49 +02:00
|
|
|
if (i > -1) {
|
|
|
|
webhooks[i] = body;
|
|
|
|
} else {
|
|
|
|
webhooks.push(body);
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
webhooks = webhooks.filter((a) => a.id !== body.id);
|
2022-04-02 08:28:49 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-01-13 05:40:33 +01:00
|
|
|
case "webhookDeleted":
|
|
|
|
webhooks = webhooks.filter((a) => a.id !== body.id);
|
2022-04-02 08:28:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|