extending the room name resolved to create a dedicated room name data class which contains a normalized version of the room name
This commit is contained in:
parent
9949779b62
commit
611bf29ebe
@ -42,7 +42,7 @@ internal class RoomSummaryMapper @Inject constructor(private val timelineEventMa
|
||||
|
||||
return RoomSummary(
|
||||
roomId = roomSummaryEntity.roomId,
|
||||
displayName = roomSummaryEntity.displayName ?: "",
|
||||
displayName = roomSummaryEntity.displayName() ?: "",
|
||||
name = roomSummaryEntity.name ?: "",
|
||||
topic = roomSummaryEntity.topic ?: "",
|
||||
avatarUrl = roomSummaryEntity.avatarUrl ?: "",
|
||||
|
@ -28,6 +28,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||
import org.matrix.android.sdk.api.session.room.model.VersioningState
|
||||
import org.matrix.android.sdk.api.session.room.model.tag.RoomTag
|
||||
import org.matrix.android.sdk.internal.database.model.presence.UserPresenceEntity
|
||||
import org.matrix.android.sdk.internal.session.room.membership.RoomName
|
||||
|
||||
internal open class RoomSummaryEntity(
|
||||
@PrimaryKey var roomId: String = "",
|
||||
@ -36,20 +37,23 @@ internal open class RoomSummaryEntity(
|
||||
var children: RealmList<SpaceChildSummaryEntity> = RealmList()
|
||||
) : RealmObject() {
|
||||
|
||||
var displayName: String? = ""
|
||||
set(value) {
|
||||
if (value != field) field = value
|
||||
private var displayName: String? = ""
|
||||
|
||||
fun displayName() = displayName
|
||||
|
||||
fun setDisplayName(roomName: RoomName) {
|
||||
if (roomName.name != displayName) {
|
||||
displayName = roomName.name
|
||||
normalizedDisplayName = roomName.normalizedName
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Workaround for Realm only supporting Latin-1 character sets when sorting
|
||||
* or filtering by case
|
||||
* See https://github.com/realm/realm-core/issues/777
|
||||
*/
|
||||
var normalizedDisplayName: String? = ""
|
||||
set(value) {
|
||||
if (value != field) field = value
|
||||
}
|
||||
private var normalizedDisplayName: String? = ""
|
||||
|
||||
var avatarUrl: String? = ""
|
||||
set(value) {
|
||||
|
@ -34,6 +34,7 @@ import org.matrix.android.sdk.internal.database.query.getOrNull
|
||||
import org.matrix.android.sdk.internal.database.query.where
|
||||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import org.matrix.android.sdk.internal.session.displayname.DisplayNameResolver
|
||||
import org.matrix.android.sdk.internal.util.Normalizer
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
@ -42,6 +43,7 @@ import javax.inject.Inject
|
||||
internal class RoomDisplayNameResolver @Inject constructor(
|
||||
matrixConfiguration: MatrixConfiguration,
|
||||
private val displayNameResolver: DisplayNameResolver,
|
||||
private val normalizer: Normalizer,
|
||||
@UserId private val userId: String
|
||||
) {
|
||||
|
||||
@ -54,7 +56,7 @@ 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): String {
|
||||
fun resolve(realm: Realm, roomId: String): RoomName {
|
||||
// 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)
|
||||
@ -66,12 +68,12 @@ internal class RoomDisplayNameResolver @Inject constructor(
|
||||
val roomName = CurrentStateEventEntity.getOrNull(realm, roomId, type = EventType.STATE_ROOM_NAME, stateKey = "")?.root
|
||||
name = ContentMapper.map(roomName?.content).toModel<RoomNameContent>()?.name
|
||||
if (!name.isNullOrEmpty()) {
|
||||
return name
|
||||
return name.toRoomName()
|
||||
}
|
||||
val canonicalAlias = CurrentStateEventEntity.getOrNull(realm, roomId, type = EventType.STATE_ROOM_CANONICAL_ALIAS, stateKey = "")?.root
|
||||
name = ContentMapper.map(canonicalAlias?.content).toModel<RoomCanonicalAliasContent>()?.canonicalAlias
|
||||
if (!name.isNullOrEmpty()) {
|
||||
return name
|
||||
return name.toRoomName()
|
||||
}
|
||||
|
||||
val roomMembers = RoomMemberHelper(realm, roomId)
|
||||
@ -152,7 +154,7 @@ internal class RoomDisplayNameResolver @Inject constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
return name ?: roomId
|
||||
return (name ?: roomId).toRoomName()
|
||||
}
|
||||
|
||||
/** See [org.matrix.android.sdk.api.session.room.sender.SenderInfo.disambiguatedDisplayName] */
|
||||
@ -165,4 +167,8 @@ internal class RoomDisplayNameResolver @Inject constructor(
|
||||
"${roomMemberSummary.displayName} (${roomMemberSummary.userId})"
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.toRoomName() = RoomName(this, normalizedName = normalizer.normalize(this))
|
||||
}
|
||||
|
||||
internal data class RoomName(val name: String, val normalizedName: String)
|
||||
|
@ -138,9 +138,7 @@ internal class RoomSummaryUpdater @Inject constructor(
|
||||
// avoid this call if we are sure there are unread events
|
||||
!isEventRead(realm.configuration, userId, roomId, latestPreviewableEvent?.eventId)
|
||||
|
||||
val roomDisplayName = roomDisplayNameResolver.resolve(realm, roomId)
|
||||
roomSummaryEntity.displayName = roomDisplayName
|
||||
roomSummaryEntity.normalizedDisplayName = normalizer.normalize(roomDisplayName)
|
||||
roomSummaryEntity.setDisplayName(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
|
||||
|
@ -166,7 +166,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
|
||||
roomSummaryEntity.directUserId = userId
|
||||
// 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)
|
||||
roomSummaryEntity.setDisplayName(roomDisplayNameResolver.resolve(realm, roomId))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -178,7 +178,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
|
||||
it.directUserId = null
|
||||
// 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)
|
||||
it.setDisplayName(roomDisplayNameResolver.resolve(realm, it.roomId))
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user