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. * Get the latest displayable content.
* Will take care to hide spoiler text * Will take care to hide spoiler text
*/ */
fun MessageContent.getTextDisplayableContent(): String { fun MessageContent.getTextDisplayableContent(imageFallback: String = ""): String {
return newContent?.toModel<MessageTextContent>()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) } return newContent?.toModel<MessageTextContent>()?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) }
?: newContent?.toModel<MessageContent>()?.body ?: newContent?.toModel<MessageContent>()?.body
?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it) } ?: (this as MessageTextContent?)?.matrixFormattedBody?.let { ContentUtils.formatSpoilerTextFromHtml(it, imageFallback = imageFallback) }
?: body ?: body
} }

View File

@ -49,7 +49,7 @@ object ContentUtils {
} }
@Suppress("RegExpRedundantEscape") @Suppress("RegExpRedundantEscape")
fun formatSpoilerTextFromHtml(formattedBody: String): String { fun formatSpoilerTextFromHtml(formattedBody: String, imageFallback: String = ""): String {
// var reason = "", // var reason = "",
// can capture the spoiler reason for better formatting? ex. { reason = it.value; ">"} // can capture the spoiler reason for better formatting? ex. { reason = it.value; ">"}
return formattedBody.replace("(?<=<span data-mx-spoiler)=\\\".+?\\\">".toRegex(), ">") return formattedBody.replace("(?<=<span data-mx-spoiler)=\\\".+?\\\">".toRegex(), ">")
@ -59,7 +59,7 @@ object ContentUtils {
tryOrNull { tryOrNull {
val alt = Regex("""\s+alt="([^"]*)"""").find(matchResult.groupValues[1]) val alt = Regex("""\s+alt="([^"]*)"""").find(matchResult.groupValues[1])
alt?.groupValues?.get(1) alt?.groupValues?.get(1)
} ?: "" } ?: imageFallback
} }
.unescapeHtml() .unescapeHtml()
} }

View File

@ -63,7 +63,7 @@ class DisplayableEventFormatter @Inject constructor(
timelineEvent.getLastMessageContent()?.let { messageContent -> timelineEvent.getLastMessageContent()?.let { messageContent ->
when (messageContent.msgType) { when (messageContent.msgType) {
MessageType.MSGTYPE_TEXT -> { 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()) { if (messageContent is MessageTextContent && messageContent.matrixFormattedBody.isNullOrBlank().not()) {
val localFormattedBody = htmlRenderer.get().parse(body) as Document val localFormattedBody = htmlRenderer.get().parse(body) as Document
val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body
@ -175,7 +175,7 @@ class DisplayableEventFormatter @Inject constructor(
(event.getClearContent().toModel() as? MessageContent)?.let { messageContent -> (event.getClearContent().toModel() as? MessageContent)?.let { messageContent ->
when (messageContent.msgType) { when (messageContent.msgType) {
MessageType.MSGTYPE_TEXT -> { 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()) { if (messageContent is MessageTextContent && messageContent.matrixFormattedBody.isNullOrBlank().not()) {
val localFormattedBody = htmlRenderer.get().parse(body) as Document val localFormattedBody = htmlRenderer.get().parse(body) as Document
val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body val renderedBody = htmlRenderer.get().render(localFormattedBody) ?: body