From 083bc6df0ef20820e4af2998726db73417e949b8 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sun, 11 Sep 2022 11:06:45 +0200 Subject: [PATCH] Better fallback for inline images without alt description text Use same text as for plain images sent Change-Id: I675558618332f4a0053db5b3f40195b046b5682c --- .../android/sdk/api/session/room/timeline/TimelineEvent.kt | 6 +++--- .../java/org/matrix/android/sdk/api/util/ContentUtils.kt | 4 ++-- .../detail/timeline/format/DisplayableEventFormatter.kt | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt index d391abf1e6..1dcf20ed42 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineEvent.kt @@ -193,9 +193,9 @@ fun TimelineEvent.getTextEditableContent(): String { * Get the latest displayable content. * Will take care to hide spoiler text */ -fun MessageContent.getTextDisplayableContent(): String { - return newContent?.toModel()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) } +fun MessageContent.getTextDisplayableContent(imageFallback: String = ""): String { + return newContent?.toModel()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) } ?: newContent?.toModel()?.body - ?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) } + ?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) } ?: body } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/ContentUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/ContentUtils.kt index 84c8bb4fb1..dfee1afd2b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/ContentUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/util/ContentUtils.kt @@ -49,7 +49,7 @@ object ContentUtils { } @Suppress("RegExpRedundantEscape") - fun formatSpoilerTextFromHtml(formattedBody: String): String { + fun formatSpoilerTextFromHtml(formattedBody: String, imageFallback: String = ""): String { // var reason = "", // can capture the spoiler reason for better formatting? ex. { reason = it.value; ">"} return formattedBody.replace("(?<=".toRegex(), ">") @@ -59,7 +59,7 @@ object ContentUtils { tryOrNull { val alt = Regex("""\s+alt="([^"]*)"""").find(matchResult.groupValues[1]) alt?.groupValues?.get(1) - } ?: "" + } ?: imageFallback } .unescapeHtml() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt index 437a7bd552..ec12930a7f 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/format/DisplayableEventFormatter.kt @@ -63,7 +63,7 @@ class DisplayableEventFormatter @Inject constructor( timelineEvent.getLastMessageContent()?.let { messageContent -> when (messageContent.msgType) { MessageType.MSGTYPE_TEXT -> { - val body = messageContent.getTextDisplayableContent() + val body = messageContent.getTextDisplayableContent(imageFallback = stringProvider.getString(R.string.sent_an_image)) if (messageContent is MessageTextContent && messageContent.matrixFormattedBody.isNullOrBlank().not()) { val localFormattedBody = htmlRenderer.get().parse(body) as Document val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body @@ -175,7 +175,7 @@ class DisplayableEventFormatter @Inject constructor( (event.getClearContent().toModel() as? MessageContent)?.let { messageContent -> when (messageContent.msgType) { MessageType.MSGTYPE_TEXT -> { - val body = messageContent.getTextDisplayableContent() + val body = messageContent.getTextDisplayableContent(imageFallback = stringProvider.getString(R.string.sent_an_image)) if (messageContent is MessageTextContent && messageContent.matrixFormattedBody.isNullOrBlank().not()) { val localFormattedBody = htmlRenderer.get().parse(body) as Document val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body