mirror of
https://github.com/spacebarchat/server.git
synced 2026-10-23 00:03:05 +00:00
handle constraints on webhook delete
This commit is contained in:
1 parent
b773b0025a
commit
5c5e8d820e
1 file changed
+32
-1
@@ -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({
|
||||
|
||||
Reference in new issue
Block a user