1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 12:42:44 +01:00

🎨 remove deleteMessageAttachments and move to entity

This commit is contained in:
Flam3rboy 2021-09-19 18:46:22 +02:00
parent d45ea78ae6
commit 313696d237
4 changed files with 8 additions and 17 deletions

View File

@ -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({

View File

@ -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
}

View File

@ -1,5 +1,4 @@
export * from "./Base64";
export * from "./cdn";
export * from "./FieldError";
export * from "./ipAddress";
export * from "./Message";

View File

@ -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);
}
}