summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-03-04 22:00:33 +0100
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-03-04 22:00:33 +0100
commit87b38d740bb3cfbe2491c75f883a5d59ad86d52b (patch)
treea7cced08d9544906531bd7d4fa7df446d7a67bec /src
parent:bug: fix login + register (diff)
downloadserver-87b38d740bb3cfbe2491c75f883a5d59ad86d52b.tar.xz
:sparkles: [Channels] messages bulk delete
Diffstat (limited to 'src')
-rw-r--r--src/routes/api/v8/channels/#channel_id/messages/bulk-delete.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/routes/api/v8/channels/#channel_id/messages/bulk-delete.ts b/src/routes/api/v8/channels/#channel_id/messages/bulk-delete.ts
new file mode 100644
index 00000000..6ec76b62
--- /dev/null
+++ b/src/routes/api/v8/channels/#channel_id/messages/bulk-delete.ts
@@ -0,0 +1,35 @@
+import { Router } from "express";
+import { ChannelModel, getPermission, MessageDeleteBulkEvent, MessageModel } from "fosscord-server-util";
+import { HTTPError } from "lambert-server";
+import Config from "../../../../../../util/Config";
+import { emitEvent } from "../../../../../../util/Event";
+import { check } from "../../../../../../util/instanceOf";
+
+const router: Router = Router();
+
+export default router;
+// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
+// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
+router.post("/", check({ messages: [BigInt] }), async (req, res) => {
+	const channel_id = BigInt(req.params.channel_id);
+	const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
+	if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
+
+	const permission = await getPermission(req.userid, channel?.guild_id, channel_id, { channel });
+	if (!permission.has("MANAGE_MESSAGES")) throw new HTTPError("You are missing the MANAGE_MESSAGES permissions");
+
+	const { maxBulkDelete } = Config.get().limits.message;
+
+	const { messages } = req.body as { messages: bigint[] };
+	if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete");
+	if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`);
+
+	await MessageModel.deleteMany({ id: { $in: messages } }).exec();
+	await emitEvent({
+		event: "MESSAGE_DELETE_BULK",
+		channel_id,
+		data: { ids: messages, channel_id, guild_id: channel.guild_id },
+	} as MessageDeleteBulkEvent);
+
+	res.status(204).send();
+});