diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/securityandprivacy/editroomaddress/EditRoomAddressPresenter.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/securityandprivacy/editroomaddress/EditRoomAddressPresenter.kt index caaf4c373d..3402e2f174 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/securityandprivacy/editroomaddress/EditRoomAddressPresenter.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/securityandprivacy/editroomaddress/EditRoomAddressPresenter.kt @@ -107,15 +107,24 @@ class EditRoomAddressPresenter @AssistedInject constructor( if (savedAliasFromHomeserver != null) { room.removeRoomAliasFromRoomDirectory(savedAliasFromHomeserver).getOrThrow() } - // Finally update the canonical alias state.. + + // Finally update the canonical alias state when { // Allow to update the canonical alias only if the saved canonical alias matches the homeserver or if there is no canonical alias savedCanonicalAlias == null || savedCanonicalAlias.matchesServer(serverName) -> { - room.updateCanonicalAlias(newRoomAlias, room.alternativeAliases).getOrThrow() + val newAlternativeAliases = room.alternativeAliases.filter { it != savedAliasFromHomeserver } + room.updateCanonicalAlias(newRoomAlias, newAlternativeAliases).getOrThrow() } - // Otherwise, update the alternative aliases and keep the current canonical alias + // Otherwise, only update the alternative aliases and keep the current canonical alias else -> { - val newAlternativeAliases = listOf(newRoomAlias) + room.alternativeAliases + val newAlternativeAliases = buildList { + add(newRoomAlias) + for (alias in room.alternativeAliases) { + if (alias != savedAliasFromHomeserver) { + add(alias) + } + } + } room.updateCanonicalAlias(savedCanonicalAlias, newAlternativeAliases).getOrThrow() } }