From 5e2d0fac017c711de3b6051d2c31fc61588832d0 Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Fri, 6 Mar 2026 15:07:49 -0600 Subject: [PATCH] merge the functions --- .../#channel_id/messages/#message_id/index.ts | 14 +-- .../channels/#channel_id/messages/index.ts | 13 +-- src/gateway/listener/listener.ts | 2 +- src/util/entities/Message.ts | 88 +++++++++---------- 4 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/api/routes/channels/#channel_id/messages/#message_id/index.ts b/src/api/routes/channels/#channel_id/messages/#message_id/index.ts index 54ba6b4e6..973e20e2e 100644 --- a/src/api/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/src/api/routes/channels/#channel_id/messages/#message_id/index.ts @@ -233,11 +233,15 @@ router.put( // no await as it shouldnt block the message send function and silently catch error postHandleMessage(message).catch((e) => console.error("[Message] post-message handler failed", e)); - const sign = new NewUrlUserSignatureData({ - ip: req.ip, - userAgent: req.headers["user-agent"] as string, - }); - return res.json(Message.prototype.withSignedComponents.call(message.withSignedAttachments(sign), sign)); + + return res.json( + message.withSignedAttachments( + new NewUrlUserSignatureData({ + ip: req.ip, + userAgent: req.headers["user-agent"] as string, + }), + ), + ); }, ); diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index bf4803aad..bd0f32ec9 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -506,11 +506,14 @@ router.post( // no await as it shouldnt block the message send function and silently catch error postHandleMessage(message).catch((e) => console.error("[Message] post-message handler failed", e)); - const sign = new NewUrlUserSignatureData({ - ip: req.ip, - userAgent: req.headers["user-agent"] as string, - }); - return res.json(Message.prototype.withSignedComponents.call(Message.prototype.withSignedAttachments.call(message.toJSON(), sign), sign)); + return res.json( + message.withSignedAttachments( + new NewUrlUserSignatureData({ + ip: req.ip, + userAgent: req.headers["user-agent"] as string, + }), + ), + ); }, ); diff --git a/src/gateway/listener/listener.ts b/src/gateway/listener/listener.ts index 6b261ccc4..149b0edb7 100644 --- a/src/gateway/listener/listener.ts +++ b/src/gateway/listener/listener.ts @@ -353,7 +353,7 @@ async function consume(this: WebSocket, opts: EventOpts) { }), ).attachments; if (data["components"]) { - data["components"] = Message.prototype.withSignedComponents.call( + data["components"] = Message.prototype.withSignedAttachments.call( data, new NewUrlUserSignatureData({ ip: this.ipAddress, diff --git a/src/util/entities/Message.ts b/src/util/entities/Message.ts index 42f79ee4d..735402f23 100644 --- a/src/util/entities/Message.ts +++ b/src/util/entities/Message.ts @@ -306,58 +306,54 @@ export class Message extends BaseClass { } withSignedAttachments(data: NewUrlUserSignatureData) { - return { - ...this, - attachments: this.attachments?.map((attachment: Attachment) => Attachment.prototype.signUrls.call(attachment, data)), - }; - } - withSignedComponents(data: NewUrlUserSignatureData) { - if (!this.components || !(this.flags & Number(MessageFlags.FLAGS.IS_COMPONENTS_V2))) return { ...this }; function signMedia(media: UnfurledMediaItem) { Object.assign(media, Attachment.prototype.signUrls.call(media, data)); } return { ...this, - components: this.components.map((comp) => { - comp = structuredClone(comp); - if (comp.type === MessageComponentType.Section) { - const accessory = comp.accessory; - if (accessory.type === MessageComponentType.Thumbnail) { - signMedia(accessory.media); - } - } else if (comp.type === MessageComponentType.MediaGallery) { - comp.items.forEach(({ media }) => signMedia(media)); - } else if (comp.type === MessageComponentType.File) { - signMedia(comp.file); - } else if (comp.type === MessageComponentType.Container) { - for (const elm of comp.components) { - switch (elm.type) { - case MessageComponentType.Separator: - case MessageComponentType.TextDisplay: - case MessageComponentType.ActionRow: - break; - case MessageComponentType.Section: { - const accessory = elm.accessory; - if (accessory.type === MessageComponentType.Thumbnail) { - signMedia(accessory.media); - } - break; - } - case MessageComponentType.MediaGallery: - elm.items.forEach(({ media }) => signMedia(media)); - break; - case MessageComponentType.File: { - signMedia(elm.file); - break; - } + attachments: this.attachments?.map((attachment: Attachment) => Attachment.prototype.signUrls.call(attachment, data)), + components: this.components + ? this.components.map((comp) => { + comp = structuredClone(comp); + if (comp.type === MessageComponentType.Section) { + const accessory = comp.accessory; + if (accessory.type === MessageComponentType.Thumbnail) { + signMedia(accessory.media); + } + } else if (comp.type === MessageComponentType.MediaGallery) { + comp.items.forEach(({ media }) => signMedia(media)); + } else if (comp.type === MessageComponentType.File) { + signMedia(comp.file); + } else if (comp.type === MessageComponentType.Container) { + for (const elm of comp.components) { + switch (elm.type) { + case MessageComponentType.Separator: + case MessageComponentType.TextDisplay: + case MessageComponentType.ActionRow: + break; + case MessageComponentType.Section: { + const accessory = elm.accessory; + if (accessory.type === MessageComponentType.Thumbnail) { + signMedia(accessory.media); + } + break; + } + case MessageComponentType.MediaGallery: + elm.items.forEach(({ media }) => signMedia(media)); + break; + case MessageComponentType.File: { + signMedia(elm.file); + break; + } - default: - elm satisfies never; - } - } - } - return comp; - }), + default: + elm satisfies never; + } + } + } + return comp; + }) + : this.components, }; }