summary refs log tree commit diff
diff options
context:
space:
mode:
authorMathMan05 <mathmanrm@gmail.com>2026-02-26 16:04:29 -0600
committerRory& <root@rory.gay>2026-03-07 00:58:31 +0100
commit442ddccde71f5d5d8bd9ebe98980d8b6704bbd46 (patch)
tree22867ed4af9934cfde60d5f66ff56fc99bab10e4
parentsign here (diff)
downloadserver-ts-442ddccde71f5d5d8bd9ebe98980d8b6704bbd46.tar.xz
fix many bugs
-rw-r--r--src/api/routes/channels/#channel_id/messages/index.ts4
-rw-r--r--src/api/util/handlers/Message.ts52
2 files changed, 31 insertions, 25 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts

index 151b9f20..fddadf13 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts
@@ -503,7 +503,7 @@ router.post( emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, - data: message, + data: message.toJSON(), } as MessageCreateEvent), message.guild_id ? Member.update({ id: req.user_id, guild_id: message.guild_id }, { last_message_id: message.id }) : null, ]); @@ -514,7 +514,7 @@ router.post( 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.prototype.withSignedComponents.call(Message.prototype.withSignedAttachments.call(message.toJSON(), sign), sign)); }, ); diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index 94d46998..5f8e7096 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts
@@ -49,6 +49,7 @@ import { Session, MessageFlags, FieldErrors, + Snowflake, } from "@spacebar/util"; import { HTTPError } from "lambert-server"; import { In, Or, Equal, IsNull } from "typeorm"; @@ -104,11 +105,12 @@ function checkActionRow(row: ActionRowComponent, knownComponentIds: string[], er } } } -async function processMedia(media: UnfurledMediaItem, messageId: string, batchId: string, user: User, channel: Channel, id: string) { +async function processMedia(media: UnfurledMediaItem, messageId: string, batchId: string, user: User, channel: Channel, id: string): Promise<(() => void) | void> { if (Object.keys(media).length > 1) throw new HTTPError("no, you can't send those"); if (!URL.canParse(media.url)) throw new HTTPError("media URL must be a URI"); const url = new URL(media.url); - if (!["http", "https", "attachment"].includes(url.protocol)) throw new HTTPError("invalid media protocol"); + console.log(url); + if (!["http:", "https:", "attachment:"].includes(url.protocol)) throw new HTTPError("invalid media protocol"); let attEnt: CloudAttachment; let delWhenDone = false; if (url.protocol !== "attachment") { @@ -121,11 +123,12 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId user: user, channel: channel, uploadFilename: uploadFilename, - userAttachmentId: "0", + userAttachmentId: id ?? "0", userFilename: name, userFileSize: blob.size, userIsClip: false, }); + console.log(attEnt.id); await attEnt.save(); const cdnUrl = Config.get().cdn.endpointPublic; const fetchUrl = `${cdnUrl}/attachments/${attEnt.uploadFilename}`; @@ -149,8 +152,9 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId }, }); } + const url2 = `${Config.get().cdn.endpointPrivate}/attachments/${attEnt.uploadFilename}/clone_to_message/${messageId}`; - const cloneResponse = await fetch(`${Config.get().cdn.endpointPrivate}/attachments/${attEnt.uploadFilename}/clone_to_message/${messageId}`, { + const cloneResponse = await fetch(url2, { method: "POST", headers: { signature: Config.get().security.requestSignature || "", @@ -158,16 +162,19 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId }); if (!cloneResponse.ok) { + console.log(cloneResponse, url2); console.error(`[Message] Failed to clone attachment ${attEnt.userFilename} to message ${messageId}`); throw new HTTPError("Failed to process attachment: " + (await cloneResponse.text()), 500); } const cloneRespBody = (await cloneResponse.json()) as { success: boolean; new_path: string }; + media.proxy_url = `${Config.get().cdn.endpointPublic}/${cloneRespBody.new_path}`; + if (url.protocol === "attachment:") media.url = media.proxy_url; const realAtt = Attachment.create({ filename: attEnt.userFilename, - url: `${Config.get().cdn.endpointPublic}/${cloneRespBody.new_path}`, - proxy_url: `${Config.get().cdn.endpointPublic}/${cloneRespBody.new_path}`, + url: media.url, + proxy_url: media.proxy_url, size: attEnt.size, height: attEnt.height, width: attEnt.width, @@ -177,24 +184,27 @@ async function processMedia(media: UnfurledMediaItem, messageId: string, batchId //TODO maybe this needs to be a new DB object? I don't see a reason to do this rn though, though this id *should* technically be different from the id of the attachment media.id = realAtt.id; - media.proxy_url = `${Config.get().cdn.endpointPublic}/${cloneRespBody.new_path}`; - if (url.protocol !== "attachment") media.url = media.proxy_url; + + console.log(media); + media.height = attEnt.height; media.width = attEnt.width; media.content_type = attEnt.contentType; //TODO flags? media.attachment_id = attEnt.id; //TODO preview stuff + console.log(media); if (delWhenDone) { - fetch(`${Config.get().cdn.endpointPrivate}/attachments/${attEnt.uploadFilename}`, { - headers: { - signature: Config.get().security.requestSignature, - }, - method: "DELETE", - }).then(() => { - attEnt.remove(); - }); + return () => + fetch(`${Config.get().cdn.endpointPrivate}/attachments/${attEnt.uploadFilename}`, { + headers: { + signature: Config.get().security.requestSignature, + }, + method: "DELETE", + }).then(() => { + attEnt.remove(); + }); } } export async function handleMessage(opts: MessageOptions): Promise<Message> { @@ -339,7 +349,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { rights.hasThrow("SEND_MESSAGES"); } - await Promise.all(medias.map((m, index) => processMedia(m, message.id, batchId, message.author as User, channel, index + ""))); + (await Promise.all(medias.map((m, index) => processMedia(m, message.id, batchId, message.author as User, channel, index + "")))).forEach((_) => _?.()); const ephermal = (message.flags & (1 << 6)) !== 0; if (!ephermal && channel.type === ChannelType.GUILD_PUBLIC_THREAD) { @@ -761,7 +771,7 @@ export async function postHandleMessage(message: Message) { const linkMatches = content?.match(LINK_REGEX) || []; message.clean_data(); - const data = { ...message }; + const data = { ...message.toJSON() }; const currentNormalizedUrls = new Set<string>(); for (const link of linkMatches) { @@ -814,14 +824,10 @@ export async function postHandleMessage(message: Message) { if (data.embeds != undefined) { data.embeds = data.embeds?.filter((embed) => embed.type === "rich"); } - const author = data.author?.toPublicUser(); const event = { event: "MESSAGE_UPDATE", channel_id: message.channel_id, - data: { - ...data, - author, - }, + data, } as MessageUpdateEvent; const embeds = data.embeds == undefined ? [] : data.embeds; await Promise.all([emitEvent(event), Message.update({ id: message.id, channel_id: message.channel_id }, { embeds: embeds })]);