Merge pull request #3547 from element-hq/feature/bma/you

Prefix message sent by the current user by `You` instead of the sender name.
This commit is contained in:
Benoit Marty 2024-09-27 10:11:21 +02:00 committed by GitHub
commit e80dc50a3b
3 changed files with 56 additions and 34 deletions

View file

@ -61,25 +61,18 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
val isOutgoing = event.isOwn val isOutgoing = event.isOwn
val senderDisambiguatedDisplayName = event.senderProfile.getDisambiguatedDisplayName(event.sender) val senderDisambiguatedDisplayName = event.senderProfile.getDisambiguatedDisplayName(event.sender)
return when (val content = event.content) { return when (val content = event.content) {
is MessageContent -> processMessageContents(content, senderDisambiguatedDisplayName, isDmRoom) is MessageContent -> content.process(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
RedactedContent -> { RedactedContent -> {
val message = sp.getString(CommonStrings.common_message_removed) val message = sp.getString(CommonStrings.common_message_removed)
if (!isDmRoom) { message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
message.prefixWith(senderDisambiguatedDisplayName)
} else {
message
}
} }
is StickerContent -> { is StickerContent -> {
prefixIfNeeded(sp.getString(CommonStrings.common_sticker) + " (" + content.body + ")", senderDisambiguatedDisplayName, isDmRoom) val message = sp.getString(CommonStrings.common_sticker) + " (" + content.body + ")"
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
} }
is UnableToDecryptContent -> { is UnableToDecryptContent -> {
val message = sp.getString(CommonStrings.common_waiting_for_decryption_key) val message = sp.getString(CommonStrings.common_waiting_for_decryption_key)
if (!isDmRoom) { message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
message.prefixWith(senderDisambiguatedDisplayName)
} else {
message
}
} }
is RoomMembershipContent -> { is RoomMembershipContent -> {
roomMembershipContentFormatter.format(content, senderDisambiguatedDisplayName, isOutgoing) roomMembershipContentFormatter.format(content, senderDisambiguatedDisplayName, isOutgoing)
@ -92,22 +85,23 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
} }
is PollContent -> { is PollContent -> {
val message = sp.getString(CommonStrings.common_poll_summary, content.question) val message = sp.getString(CommonStrings.common_poll_summary, content.question)
prefixIfNeeded(message, senderDisambiguatedDisplayName, isDmRoom) message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
} }
is FailedToParseMessageLikeContent, is FailedToParseStateContent, is UnknownContent -> { is FailedToParseMessageLikeContent, is FailedToParseStateContent, is UnknownContent -> {
prefixIfNeeded(sp.getString(CommonStrings.common_unsupported_event), senderDisambiguatedDisplayName, isDmRoom) val message = sp.getString(CommonStrings.common_unsupported_event)
message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
} }
is LegacyCallInviteContent -> sp.getString(CommonStrings.common_call_invite) is LegacyCallInviteContent -> sp.getString(CommonStrings.common_call_invite)
is CallNotifyContent -> sp.getString(CommonStrings.common_call_started) is CallNotifyContent -> sp.getString(CommonStrings.common_call_started)
}?.take(MAX_SAFE_LENGTH) }?.take(MAX_SAFE_LENGTH)
} }
private fun processMessageContents( private fun MessageContent.process(
messageContent: MessageContent,
senderDisambiguatedDisplayName: String, senderDisambiguatedDisplayName: String,
isDmRoom: Boolean, isDmRoom: Boolean,
isOutgoing: Boolean
): CharSequence { ): CharSequence {
val internalMessage = when (val messageType: MessageType = messageContent.type) { val message = when (val messageType: MessageType = type) {
// Doesn't need a prefix // Doesn't need a prefix
is EmoteMessageType -> { is EmoteMessageType -> {
return "* $senderDisambiguatedDisplayName ${messageType.body}" return "* $senderDisambiguatedDisplayName ${messageType.body}"
@ -143,16 +137,22 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
messageType.body messageType.body
} }
} }
return prefixIfNeeded(internalMessage, senderDisambiguatedDisplayName, isDmRoom) return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing)
} }
private fun prefixIfNeeded( private fun String.prefixIfNeeded(
message: String,
senderDisambiguatedDisplayName: String, senderDisambiguatedDisplayName: String,
isDmRoom: Boolean, isDmRoom: Boolean,
isOutgoing: Boolean,
): CharSequence = if (isDmRoom) { ): CharSequence = if (isDmRoom) {
message this
} else { } else {
message.prefixWith(senderDisambiguatedDisplayName) prefixWith(
if (isOutgoing) {
sp.getString(CommonStrings.common_you)
} else {
senderDisambiguatedDisplayName
}
)
} }
} }

