From 8b1e7c058e66f357dffbc9e499bdacfbf9e1e04c Mon Sep 17 00:00:00 2001 From: Josie Date: Mon, 3 Aug 2026 23:13:12 +0100 Subject: [PATCH] Fix pmp /me rendering --- src/app/components/RenderMessageContent.tsx | 27 ++++++++++++++++--- .../components/message/MsgTypeRenderers.tsx | 14 +++++++--- src/app/components/message/Reply.tsx | 7 +++-- src/app/features/room/RoomInput.tsx | 3 ++- .../features/room/buildReplacementContent.ts | 12 +++++---- .../features/room/message/MessageEditor.tsx | 6 ++--- src/app/hooks/usePerMessageProfile.ts | 17 ++++++++++++ 7 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/app/components/RenderMessageContent.tsx b/src/app/components/RenderMessageContent.tsx index e62b96713..175215164 100644 --- a/src/app/components/RenderMessageContent.tsx +++ b/src/app/components/RenderMessageContent.tsx @@ -54,6 +54,12 @@ import { PollEvent } from './message/PollEvent'; import { M_POLL_START, M_TEXT } from 'matrix-js-sdk'; import type { IImageInfo, IGalleryContent } from '$types/matrix/common'; import { GALLERY_MSGTYPE } from '$types/matrix/common'; +import { + convertBeeperFormatToOurPerMessageProfile, + type PerMessageProfileBeeperFormat, + stripPerMessageProfileFormattedBody, + stripPerMessageProfilePlainBody, +} from '$hooks/usePerMessageProfile'; type RenderMessageContentProps = { displayName: string; @@ -209,7 +215,7 @@ function RenderMessageContentInternal({ ), [urlPreview] ); - const messageUrlsPreview = urlPreview ? renderUrlsPreview : undefined; + const messageUrlsPreview = urlPreview || themeChatSableWidgets ? renderUrlsPreview : undefined; const messageBundlePreview = bundledPreview ? renderBundledPreviews : undefined; const renderCaption = () => { @@ -339,6 +345,21 @@ function RenderMessageContentInternal({ } if (msgType === (MsgType.Emote as string)) { + const beeperProfile = content['com.beeper.per_message_profile'] as + | PerMessageProfileBeeperFormat + | undefined; + const pmp = beeperProfile + ? convertBeeperFormatToOurPerMessageProfile(beeperProfile) + : undefined; + + const strippedContent = pmp + ? { + ...content, + formatted_body: stripPerMessageProfileFormattedBody(content['formatted_body'] as string), + body: stripPerMessageProfilePlainBody(content['body'] as string), + } + : content; + if ((content as { 'fyi.cisnt.headpat'?: boolean })['fyi.cisnt.headpat']) { return ( - cleanedMessage?.replace(/]*data-mx-profile-fallback[^>]*>(.*?):\s*<\/strong>/i, ''), + () => (cleanedMessage ? stripPerMessageProfileFormattedBody(cleanedMessage) : undefined), [cleanedMessage] ); // the plain body, with PMP fallback removed const unwrappedPmpBody = useMemo( - () => (hadPerMessageProfileFallback ? trimmedBody?.replace(/(.*?):/i, '') : trimmedBody), + () => + hadPerMessageProfileFallback + ? stripPerMessageProfilePlainBody(trimmedBody ?? '') + : trimmedBody, [trimmedBody, hadPerMessageProfileFallback] ); diff --git a/src/app/components/message/Reply.tsx b/src/app/components/message/Reply.tsx index 7939ef854..bc3b0397f 100644 --- a/src/app/components/message/Reply.tsx +++ b/src/app/components/message/Reply.tsx @@ -63,7 +63,10 @@ import { LinePlaceholder } from './placeholder'; import { ReactionKeyInline } from './ReactionKeyInline'; import { M_POLL_START, M_TEXT } from 'matrix-js-sdk'; import type { PerMessageProfileBeeperFormat } from '$hooks/usePerMessageProfile'; -import { convertBeeperFormatToOurPerMessageProfile } from '$hooks/usePerMessageProfile'; +import { + convertBeeperFormatToOurPerMessageProfile, + stripPerMessageProfileFormattedBody, +} from '$hooks/usePerMessageProfile'; const ROOM_REPLY_TIMELINE_EVENT_TYPES = new Set([ EventType.RoomMessage as string, @@ -272,7 +275,7 @@ export const Reply = as<'div', ReplyProps>( // strip the PMP fallback if it's there, to avoid displaying the PMP name twice const formattedBodyStripped: string = pmp - ? formattedBody?.replace(/^]*>.*?<\/strong>/, '') + ? stripPerMessageProfileFormattedBody(formattedBody ?? '') : formattedBody; const extensibleContent = contentForPreview[M_TEXT.name] as diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 2758e9ffd..6adb44390 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -152,6 +152,7 @@ import { getCurrentlyUsedPerMessageProfileForRoom, type PerMessageProfileMsc4461, setCurrentlyUsedPerMessageProfileIdForRoom, + stripPerMessageProfileFormattedBody, } from '$hooks/usePerMessageProfile'; import { Bell, @@ -640,7 +641,7 @@ export const RoomInput = forwardRef( } } const editableHtml = pmpDisplayname - ? customHtml?.replace(/^]*>.*?<\/strong>/, '') + ? stripPerMessageProfileFormattedBody(customHtml ?? '') : customHtml; const mentionOptions = { diff --git a/src/app/features/room/buildReplacementContent.ts b/src/app/features/room/buildReplacementContent.ts index 011ba3b3d..ce325933d 100644 --- a/src/app/features/room/buildReplacementContent.ts +++ b/src/app/features/room/buildReplacementContent.ts @@ -3,7 +3,10 @@ import { MsgType, RelationType } from '$types/matrix-sdk'; import { customHtmlEqualsPlainText } from '$components/editor'; import { sanitizeText } from '$utils/sanitize'; import type { PerMessageProfileMsc4461 } from '$hooks/usePerMessageProfile'; -import { convertPerMessageProfileToBeeperFormat } from '$hooks/usePerMessageProfile'; +import { + convertPerMessageProfileToBeeperFormat, + stripPerMessageProfileFormattedBody, +} from '$hooks/usePerMessageProfile'; import { MATRIX_UNSTABLE_PER_MESSAGE_PROFILE_PROPERTY_NAME } from '$unstable/prefixes'; /** @@ -111,10 +114,9 @@ export function buildReplacementPmpContent( let newPlainBody = plainBody?.replace(/^.*?: /, ''); const formattedBody = oldContent.formatted_body; - let newFormattedBody = formattedBody?.replace( - /^]*>.*?<\/strong>/, - '' - ); + let newFormattedBody = formattedBody + ? stripPerMessageProfileFormattedBody(formattedBody) + : undefined; oldContent.formatted_body = newFormattedBody; oldContent.body = newPlainBody; diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 7829440f4..09253cfd1 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -81,6 +81,7 @@ import { readdAngleBracketsForHiddenPreviews, stripMarkdownEscapesForHiddenPreviews, } from './hiddenLinkPreviews'; +import { stripPerMessageProfileFormattedBody } from '$hooks/usePerMessageProfile'; // Wraps the mobile emoji-board overlay so the Android back action closes it // instead of navigating away. Hooks can't run inside the UseStateProvider @@ -164,10 +165,7 @@ export const MessageEditor = as<'div', MessageEditorProps>( } if (pmpDisplayname && typeof customHtml === 'string') { - customHtml = customHtml.replace( - /^]*>.*?<\/strong>/, - '' - ); + customHtml = stripPerMessageProfileFormattedBody(customHtml); } const bundleContent = diff --git a/src/app/hooks/usePerMessageProfile.ts b/src/app/hooks/usePerMessageProfile.ts index 5fdf4d7f4..b0673f31c 100644 --- a/src/app/hooks/usePerMessageProfile.ts +++ b/src/app/hooks/usePerMessageProfile.ts @@ -825,3 +825,20 @@ export async function getCurrentlyUsedPerMessageProfileForAccount( const pmp = profileId ? await getPerMessageProfileById(mx, profileId) : undefined; return profileId ? pmp : undefined; } + +/* + * If you don't supply a profile, it may fail if the displayname has a colon. + */ +export function stripPerMessageProfilePlainBody( + formatted_body: string, + profile?: PerMessageProfileMsc4461 +): string { + if (profile) { + return formatted_body.replace(`${profile.displayname}: `, ''); + } else { + return formatted_body.replace(/^.*?: /, ''); + } +} +export function stripPerMessageProfileFormattedBody(formatted_body: string): string { + return formatted_body.replace(/^]*>.*?<\/strong>/, ''); +}