handle constraints on webhook delete

This commit is contained in:
Rory& committed 2026-05-03 10:50:22 +02:00
1 parent b773b0025a
commit 5c5e8d820e
1 file changed
+32 -1
+32 -1
View File
@@ -1,8 +1,21 @@
import { route } from "@spacebar/api";
import { Config, DiscordApiErrors, getPermission, Webhook, WebhooksUpdateEvent, emitEvent, Channel, handleFile, ValidateName } from "@spacebar/util";
import {
Config,
DiscordApiErrors,
getPermission,
Webhook,
WebhooksUpdateEvent,
emitEvent,
Channel,
handleFile,
ValidateName,
Message,
MessageDeleteBulkEvent,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
import { WebhookUpdateSchema } from "@spacebar/schemas";
import { In } from "typeorm";
const router = Router({ mergeParams: true });
router.get(
@@ -62,6 +75,24 @@ router.delete(
} else if (webhook.user_id != req.user_id) throw DiscordApiErrors.UNKNOWN_WEBHOOK;
const channel_id = webhook.channel_id;
const channel = await Channel.findOneOrFail({ where: { id: channel_id } });
// work around foreign key constraint
while (await Message.count({ where: { webhook_id, channel_id } })) {
const ids = (await Message.find({ where: { webhook_id, channel_id }, select: { id: true }, order: { id: "asc" }, take: 100 })).map((x) => x.id);
await Message.delete({ id: In(ids) });
await emitEvent({
event: "MESSAGE_DELETE_BULK",
channel_id,
origin: "webhook delete",
data: {
channel_id,
guild_id: channel.guild_id,
ids,
},
} satisfies MessageDeleteBulkEvent);
}
await Webhook.delete({ id: webhook_id });
await emitEvent({