From cee2cb6118b3505149f97deaee805df25affe5ed Mon Sep 17 00:00:00 2001 From: Rye Date: Wed, 18 Mar 2026 21:56:31 +0100 Subject: [PATCH] added fallback body to be spec compliant --- .../components/message/MsgTypeRenderers.tsx | 22 +++++++++ src/app/features/room/RoomInput.tsx | 49 ++++++++++++++----- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx index d1e2601a..93c67bba 100644 --- a/src/app/components/message/MsgTypeRenderers.tsx +++ b/src/app/components/message/MsgTypeRenderers.tsx @@ -109,6 +109,15 @@ export function MText({ edited, content, renderBody, renderUrlsPreview, style }: const forwardMeta = content['moe.sable.message.forward']; return typeof forwardMeta === 'object'; }, [content]); + + /** + * For the unwrapping of per-message profile fallbacks, we look for tags with the data-mx-profile-fallback attribute + */ + const unwrappedPerMessageProfileMessage = useMemo( + () => customBody?.replace(/]*data-mx-profile-fallback[^>]*>(.*?):\s*<\/strong>/i, ''), + [customBody] + ); + const isJumbo = useMemo(() => { if (!trimmedBody || trimmedBody.length >= 500) return false; if (!JUMBO_EMOJI_REG.test(trimmedBody)) return false; @@ -126,6 +135,19 @@ export function MText({ edited, content, renderBody, renderUrlsPreview, style }: const urlsMatch = renderUrlsPreview && trimmedBody.match(URL_REG); const urls = urlsMatch ? [...new Set(urlsMatch)] : undefined; + if (content['com.beeper.per_message_profile']) { + // unwrap per-message profile fallback if present + return ( + + {renderBody({ + body: trimmedBody, + customBody: unwrappedPerMessageProfileMessage, + })} + {edited && } + + ); + } + if (isForwarded && unwrappedForwardedContent) { return ( diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index c650d55e..44094a53 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -159,6 +159,7 @@ import { } from '$hooks/usePerMessageProfile'; import { Microphone, Stop } from '@phosphor-icons/react'; import { getSupportedAudioExtension } from '$plugins/voice-recorder-kit/supportedCodec'; +import { sanitizeCustomHtml } from '$utils/sanitize'; import { SchedulePickerDialog } from './schedule-send'; import * as css from './schedule-send/SchedulePickerDialog.css'; import { @@ -688,18 +689,6 @@ export const RoomInput = forwardRef( body, }; - /** - * the currently with the room associated per-message profile, if any, so that it can be included in the message content when sending. - * This allows the server to apply the correct profile-based transformations (e.g. font size adjustments) when processing the message, - * and also allows clients to display an accurate preview of how the message will look with the profile applied while it's being composed. - */ - const perMessageProfile = await getCurrentlyUsedPerMessageProfileForRoom(mx, roomId); - - if (perMessageProfile) { - content['com.beeper.per_message_profile'] = - convertPerMessageProfileToBeeperFormat(perMessageProfile); - } - if (replyDraft && !silentReply) { mentionData.users.add(replyDraft.userId); } @@ -710,6 +699,42 @@ export const RoomInput = forwardRef( content.format = 'org.matrix.custom.html'; content.formatted_body = formattedBody; } + + /** + * the currently with the room associated per-message profile, if any, so that it can be included in the message content when sending. + * This allows the server to apply the correct profile-based transformations (e.g. font size adjustments) when processing the message, + * and also allows clients to display an accurate preview of how the message will look with the profile applied while it's being composed. + */ + const perMessageProfile = await getCurrentlyUsedPerMessageProfileForRoom(mx, roomId); + + if (perMessageProfile) { + content['com.beeper.per_message_profile'] = + convertPerMessageProfileToBeeperFormat(perMessageProfile); + + // if a per-message profile is used, it must per spec include a fallback + const prefix = `${perMessageProfile.name}: `; + + if (!content.body.startsWith(prefix)) { + // to prevent double-prefixing when the fallback is already present + content.body = prefix + content.body; + } + + /** + * html escaped version of the display name + */ + const escapedName = sanitizeCustomHtml(perMessageProfile.name); + + const htmlPrefix = `${escapedName}: `; + + if (content.formatted_body && !content.formatted_body.startsWith(htmlPrefix)) { + content.formatted_body = htmlPrefix + content.formatted_body; + } else { + // we don't have a formatted body, but we need one + content.format = 'org.matrix.custom.html'; + content.formatted_body = `${htmlPrefix} ${plainText}`; + } + } + if (replyDraft) { content['m.relates_to'] = getReplyContent(replyDraft, room); }