summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-19 18:46:22 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-19 18:46:22 +0200
commit313696d237cc614cca8f68d8a63ad3039d00baf9 (patch)
treeffee7dd8d0bcfd4fda2ad5c14238003d49b74f68
parent:bug: fix bundle debug (diff)
downloadserver-313696d237cc614cca8f68d8a63ad3039d00baf9.tar.xz
:art: remove deleteMessageAttachments and move to entity
-rw-r--r--api/src/routes/channels/#channel_id/messages/#message_id/index.ts3
-rw-r--r--api/src/util/Attachments.ts12
-rw-r--r--api/src/util/index.ts1
-rw-r--r--util/src/entities/Attachment.ts9
4 files changed, 8 insertions, 17 deletions
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/util/Attachments.ts b/api/src/util/Attachments.ts
deleted file mode 100644
index addda97f..00000000
--- a/api/src/util/Attachments.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { Attachment } from "@fosscord/util";
-import { deleteFile } from "@fosscord/api";
-import { URL } from "url";
-
-export async function deleteMessageAttachments(messageId: string, keep?: Attachment[]) {
-	let attachments = await Attachment.find({ message_id: messageId });
-	if (keep)
-		attachments = attachments.filter(x => !keep.map(k => k.id).includes(x.id));
-	await Promise.all(attachments.map(a => a.remove()));
-
-	attachments.forEach(a => deleteFile((new URL(a.url)).pathname)); //We don't need to await since this is done on the cdn
-}
diff --git a/api/src/util/index.ts b/api/src/util/index.ts
index 4b1e8e77..3e47ce4e 100644
--- a/api/src/util/index.ts
+++ b/api/src/util/index.ts
@@ -1,5 +1,4 @@
 export * from "./Base64";
-export * from "./cdn";
 export * from "./FieldError";
 export * from "./ipAddress";
 export * from "./Message";
diff --git a/util/src/entities/Attachment.ts b/util/src/entities/Attachment.ts
index ca893400..82c5ecf5 100644
--- a/util/src/entities/Attachment.ts
+++ b/util/src/entities/Attachment.ts
@@ -1,4 +1,6 @@
-import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
+import { BeforeRemove, Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
+import { URL } from "url";
+import { deleteFile } from "../util/cdn";
 import { BaseClass } from "./BaseClass";
 
 @Entity("attachments")
@@ -31,4 +33,9 @@ export class Attachment extends BaseClass {
 	@JoinColumn({ name: "message_id" })
 	@ManyToOne(() => require("./Message").Message, (message: import("./Message").Message) => message.attachments)
 	message: import("./Message").Message;
+
+	@BeforeRemove()
+	onDelete() {
+		return deleteFile(new URL(this.url).pathname);
+	}
 }