Better fallback for inline images without alt description text

Use same text as for plain images sent

Change-Id: I675558618332f4a0053db5b3f40195b046b5682c
This commit is contained in:
SpiritCroc 2022-09-11 11:06:45 +02:00
parent 5751862c35
commit 083bc6df0e
3 changed files with 7 additions and 7 deletions

View File

@ -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<MessageTextContent>()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) }
fun MessageContent.getTextDisplayableContent(imageFallback: String = ""): String {
return newContent?.toModel<MessageTextContent>()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) }
?: newContent?.toModel<MessageContent>()?.body
?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) }
?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) }
?: body
}

View File

@ -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("(?<=<span data-mx-spoiler)=\\\".+?\\\">".toRegex(), ">")
@ -59,7 +59,7 @@ object ContentUtils {
tryOrNull {
val alt = Regex("""\s+alt="([^"]*)"""").find(matchResult.groupValues[1])
alt?.groupValues?.get(1)
} ?: ""
} ?: imageFallback
}
.unescapeHtml()
}

View File

@ -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