Skip to content

Commit 611bf29

Browse files
committed
extending the room name resolved to create a dedicated room name data class which contains a normalized version of the room name
1 parent 9949779 commit 611bf29

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/RoomSummaryMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class RoomSummaryMapper @Inject constructor(private val timelineEventMa
4242

4343
return RoomSummary(
4444
roomId = roomSummaryEntity.roomId,
45-
displayName = roomSummaryEntity.displayName ?: "",
45+
displayName = roomSummaryEntity.displayName() ?: "",
4646
name = roomSummaryEntity.name ?: "",
4747
topic = roomSummaryEntity.topic ?: "",
4848
avatarUrl = roomSummaryEntity.avatarUrl ?: "",

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/RoomSummaryEntity.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomSummary
2828
import org.matrix.android.sdk.api.session.room.model.VersioningState
2929
import org.matrix.android.sdk.api.session.room.model.tag.RoomTag
3030
import org.matrix.android.sdk.internal.database.model.presence.UserPresenceEntity
31+
import org.matrix.android.sdk.internal.session.room.membership.RoomName
3132

3233
internal open class RoomSummaryEntity(
3334
@PrimaryKey var roomId: String = "",
@@ -36,20 +37,23 @@ internal open class RoomSummaryEntity(
3637
var children: RealmList<SpaceChildSummaryEntity> = RealmList()
3738
) : RealmObject() {
3839

39-
var displayName: String? = ""
40-
set(value) {
41-
if (value != field) field = value
40+
private var displayName: String? = ""
41+
42+
fun displayName() = displayName
43+
44+
fun setDisplayName(roomName: RoomName) {
45+
if (roomName.name != displayName) {
46+
displayName = roomName.name
47+
normalizedDisplayName = roomName.normalizedName
4248
}
49+
}
4350

4451
/**
4552
* Workaround for Realm only supporting Latin-1 character sets when sorting
4653
* or filtering by case
4754
* See https://github.com/realm/realm-core/issues/777
4855
*/
49-
var normalizedDisplayName: String? = ""
50-
set(value) {
51-
if (value != field) field = value
52-
}
56+
private var normalizedDisplayName: String? = ""
5357

5458
var avatarUrl: String? = ""
5559
set(value) {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/RoomDisplayNameResolver.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.matrix.android.sdk.internal.database.query.getOrNull
3434
import org.matrix.android.sdk.internal.database.query.where
3535
import org.matrix.android.sdk.internal.di.UserId
3636
import org.matrix.android.sdk.internal.session.displayname.DisplayNameResolver
37+
import org.matrix.android.sdk.internal.util.Normalizer
3738
import javax.inject.Inject
3839

3940
/**
@@ -42,6 +43,7 @@ import javax.inject.Inject
4243
internal class RoomDisplayNameResolver @Inject constructor(
4344
matrixConfiguration: MatrixConfiguration,
4445
private val displayNameResolver: DisplayNameResolver,
46+
private val normalizer: Normalizer,
4547
@UserId private val userId: String
4648
) {
4749

@@ -54,7 +56,7 @@ internal class RoomDisplayNameResolver @Inject constructor(
5456
* @param roomId: the roomId to resolve the name of.
5557
* @return the room display name
5658
*/
57-
fun resolve(realm: Realm, roomId: String): String {
59+
fun resolve(realm: Realm, roomId: String): RoomName {
5860
// this algorithm is the one defined in
5961
// https://github.com/matrix-org/matrix-js-sdk/blob/develop/lib/models/room.js#L617
6062
// calculateRoomName(room, userId)
@@ -66,12 +68,12 @@ internal class RoomDisplayNameResolver @Inject constructor(
6668
val roomName = CurrentStateEventEntity.getOrNull(realm, roomId, type = EventType.STATE_ROOM_NAME, stateKey = "")?.root
6769
name = ContentMapper.map(roomName?.content).toModel<RoomNameContent>()?.name
6870
if (!name.isNullOrEmpty()) {
69-
return name
71+
return name.toRoomName()
7072
}
7173
val canonicalAlias = CurrentStateEventEntity.getOrNull(realm, roomId, type = EventType.STATE_ROOM_CANONICAL_ALIAS, stateKey = "")?.root
7274
name = ContentMapper.map(canonicalAlias?.content).toModel<RoomCanonicalAliasContent>()?.canonicalAlias
7375
if (!name.isNullOrEmpty()) {
74-
return name
76+
return name.toRoomName()
7577
}
7678

7779
val roomMembers = RoomMemberHelper(realm, roomId)
@@ -152,7 +154,7 @@ internal class RoomDisplayNameResolver @Inject constructor(
152154
}
153155
}
154156
}
155-
return name ?: roomId
157+
return (name ?: roomId).toRoomName()
156158
}
157159

158160
/** See [org.matrix.android.sdk.api.session.room.sender.SenderInfo.disambiguatedDisplayName] */
@@ -165,4 +167,8 @@ internal class RoomDisplayNameResolver @Inject constructor(
165167
"${roomMemberSummary.displayName} (${roomMemberSummary.userId})"
166168
}
167169
}
170+
171+
private fun String.toRoomName() = RoomName(this, normalizedName = normalizer.normalize(this))
168172
}
173+
174+
internal data class RoomName(val name: String, val normalizedName: String)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/summary/RoomSummaryUpdater.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ internal class RoomSummaryUpdater @Inject constructor(
138138
// avoid this call if we are sure there are unread events
139139
!isEventRead(realm.configuration, userId, roomId, latestPreviewableEvent?.eventId)
140140

141-
val roomDisplayName = roomDisplayNameResolver.resolve(realm, roomId)
142-
roomSummaryEntity.displayName = roomDisplayName
143-
roomSummaryEntity.normalizedDisplayName = normalizer.normalize(roomDisplayName)
141+
roomSummaryEntity.setDisplayName(roomDisplayNameResolver.resolve(realm, roomId))
144142
roomSummaryEntity.avatarUrl = roomAvatarResolver.resolve(realm, roomId)
145143
roomSummaryEntity.name = ContentMapper.map(lastNameEvent?.content).toModel<RoomNameContent>()?.name
146144
roomSummaryEntity.topic = ContentMapper.map(lastTopicEvent?.content).toModel<RoomTopicContent>()?.topic

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/handler/UserAccountDataSyncHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
166166
roomSummaryEntity.directUserId = userId
167167
// Also update the avatar and displayname, there is a specific treatment for DMs
168168
roomSummaryEntity.avatarUrl = roomAvatarResolver.resolve(realm, roomId)
169-
roomSummaryEntity.displayName = roomDisplayNameResolver.resolve(realm, roomId)
169+
roomSummaryEntity.setDisplayName(roomDisplayNameResolver.resolve(realm, roomId))
170170
}
171171
}
172172
}
@@ -178,7 +178,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(
178178
it.directUserId = null
179179
// Also update the avatar and displayname, there was a specific treatment for DMs
180180
it.avatarUrl = roomAvatarResolver.resolve(realm, it.roomId)
181-
it.displayName = roomDisplayNameResolver.resolve(realm, it.roomId)
181+
it.setDisplayName(roomDisplayNameResolver.resolve(realm, it.roomId))
182182
}
183183
}
184184

0 commit comments

Comments
 (0)