View file

@ -148,7 +148,29 @@ class DefaultRoomLastMessageFormatterTest {
@Test @Test
@Config(qualifiers = "en") @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" val body = "Shared body"
fun createMessageContent(type: MessageType): MessageContent { fun createMessageContent(type: MessageType): MessageContent {
return MessageContent(body, null, false, false, type) return MessageContent(body, null, false, false, type)
@ -167,7 +189,6 @@ class DefaultRoomLastMessageFormatterTest {
EmoteMessageType(body, null), EmoteMessageType(body, null),
OtherMessageType(msgType = "a_type", body = body), OtherMessageType(msgType = "a_type", body = body),
) )
val senderName = "Someone"
val resultsInRoom = mutableListOf<Pair<MessageType, CharSequence?>>() val resultsInRoom = mutableListOf<Pair<MessageType, CharSequence?>>()
val resultsInDm = mutableListOf<Pair<MessageType, CharSequence?>>() val resultsInDm = mutableListOf<Pair<MessageType, CharSequence?>>()
@ -175,7 +196,7 @@ class DefaultRoomLastMessageFormatterTest {
sequenceOf(false, true).forEach { isDm -> sequenceOf(false, true).forEach { isDm ->
sharedContentMessagesTypes.forEach { type -> sharedContentMessagesTypes.forEach { type ->
val content = createMessageContent(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) val result = formatter.format(message, isDmRoom = isDm)
if (isDm) { if (isDm) {
resultsInDm.add(type to result) resultsInDm.add(type to result)
@ -207,16 +228,16 @@ class DefaultRoomLastMessageFormatterTest {
for ((type, result) in resultsInRoom) { for ((type, result) in resultsInRoom) {
val string = result.toString() val string = result.toString()
val expectedResult = when (type) { val expectedResult = when (type) {
is VideoMessageType -> "$senderName: Video" is VideoMessageType -> "$expectedPrefix: Video"
is AudioMessageType -> "$senderName: Audio" is AudioMessageType -> "$expectedPrefix: Audio"
is VoiceMessageType -> "$senderName: Voice message" is VoiceMessageType -> "$expectedPrefix: Voice message"
is ImageMessageType -> "$senderName: Image" is ImageMessageType -> "$expectedPrefix: Image"
is StickerMessageType -> "$senderName: Sticker" is StickerMessageType -> "$expectedPrefix: Sticker"
is FileMessageType -> "$senderName: File" is FileMessageType -> "$expectedPrefix: File"
is LocationMessageType -> "$senderName: Shared location" is LocationMessageType -> "$expectedPrefix: Shared location"
is TextMessageType, is TextMessageType,
is NoticeMessageType, is NoticeMessageType,
is OtherMessageType -> "$senderName: $body" is OtherMessageType -> "$expectedPrefix: $body"
is EmoteMessageType -> "* $senderName ${type.body}" is EmoteMessageType -> "* $senderName ${type.body}"
} }
val shouldCreateAnnotatedString = when (type) { val shouldCreateAnnotatedString = when (type) {
@ -821,7 +842,7 @@ class DefaultRoomLastMessageFormatterTest {
val pollContent = aPollContent() val pollContent = aPollContent()
val mineContentEvent = createRoomEvent(sentByYou = true, senderDisplayName = "Alice", content = pollContent) 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) val contentEvent = createRoomEvent(sentByYou = false, senderDisplayName = "Bob", content = pollContent)
assertThat(formatter.format(contentEvent, false).toString()).isEqualTo("Bob: Poll: Do you like polls?") 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_voice_message">"Voice message"</string>
<string name="common_waiting">"Waiting…"</string> <string name="common_waiting">"Waiting…"</string>
<string name="common_waiting_for_decryption_key">"Waiting for this message"</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_confirmation">"Confirmation"</string>
<string name="dialog_title_error">"Error"</string> <string name="dialog_title_error">"Error"</string>
<string name="dialog_title_success">"Success"</string> <string name="dialog_title_success">"Success"</string>