Strip formatting from media captions in room summary

This commit is contained in:
bxdxnn 2026-04-27 16:31:20 +00:00
parent 9a2ad3928a
commit fff27ff3f1
3 changed files with 32 additions and 14 deletions

View file

@ -77,26 +77,28 @@ class DefaultPinnedMessagesBannerFormatter(
messageType.toPlainText(permalinkParser)
}
is VideoMessageType -> {
messageType.bestDescription.prefixWith(CommonStrings.common_video)
messageType.toPlainText(permalinkParser).prefixWith(CommonStrings.common_video)
}
is ImageMessageType -> {
messageType.bestDescription.prefixWith(CommonStrings.common_image)
messageType.toPlainText(permalinkParser).prefixWith(CommonStrings.common_image)
}
is StickerMessageType -> {
messageType.bestDescription.prefixWith(CommonStrings.common_sticker)
messageType.toPlainText(permalinkParser).prefixWith(CommonStrings.common_sticker)
}
is LocationMessageType -> {
messageType.body.prefixWith(CommonStrings.common_shared_location)
}
is FileMessageType -> {
messageType.bestDescription.prefixWith(CommonStrings.common_file)
messageType.toPlainText(permalinkParser).prefixWith(CommonStrings.common_file)
}
is AudioMessageType -> {
messageType.bestDescription.prefixWith(CommonStrings.common_audio)
messageType.toPlainText(permalinkParser).prefixWith(CommonStrings.common_audio)
}
is VoiceMessageType -> {
// In this case, do not use bestDescription, because the filename is useless, only use the caption if available.
messageType.caption?.prefixWith(sp.getString(CommonStrings.common_voice_message))
messageType
.toPlainText(permalinkParser, "")
.takeIf { it.isNotEmpty() }
?.prefixWith(sp.getString(CommonStrings.common_voice_message))
?: sp.getString(CommonStrings.common_voice_message)
}
is OtherMessageType -> {

View file

@ -139,26 +139,28 @@ class DefaultRoomLatestEventFormatter(
messageType.toPlainText(permalinkParser)
}
is VideoMessageType -> {
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_video))
messageType.toPlainText(permalinkParser).prefixWith(sp.getString(CommonStrings.common_video))
}
is ImageMessageType -> {
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_image))
messageType.toPlainText(permalinkParser).prefixWith(sp.getString(CommonStrings.common_image))
}
is StickerMessageType -> {
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_sticker))
messageType.toPlainText(permalinkParser).prefixWith(sp.getString(CommonStrings.common_sticker))
}
is LocationMessageType -> {
sp.getString(CommonStrings.common_shared_location)
}
is FileMessageType -> {
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_file))
messageType.toPlainText(permalinkParser).prefixWith(sp.getString(CommonStrings.common_file))
}
is AudioMessageType -> {
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_audio))
messageType.toPlainText(permalinkParser).prefixWith(sp.getString(CommonStrings.common_audio))
}
is VoiceMessageType -> {
// In this case, do not use bestDescription, because the filename is useless, only use the caption if available.
messageType.caption?.prefixWith(sp.getString(CommonStrings.common_voice_message))
messageType
.toPlainText(permalinkParser, "")
.takeIf { it.isNotEmpty() }
?.prefixWith(sp.getString(CommonStrings.common_voice_message))
?: sp.getString(CommonStrings.common_voice_message)
}
is OtherMessageType -> {

View file

@ -11,6 +11,7 @@ package io.element.android.libraries.matrix.ui.messages
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
import io.element.android.libraries.matrix.api.timeline.item.event.MessageTypeWithAttachment
import io.element.android.libraries.matrix.api.timeline.item.event.TextMessageType
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
@ -26,6 +27,19 @@ fun TextMessageType.toPlainText(
permalinkParser: PermalinkParser,
) = formatted?.toPlainText(permalinkParser) ?: body
/**
* Converts the HTML string in [MessageTypeWithAttachment.formattedCaption] to a plain text representation by parsing it and removing all formatting.
* If the caption is not formatted or the format is not [MessageFormat.HTML], the [MessageTypeWithAttachment.caption] is returned instead.
* If there is no caption, returns [default].
*/
fun MessageTypeWithAttachment.toPlainText(
permalinkParser: PermalinkParser,
default: String = filename,
): String {
val plainTextFromFormatted = formattedCaption?.toPlainText(permalinkParser)
return plainTextFromFormatted ?: caption ?: default
}
/**
* Converts the HTML string in [FormattedBody.body] to a plain text representation by parsing it and removing all formatting.
* If the message is not formatted or the format is not [MessageFormat.HTML] we return `null`.