From 41c779484852d3c937e0aecdb1cb98aa19edda74 Mon Sep 17 00:00:00 2001 From: Rye Date: Sun, 8 Mar 2026 20:01:33 +0100 Subject: [PATCH] fix: properly apply blockquotes so that custom html also gets forwarded --- .../message/modals/MessageForward.tsx | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/app/components/message/modals/MessageForward.tsx b/src/app/components/message/modals/MessageForward.tsx index d113cbec..84979272 100644 --- a/src/app/components/message/modals/MessageForward.tsx +++ b/src/app/components/message/modals/MessageForward.tsx @@ -153,26 +153,52 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward type SendEventContent = Parameters[3]; const eventType = mEvent.getType() as SendEventType; + const originalContent = mEvent.getContent(); + const isTextMessage = originalContent.msgtype === 'm.text'; // using reference relation to indicate that this is a forwarded message, // which allows clients to display it as such - const bodyModifText = `(Forwarded message from ${isPrivate ? 'a private room' : (getRoom(room.roomId)?.name ?? 'a room')})`; + const originalBody = typeof originalContent.body === 'string' ? originalContent.body : ''; + const originalFormattedBody = + originalContent.format === 'org.matrix.custom.html' && + typeof originalContent.formatted_body === 'string' + ? originalContent.formatted_body + : undefined; + + const bodyModifText = `(Forwarded message from ${ + isPrivate ? 'a private room' : (getRoom(room.roomId)?.name ?? 'a room') + })`; + let newBodyPlain = ''; let newBodyHtml = ''; // transform if msgtype is m.text - if (mEvent.getContent().msgtype === 'm.text') { - const original = mEvent.getContent().body; - newBodyPlain = `${bodyModifText}\n\n${original + if (isTextMessage) { + const quotedBody = originalBody .split('\n') - .map((l: string) => `> ${l}`) - .join('\n')}`; - const safeHtml = sanitizeCustomHtml(original).replace(/\n/g, '
'); + .map((line) => `> ${line}`) + .join('\n'); + + newBodyPlain = originalBody.length > 0 ? `${bodyModifText}\n\n${quotedBody}` : bodyModifText; + + const safeHtml = + originalFormattedBody !== undefined + ? sanitizeCustomHtml(originalFormattedBody) + : sanitizeCustomHtml(originalBody).replace(/\n/g, '
'); + newBodyHtml = - `
` + + `
` + `

${sanitizeCustomHtml(bodyModifText)}

` + `
${safeHtml}
` + `
`; } + // the quoted body + const forwardedTextContent = isTextMessage + ? { + body: newBodyPlain, + format: 'org.matrix.custom.html', + formatted_body: newBodyHtml, + } + : {}; let content; // handle privacy stuff if (isPrivate) { @@ -180,9 +206,7 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward // we can still include the original message content in the body of the message, so we'll just use a fallback text/plain content with the original message body content = { ...mEvent.getContent(), - body: newBodyPlain, - format: 'org.matrix.custom.html', - formatted_body: newBodyHtml, + ...forwardedTextContent, 'moe.sable.message.forward': { v: 1, is_forwarded: true, @@ -193,9 +217,7 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward } else { content = { ...mEvent.getContent(), - body: newBodyPlain, - format: 'org.matrix.custom.html', - formatted_body: newBodyHtml, + ...forwardedTextContent, 'm.relates_to': { rel_type: 'm.reference', event_id: eventId,