iceshrimp-legacy/packages/backend/src/misc/webhook-cache.ts

50 lines
1 KiB
TypeScript
Raw Normal View History

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";
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) => {
const obj = JSON.parse(data);
2023-01-13 05:40:33 +01:00
if (obj.channel === "internal") {
const { type, body } = obj.message;
switch (type) {
2023-01-13 05:40:33 +01:00
case "webhookCreated":
if (body.active) {
webhooks.push(body);
}
break;
2023-01-13 05:40:33 +01:00
case "webhookUpdated":
if (body.active) {
2023-01-13 05:40:33 +01:00
const i = webhooks.findIndex((a) => a.id === body.id);
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);
}
break;
2023-01-13 05:40:33 +01:00
case "webhookDeleted":
webhooks = webhooks.filter((a) => a.id !== body.id);
break;
default:
break;
}
}
});