summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-04-29 12:54:59 +0200
committerRory& <root@rory.gay>2026-05-03 10:50:22 +0200
commit5c5e8d820e664182b7951a4924d7d60e86f81a4a (patch)
tree79185057a8dbaa7056cf3404d6ae747f15670bed
parentClarify STORAGE_FORCE_PATH_STYLE env behavior (diff)
downloadserver-ts-5c5e8d820e664182b7951a4924d7d60e86f81a4a.tar.xz
handle constraints on webhook delete
-rw-r--r--src/api/routes/webhooks/#webhook_id/index.ts33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/api/routes/webhooks/#webhook_id/index.ts b/src/api/routes/webhooks/#webhook_id/index.ts

index 7398db42..f8f50864 100644 --- a/src/api/routes/webhooks/#webhook_id/index.ts +++ b/src/api/routes/webhooks/#webhook_id/index.ts
@@ -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({