From c85cd9f5ec56e80ccba97ec0e980eb398c26b56f Mon Sep 17 00:00:00 2001 From: Rye Date: Sat, 7 Mar 2026 20:36:36 +0100 Subject: [PATCH] add unwrapping of forwarded message content and enhance MessageForwardItem styling --- .../components/message/MsgTypeRenderers.tsx | 22 ++++ .../message/modals/MessageForward.tsx | 103 ++++++++++++------ src/app/features/room/message/Message.tsx | 2 +- 3 files changed, 95 insertions(+), 32 deletions(-) diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index dc738c0a..8aacde5f 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -31,6 +31,7 @@ import { MessageUnsupportedContent, } from './content'; import { MessageTextBody } from './layout'; +import { unwrapForwardedContent } from './modals/MessageForward'; export function MBadEncrypted() { return ( @@ -86,6 +87,10 @@ export function MText({ edited, content, renderBody, renderUrlsPreview, style }: typeof content.formatted_body === 'string' ? content.formatted_body : undefined; const trimmedBody = useMemo(() => trimReplyFromBody(body), [body]); + const unwrappedForwardedContent = useMemo( + () => unwrapForwardedContent(customBody ?? body), + [customBody, body] + ); const safeCustomBody = useMemo(() => { if (!customBody) return undefined; @@ -96,6 +101,10 @@ export function MText({ edited, content, renderBody, renderUrlsPreview, style }: return customBody; }, [customBody]); + const isForwarded = useMemo(() => { + const forwardMeta = content['moe.sable.message.forward']; + return typeof forwardMeta === 'object'; + }, [content]); const isJumbo = useMemo(() => { if (!trimmedBody || trimmedBody.length >= 500) return false; if (!JUMBO_EMOJI_REG.test(trimmedBody)) return false; @@ -113,6 +122,19 @@ export function MText({ edited, content, renderBody, renderUrlsPreview, style }: const urlsMatch = renderUrlsPreview && trimmedBody.match(URL_REG); const urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + if (isForwarded && unwrappedForwardedContent) { + return ( + + {renderBody({ + body: trimmedBody, + customBody: unwrappedForwardedContent, + })} + {edited && } + {renderUrlsPreview && urls && urls.length > 0 && renderUrlsPreview(urls)} + + ); + } + return ( <> void; -}) { - const setModal = useSetAtom(modalAtom); +export const MessageForwardItem = as<'button', MessageForwardItemProps>( + ({ room, mEvent, onClose, ...props }: MessageForwardItemProps) => { + const setModal = useSetAtom(modalAtom); - const handleClick = () => { - setModal({ - type: ModalType.Forward, - room, - mEvent, - }); - onClose(); - }; + const handleClick = () => { + setModal({ + type: ModalType.Forward, + room, + mEvent, + }); + onClose?.(); + }; - return ( - } - radii="300" - onClick={handleClick} - > - - Forward - - - ); -} + return ( + } + radii="300" + {...props} + onClick={handleClick} + > + + Forward + + + ); + } +); + +export const unwrapForwardedContent = (content: string) => { + // unwrap the content of a forwarded message if it was wrapped in a blockquote with the data-forward-marker attribute + const parser = new DOMParser(); + const doc = parser.parseFromString(content, 'text/html'); + const forwardMarker = doc.querySelector('[data-forward-marker]'); + if (forwardMarker) { + const blockquote = forwardMarker.querySelector('blockquote'); + if (blockquote) { + return blockquote.innerHTML; + } + } + return content; +}; type MessageForwardInternalProps = { room: Room; @@ -125,6 +137,23 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward // 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')} - originally sent at ${new Date(mEvent.getTs()).toLocaleString()} )`; + 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 + .split('\n') + .map((l: string) => `> ${l}`) + .join('\n')}`; + const safeHtml = sanitizeCustomHtml(original).replace(/\n/g, '
'); + newBodyHtml = + `
` + + `

${sanitizeCustomHtml(bodyModifText)}

` + + `
${safeHtml}
` + + `
`; + } let content; // handle privacy stuff if (isPrivate) { @@ -132,6 +161,9 @@ 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, 'moe.sable.message.forward': { v: 1, is_forwarded: true, @@ -142,6 +174,9 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward } else { content = { ...mEvent.getContent(), + body: newBodyPlain, + format: 'org.matrix.custom.html', + formatted_body: newBodyHtml, 'm.relates_to': { rel_type: 'm.reference', event_id: eventId, @@ -223,3 +258,9 @@ export function MessageForwardInternal({ room, mEvent, onClose }: MessageForward ); } + +type MessageForwardItemProps = { + room: Room; + mEvent: MatrixEvent; + onClose?: () => void; +}; diff --git a/src/app/features/room/message/Message.tsx b/src/app/features/room/message/Message.tsx index 2d38bacb..41a51d5c 100644 --- a/src/app/features/room/message/Message.tsx +++ b/src/app/features/room/message/Message.tsx @@ -539,7 +539,7 @@ function MessageInternal( compact={messageLayout === MessageLayout.Compact} hour24Clock={hour24Clock} dateFormatString={dateFormatString} - style={{ marginLeft: config.space.S100 }} + style={{ marginLeft: config.space.S100, justifyContent: 'flex-end' }} />