Improve last message formatter: add caption (or filename)

This commit is contained in:
Benoit Marty
2024-11-04 16:04:38 +01:00
committed by Benoit Marty
parent 3958edb816
commit 7bcbd2181a
2 changed files with 41 additions and 19 deletions

View File

@@ -110,25 +110,27 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
messageType.toPlainText(permalinkParser)
}
is VideoMessageType -> {
sp.getString(CommonStrings.common_video)
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_video))
}
is ImageMessageType -> {
sp.getString(CommonStrings.common_image)
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_image))
}
is StickerMessageType -> {
sp.getString(CommonStrings.common_sticker)
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_sticker))
}
is LocationMessageType -> {
sp.getString(CommonStrings.common_shared_location)
}
is FileMessageType -> {
sp.getString(CommonStrings.common_file)
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_file))
}
is AudioMessageType -> {
sp.getString(CommonStrings.common_audio)
messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_audio))
}
is VoiceMessageType -> {
sp.getString(CommonStrings.common_voice_message)
// 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))
?: sp.getString(CommonStrings.common_voice_message)
}
is OtherMessageType -> {
messageType.body
@@ -140,7 +142,7 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
private fun String.prefixIfNeeded(
private fun CharSequence.prefixIfNeeded(
senderDisambiguatedDisplayName: String,
isDmRoom: Boolean,
isOutgoing: Boolean,

View File

@@ -208,32 +208,51 @@ class DefaultRoomLastMessageFormatterTest {
// Verify results of DM mode
for ((type, result) in resultsInDm) {
val string = result.toString()
val expectedResult = when (type) {
is VideoMessageType -> "Video"
is AudioMessageType -> "Audio"
is VideoMessageType -> "Video: Shared body"
is AudioMessageType -> "Audio: Shared body"
is VoiceMessageType -> "Voice message"
is ImageMessageType -> "Image"
is StickerMessageType -> "Sticker"
is FileMessageType -> "File"
is ImageMessageType -> "Image: Shared body"
is StickerMessageType -> "Sticker: Shared body"
is FileMessageType -> "File: Shared body"
is LocationMessageType -> "Shared location"
is EmoteMessageType -> "* $senderName ${type.body}"
is TextMessageType,
is NoticeMessageType,
is OtherMessageType -> body
}
assertWithMessage("$type was not properly handled for DM").that(result).isEqualTo(expectedResult)
val shouldCreateAnnotatedString = when (type) {
is VideoMessageType -> true
is AudioMessageType -> true
is VoiceMessageType -> false
is ImageMessageType -> true
is StickerMessageType -> true
is FileMessageType -> true
is LocationMessageType -> false
is EmoteMessageType -> false
is TextMessageType -> false
is NoticeMessageType -> false
is OtherMessageType -> false
}
if (shouldCreateAnnotatedString) {
assertWithMessage("$type doesn't produce an AnnotatedString")
.that(result)
.isInstanceOf(AnnotatedString::class.java)
}
assertWithMessage("$type was not properly handled for DM").that(string).isEqualTo(expectedResult)
}
// Verify results of Room mode
for ((type, result) in resultsInRoom) {
val string = result.toString()
val expectedResult = when (type) {
is VideoMessageType -> "$expectedPrefix: Video"
is AudioMessageType -> "$expectedPrefix: Audio"
is VideoMessageType -> "$expectedPrefix: Video: Shared body"
is AudioMessageType -> "$expectedPrefix: Audio: Shared body"
is VoiceMessageType -> "$expectedPrefix: Voice message"
is ImageMessageType -> "$expectedPrefix: Image"
is StickerMessageType -> "$expectedPrefix: Sticker"
is FileMessageType -> "$expectedPrefix: File"
is ImageMessageType -> "$expectedPrefix: Image: Shared body"
is StickerMessageType -> "$expectedPrefix: Sticker: Shared body"
is FileMessageType -> "$expectedPrefix: File: Shared body"
is LocationMessageType -> "$expectedPrefix: Shared location"
is TextMessageType,
is NoticeMessageType,
@@ -249,7 +268,8 @@ class DefaultRoomLastMessageFormatterTest {
is FileMessageType -> true
is LocationMessageType -> false
is EmoteMessageType -> false
is TextMessageType, is NoticeMessageType -> true
is TextMessageType -> true
is NoticeMessageType -> true
is OtherMessageType -> true
}
if (shouldCreateAnnotatedString) {