Add better support for empty room name fallback

This commit is contained in:
Benoit Marty 2021-04-06 09:56:12 +02:00 committed by Benoit Marty
parent bd14c77ff0
commit 2d4866cdc5
7 changed files with 27 additions and 13 deletions

View file

@ -14,6 +14,7 @@ Improvements 🙌:
- Update reactions to Unicode 13.1 (#2998)
- Be more robust when parsing some enums
- Improve timeline filtering (dissociate membership and profile events, display hidden events when highlighted, fix hidden item/read receipts behavior)
- Add better support for empty room name fallback (#3106)
Bugfix 🐛:
- Fix bad theme change for the MainActivity

View file

@ -18,12 +18,12 @@ package org.matrix.android.sdk.common
import org.matrix.android.sdk.api.RoomDisplayNameFallbackProvider
class TestRoomDisplayNameFallbackProvider() : RoomDisplayNameFallbackProvider {
class TestRoomDisplayNameFallbackProvider : RoomDisplayNameFallbackProvider {
override fun getNameForRoomInvite() =
"Room invite"
override fun getNameForEmptyRoom() =
override fun getNameForEmptyRoom(isDirect: Boolean, leftMemberNames: List<String>) =
"Empty room"
override fun getNameFor2members(name1: String?, name2: String?) =

View file

@ -18,7 +18,7 @@ package org.matrix.android.sdk.api
interface RoomDisplayNameFallbackProvider {
fun getNameForRoomInvite(): String
fun getNameForEmptyRoom(): String
fun getNameForEmptyRoom(isDirect: Boolean, leftMemberNames: List<String>): String
fun getNameFor2members(name1: String?, name2: String?): String
fun getNameFor3members(name1: String?, name2: String?, name3: String?): String
fun getNameFor4members(name1: String?, name2: String?, name3: String?, name4: String?): String

View file

@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room.membership
import io.realm.Realm
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.Membership
@ -51,14 +52,14 @@ internal class RoomDisplayNameResolver @Inject constructor(
* @param roomId: the roomId to resolve the name of.
* @return the room display name
*/
fun resolve(realm: Realm, roomId: String): CharSequence {
fun resolve(realm: Realm, roomId: String): String {
// this algorithm is the one defined in
// https://github.com/matrix-org/matrix-js-sdk/blob/develop/lib/models/room.js#L617
// calculateRoomName(room, userId)
// For Lazy Loaded room, see algorithm here:
// https://docs.google.com/document/d/11i14UI1cUz-OJ0knD5BFu7fmT6Fo327zvMYqfSAR7xs/edit#heading=h.qif6pkqyjgzn
var name: CharSequence?
var name: String?
val roomEntity = RoomEntity.where(realm, roomId = roomId).findFirst()
val roomName = CurrentStateEventEntity.getOrNull(realm, roomId, type = EventType.STATE_ROOM_NAME, stateKey = "")?.root
name = ContentMapper.map(roomName?.content).toModel<RoomNameContent>()?.name
@ -105,8 +106,12 @@ internal class RoomDisplayNameResolver @Inject constructor(
val otherMembersCount = otherMembersSubset.count()
name = when (otherMembersCount) {
0 -> {
roomDisplayNameFallbackProvider.getNameForEmptyRoom()
// TODO (was xx and yyy) ...
// Get left members if any
val leftMembersNames = roomMembers.queryRoomMembersEvent()
.findAll()
.filter { it.membership == Membership.LEAVE }
.map { it.getBestName() }
roomDisplayNameFallbackProvider.getNameForEmptyRoom(roomSummary?.isDirect.orFalse(), leftMembersNames)
}
1 -> resolveRoomMemberName(otherMembersSubset[0], roomMembers)
2 -> {

View file

@ -102,7 +102,7 @@ internal class RoomSummaryUpdater @Inject constructor(
// avoid this call if we are sure there are unread events
|| !isEventRead(realm.configuration, userId, roomId, latestPreviewableEvent?.eventId)
roomSummaryEntity.displayName = roomDisplayNameResolver.resolve(realm, roomId).toString()
roomSummaryEntity.displayName = roomDisplayNameResolver.resolve(realm, roomId)
roomSummaryEntity.avatarUrl = roomAvatarResolver.resolve(realm, roomId)
roomSummaryEntity.name = ContentMapper.map(lastNameEvent?.content).toModel<RoomNameContent>()?.name
roomSummaryEntity.topic = ContentMapper.map(lastTopicEvent?.content).toModel<RoomTopicContent>()?.topic

View file

@ -46,6 +46,7 @@ import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.room.RoomAvatarResolver
import org.matrix.android.sdk.internal.session.room.membership.RoomDisplayNameResolver
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper
import org.matrix.android.sdk.internal.session.sync.model.InvitedRoomSync
import org.matrix.android.sdk.internal.session.sync.model.accountdata.BreadcrumbsContent
@ -62,7 +63,8 @@ internal class UserAccountDataSyncHandler @Inject constructor(
@UserId private val userId: String,
private val directChatsHelper: DirectChatsHelper,
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
private val roomAvatarResolver: RoomAvatarResolver
private val roomAvatarResolver: RoomAvatarResolver,
private val roomDisplayNameResolver: RoomDisplayNameResolver
) {
fun handle(realm: Realm, accountData: UserAccountDataSync?) {
@ -161,8 +163,9 @@ internal class UserAccountDataSyncHandler @Inject constructor(
if (roomSummaryEntity != null) {
roomSummaryEntity.isDirect = true
roomSummaryEntity.directUserId = userId
// Also update the avatar, there is a specific treatment for DMs
// Also update the avatar and displayname, there is a specific treatment for DMs
roomSummaryEntity.avatarUrl = roomAvatarResolver.resolve(realm, roomId)
roomSummaryEntity.displayName = roomDisplayNameResolver.resolve(realm, roomId)
}
}
}
@ -172,8 +175,9 @@ internal class UserAccountDataSyncHandler @Inject constructor(
.forEach {
it.isDirect = false
it.directUserId = null
// Also update the avatar, there was a specific treatment for DMs
// Also update the avatar and displayname, there was a specific treatment for DMs
it.avatarUrl = roomAvatarResolver.resolve(realm, it.roomId)
it.displayName = roomDisplayNameResolver.resolve(realm, it.roomId)
}
}

View file

@ -28,8 +28,12 @@ class VectorRoomDisplayNameFallbackProvider(
return context.getString(R.string.room_displayname_room_invite)
}
override fun getNameForEmptyRoom(): String {
return context.getString(R.string.room_displayname_empty_room)
override fun getNameForEmptyRoom(isDirect: Boolean, leftMemberNames: List<String>): String {
return if (leftMemberNames.isEmpty()) {
context.getString(R.string.room_displayname_empty_room)
} else {
context.getString(R.string.room_displayname_empty_room_was, leftMemberNames.joinToString())
}
}
override fun getNameFor2members(name1: String?, name2: String?): String {