merge the functions

This commit is contained in:
MathMan05
2026-03-06 15:07:49 -06:00
committed by Rory&
parent ccbe60ffb6
commit 5e2d0fac01
4 changed files with 60 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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