Merge pull request #2221 from element-hq/feature/bma/displayNameAmbiguous

Display name disambiguation
This commit is contained in:
Benoit Marty 2024-01-19 17:42:32 +01:00 committed by GitHub
commit 5e359a4e73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 101 additions and 5 deletions

View file

@ -17,6 +17,7 @@
package io.element.android.libraries.matrix.api.timeline.item.event
import androidx.compose.runtime.Immutable
import io.element.android.libraries.matrix.api.core.UserId
@Immutable
sealed interface ProfileTimelineDetails {
@ -34,3 +35,20 @@ sealed interface ProfileTimelineDetails {
val message: String
) : ProfileTimelineDetails
}
/**
* Returns a disambiguated display name for the user.
* If the display name is null, or profile is not Ready, the user ID is returned.
* If the display name is ambiguous, the user ID is appended in parentheses.
* Otherwise, the display name is returned.
*/
fun ProfileTimelineDetails.getDisambiguatedDisplayName(userId: UserId): String {
return when (this) {
is ProfileTimelineDetails.Ready -> when {
displayName == null -> userId.value
displayNameAmbiguous -> "$displayName ($userId)"
else -> displayName
}
else -> userId.value
}
}