summary refs log tree commit diff
path: root/api/src/routes
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-20 21:27:08 +0200
committerGitHub <noreply@github.com>2021-09-20 21:27:08 +0200
commit8af0094d8e8c46176cfbfac62e0b112f00c8e4be (patch)
tree0642670af9ad961d428d114ae9f24fa6a32626e6 /api/src/routes
parentMerge pull request #386 from ChrisChrome/master (diff)
parent:bug: fix .delete -> add onDelete: "CASCADE" (diff)
downloadserver-8af0094d8e8c46176cfbfac62e0b112f00c8e4be.tar.xz
Merge pull request #382 from fosscord/auto-delete-relations
Auto delete relations
Diffstat (limited to 'api/src/routes')
-rw-r--r--api/src/routes/channels/#channel_id/index.ts20
-rw-r--r--api/src/routes/channels/#channel_id/messages/#message_id/index.ts3
-rw-r--r--api/src/routes/guilds/#guild_id/delete.ts12
3 files changed, 15 insertions, 20 deletions
diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts
index fde75ec9..61c851e8 100644
--- a/api/src/routes/channels/#channel_id/index.ts
+++ b/api/src/routes/channels/#channel_id/index.ts
@@ -1,6 +1,15 @@
-import { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util";
+import {
+	Channel,
+	ChannelDeleteEvent,
+	ChannelPermissionOverwriteType,
+	ChannelType,
+	ChannelUpdateEvent,
+	emitEvent,
+	Recipient,
+	handleFile
+} from "@fosscord/util";
 import { Request, Response, Router } from "express";
-import { handleFile, route } from "@fosscord/api";
+import { route } from "@fosscord/api";
 
 const router: Router = Router();
 // TODO: delete channel
@@ -20,15 +29,14 @@ router.delete("/", route({ permission: "MANAGE_CHANNELS" }), async (req: Request
 	const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients"] });
 
 	if (channel.type === ChannelType.DM) {
-		const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } })
-		recipient.closed = true
+		const recipient = await Recipient.findOneOrFail({ where: { channel_id: channel_id, user_id: req.user_id } });
+		recipient.closed = true;
 		await Promise.all([
 			recipient.save(),
 			emitEvent({ event: "CHANNEL_DELETE", data: channel, user_id: req.user_id } as ChannelDeleteEvent)
 		]);
-
 	} else if (channel.type === ChannelType.GROUP_DM) {
-		await Channel.removeRecipientFromChannel(channel, req.user_id)
+		await Channel.removeRecipientFromChannel(channel, req.user_id);
 	} else {
 		await Promise.all([
 			Channel.delete({ id: channel_id }),
diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts
index b5220fab..7f7de264 100644
--- a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts
+++ b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts
@@ -3,7 +3,6 @@ import { Router, Response, Request } from "express";
 import { route } from "@fosscord/api";
 import { handleMessage, postHandleMessage } from "@fosscord/api";
 import { MessageCreateSchema } from "../index";
-import { deleteMessageAttachments } from "@fosscord/api/util/Attachments";
 
 const router = Router();
 // TODO: message content/embed string length limit
@@ -34,7 +33,6 @@ router.patch("/", route({ body: "MessageCreateSchema", permission: "SEND_MESSAGE
 	});
 
 	await Promise.all([
-		await deleteMessageAttachments(message_id, new_message.attachments), //This delete all the attachments not in the array
 		new_message!.save(),
 		await emitEvent({
 			event: "MESSAGE_UPDATE",
@@ -60,7 +58,6 @@ router.delete("/", route({}), async (req: Request, res: Response) => {
 		permission.hasThrow("MANAGE_MESSAGES");
 	}
 
-	await deleteMessageAttachments(message_id);
 	await Message.delete({ id: message_id });
 
 	await emitEvent({
diff --git a/api/src/routes/guilds/#guild_id/delete.ts b/api/src/routes/guilds/#guild_id/delete.ts
index 7c3c5530..bd158c56 100644
--- a/api/src/routes/guilds/#guild_id/delete.ts
+++ b/api/src/routes/guilds/#guild_id/delete.ts
@@ -13,15 +13,8 @@ router.post("/", route({}), async (req: Request, res: Response) => {
 	const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] });
 	if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
 
-	// do not put everything into promise all, because of "QueryFailedError: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed"
-
-	await Message.delete({ guild_id }); // messages must be deleted before channel
-
 	await Promise.all([
-		Role.delete({ guild_id }),
-		Channel.delete({ guild_id }),
-		Emoji.delete({ guild_id }),
-		Member.delete({ guild_id }),
+		Guild.delete({ id: guild_id }), // this will also delete all guild related data
 		emitEvent({
 			event: "GUILD_DELETE",
 			data: {
@@ -31,9 +24,6 @@ router.post("/", route({}), async (req: Request, res: Response) => {
 		} as GuildDeleteEvent)
 	]);
 
-	await Invite.delete({ guild_id }); // invite must be deleted after channel
-	await Guild.delete({ id: guild_id }); // guild must be deleted after everything else
-
 	return res.sendStatus(204);
 });