Prefix with You instead of display name #3470

This commit is contained in:
Benoit Marty
2024-09-26 15:42:43 +02:00
parent 2468592ec0
commit 8118ae7ae1
3 changed files with 50 additions and 20 deletions

View File

@@ -61,18 +61,18 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
val isOutgoing = event.isOwn
val senderDisambiguatedDisplayName = event.senderProfile.getDisambiguatedDisplayName(event.sender)
return when (val content = event.content) {
is MessageContent -> content.process(senderDisambiguatedDisplayName, isDmRoom)
is MessageContent -> content.process(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
RedactedContent -> {
val message = sp.getString(CommonStrings.common_message_removed)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
is StickerContent -> {
val message = sp.getString(CommonStrings.common_sticker) + " (" + content.body + ")"
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
is UnableToDecryptContent -> {
val message = sp.getString(CommonStrings.common_waiting_for_decryption_key)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
is RoomMembershipContent -> {
roomMembershipContentFormatter.format(content, senderDisambiguatedDisplayName, isOutgoing)
@@ -85,11 +85,11 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
}
is PollContent -> {
val message = sp.getString(CommonStrings.common_poll_summary, content.question)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
is FailedToParseMessageLikeContent, is FailedToParseStateContent, is UnknownContent -> {
val message = sp.getString(CommonStrings.common_unsupported_event)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
is LegacyCallInviteContent -> sp.getString(CommonStrings.common_call_invite)
is CallNotifyContent -> sp.getString(CommonStrings.common_call_started)
@@ -99,6 +99,7 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
private fun MessageContent.process(
senderDisambiguatedDisplayName: String,
isDmRoom: Boolean,
isOutgoing: Boolean
): CharSequence {
val message = when (val messageType: MessageType = type) {
// Doesn't need a prefix
@@ -136,15 +137,22 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
messageType.body
}
}
return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom)
return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
}
private fun String.prefixIfNeeded(
senderDisambiguatedDisplayName: String,
isDmRoom: Boolean,
isOutgoing: Boolean,
): CharSequence = if (isDmRoom) {
this
} else {
prefixWith(senderDisambiguatedDisplayName)
prefixWith(
if (isOutgoing) {
sp.getString(CommonStrings.common_you)
} else {
senderDisambiguatedDisplayName
}
)
}
}

View File

@@ -148,7 +148,29 @@ class DefaultRoomLastMessageFormatterTest {
@Test
@Config(qualifiers = "en")
fun `Message contents`() {
fun `Message contents sent by other user`() {
testMessageContents(
sentByYou = false,
senderName = "Alice",
expectedPrefix = "Alice",
)
}
@Test
@Config(qualifiers = "en")
fun `Message contents sent by current user`() {
testMessageContents(
sentByYou = true,
senderName = "Bob",
expectedPrefix = "You",
)
}
private fun testMessageContents(
sentByYou: Boolean,
senderName: String,
expectedPrefix: String,
) {
val body = "Shared body"
fun createMessageContent(type: MessageType): MessageContent {
return MessageContent(body, null, false, false, type)
@@ -167,7 +189,6 @@ class DefaultRoomLastMessageFormatterTest {
EmoteMessageType(body, null),
OtherMessageType(msgType = "a_type", body = body),
)
val senderName = "Someone"
val resultsInRoom = mutableListOf<Pair<MessageType, CharSequence?>>()
val resultsInDm = mutableListOf<Pair<MessageType, CharSequence?>>()
@@ -175,7 +196,7 @@ class DefaultRoomLastMessageFormatterTest {
sequenceOf(false, true).forEach { isDm ->
sharedContentMessagesTypes.forEach { type ->
val content = createMessageContent(type)
val message = createRoomEvent(sentByYou = false, senderDisplayName = "Someone", content = content)
val message = createRoomEvent(sentByYou = sentByYou, senderDisplayName = senderName, content = content)
val result = formatter.format(message, isDmRoom = isDm)
if (isDm) {
resultsInDm.add(type to result)
@@ -207,16 +228,16 @@ class DefaultRoomLastMessageFormatterTest {
for ((type, result) in resultsInRoom) {
val string = result.toString()
val expectedResult = when (type) {
is VideoMessageType -> "$senderName: Video"
is AudioMessageType -> "$senderName: Audio"
is VoiceMessageType -> "$senderName: Voice message"
is ImageMessageType -> "$senderName: Image"
is StickerMessageType -> "$senderName: Sticker"
is FileMessageType -> "$senderName: File"
is LocationMessageType -> "$senderName: Shared location"
is VideoMessageType -> "$expectedPrefix: Video"
is AudioMessageType -> "$expectedPrefix: Audio"
is VoiceMessageType -> "$expectedPrefix: Voice message"
is ImageMessageType -> "$expectedPrefix: Image"
is StickerMessageType -> "$expectedPrefix: Sticker"
is FileMessageType -> "$expectedPrefix: File"
is LocationMessageType -> "$expectedPrefix: Shared location"
is TextMessageType,
is NoticeMessageType,
is OtherMessageType -> "$senderName: $body"
is OtherMessageType -> "$expectedPrefix: $body"
is EmoteMessageType -> "* $senderName ${type.body}"
}
val shouldCreateAnnotatedString = when (type) {
@@ -821,7 +842,7 @@ class DefaultRoomLastMessageFormatterTest {
val pollContent = aPollContent()
val mineContentEvent = createRoomEvent(sentByYou = true, senderDisplayName = "Alice", content = pollContent)
assertThat(formatter.format(mineContentEvent, false).toString()).isEqualTo("Alice: Poll: Do you like polls?")
assertThat(formatter.format(mineContentEvent, false).toString()).isEqualTo("You: Poll: Do you like polls?")
val contentEvent = createRoomEvent(sentByYou = false, senderDisplayName = "Bob", content = pollContent)
assertThat(formatter.format(contentEvent, false).toString()).isEqualTo("Bob: Poll: Do you like polls?")

View File

@@ -248,6 +248,7 @@ Reason: %1$s."</string>
<string name="common_voice_message">"Voice message"</string>
<string name="common_waiting">"Waiting…"</string>
<string name="common_waiting_for_decryption_key">"Waiting for this message"</string>
<string name="common_you">"You"</string>
<string name="dialog_title_confirmation">"Confirmation"</string>
<string name="dialog_title_error">"Error"</string>
<string name="dialog_title_success">"Success"</string>