diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapper.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapper.kt index 6ec3a08487..b676d6909d 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapper.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapper.kt @@ -11,23 +11,28 @@ import io.element.android.libraries.matrix.api.core.RoomAlias import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.room.preview.RoomPreview import io.element.android.libraries.matrix.impl.room.toRoomType +import org.matrix.rustcomponents.sdk.JoinRule +import org.matrix.rustcomponents.sdk.Membership import org.matrix.rustcomponents.sdk.RoomPreview as RustRoomPreview object RoomPreviewMapper { fun map(roomPreview: RustRoomPreview): RoomPreview { - return RoomPreview( - roomId = RoomId(roomPreview.roomId), - canonicalAlias = roomPreview.canonicalAlias?.let(::RoomAlias), - name = roomPreview.name, - topic = roomPreview.topic, - avatarUrl = roomPreview.avatarUrl, - numberOfJoinedMembers = roomPreview.numJoinedMembers.toLong(), - roomType = roomPreview.roomType.toRoomType(), - isHistoryWorldReadable = roomPreview.isHistoryWorldReadable, - isJoined = roomPreview.isJoined, - isInvited = roomPreview.isInvited, - isPublic = roomPreview.isPublic, - canKnock = roomPreview.canKnock - ) + return roomPreview.use { + val info = roomPreview.info() + RoomPreview( + roomId = RoomId(info.roomId), + canonicalAlias = info.canonicalAlias?.let(::RoomAlias), + name = info.name, + topic = info.topic, + avatarUrl = info.avatarUrl, + numberOfJoinedMembers = info.numJoinedMembers.toLong(), + roomType = info.roomType.toRoomType(), + isHistoryWorldReadable = info.isHistoryWorldReadable, + isJoined = info.membership == Membership.JOINED, + isInvited = info.membership == Membership.INVITED, + isPublic = info.joinRule == JoinRule.Public, + canKnock = info.joinRule == JoinRule.Knock + ) + } } } diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreview.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreviewInfo.kt similarity index 62% rename from libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreview.kt rename to libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreviewInfo.kt index 54abeb90b8..0f344d3de0 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreview.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/factories/RoomPreviewInfo.kt @@ -9,16 +9,16 @@ package io.element.android.libraries.matrix.impl.fixtures.factories import io.element.android.libraries.matrix.test.A_ROOM_ALIAS import io.element.android.libraries.matrix.test.A_ROOM_ID -import org.matrix.rustcomponents.sdk.RoomPreview +import org.matrix.rustcomponents.sdk.JoinRule +import org.matrix.rustcomponents.sdk.Membership +import org.matrix.rustcomponents.sdk.RoomPreviewInfo -internal fun aRustRoomPreview( +internal fun aRustRoomPreviewInfo( canonicalAlias: String? = A_ROOM_ALIAS.value, - isJoined: Boolean = true, - isInvited: Boolean = true, - isPublic: Boolean = true, - canKnock: Boolean = true, -): RoomPreview { - return RoomPreview( + membership: Membership? = Membership.JOINED, + joinRule: JoinRule = JoinRule.Public, +): RoomPreviewInfo { + return RoomPreviewInfo( roomId = A_ROOM_ID.value, canonicalAlias = canonicalAlias, name = "name", @@ -27,9 +27,7 @@ internal fun aRustRoomPreview( numJoinedMembers = 1u, roomType = null, isHistoryWorldReadable = true, - isJoined = isJoined, - isInvited = isInvited, - isPublic = isPublic, - canKnock = canKnock, + membership = membership, + joinRule = joinRule, ) } diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustRoomPreview.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustRoomPreview.kt new file mode 100644 index 0000000000..fda57cd329 --- /dev/null +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustRoomPreview.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.libraries.matrix.impl.fixtures.fakes + +import io.element.android.libraries.matrix.impl.fixtures.factories.aRustRoomPreviewInfo +import org.matrix.rustcomponents.sdk.NoPointer +import org.matrix.rustcomponents.sdk.RoomPreview +import org.matrix.rustcomponents.sdk.RoomPreviewInfo + +class FakeRustRoomPreview( + private val info: RoomPreviewInfo = aRustRoomPreviewInfo(), +) : RoomPreview(NoPointer) { + override fun info(): RoomPreviewInfo { + return info + } +} diff --git a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapperTest.kt b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapperTest.kt index 7d7890198e..1dc03bc49b 100644 --- a/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapperTest.kt +++ b/libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/room/preview/RoomPreviewMapperTest.kt @@ -10,19 +10,23 @@ package io.element.android.libraries.matrix.impl.room.preview import com.google.common.truth.Truth.assertThat import io.element.android.libraries.matrix.api.room.RoomType import io.element.android.libraries.matrix.api.room.preview.RoomPreview -import io.element.android.libraries.matrix.impl.fixtures.factories.aRustRoomPreview +import io.element.android.libraries.matrix.impl.fixtures.factories.aRustRoomPreviewInfo +import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeRustRoomPreview import io.element.android.libraries.matrix.test.A_ROOM_ALIAS import io.element.android.libraries.matrix.test.A_ROOM_ID import org.junit.Test +import org.matrix.rustcomponents.sdk.JoinRule +import org.matrix.rustcomponents.sdk.Membership class RoomPreviewMapperTest { @Test fun `map should map values 1`() { assertThat( RoomPreviewMapper.map( - aRustRoomPreview( - isJoined = false, - isInvited = false, + FakeRustRoomPreview( + info = aRustRoomPreviewInfo( + membership = null, + ) ) ) ).isEqualTo( @@ -38,7 +42,7 @@ class RoomPreviewMapperTest { isJoined = false, isInvited = false, isPublic = true, - canKnock = true, + canKnock = false, ) ) } @@ -47,10 +51,12 @@ class RoomPreviewMapperTest { fun `map should map values 2`() { assertThat( RoomPreviewMapper.map( - aRustRoomPreview( - canonicalAlias = null, - isPublic = false, - canKnock = false, + FakeRustRoomPreview( + info = aRustRoomPreviewInfo( + canonicalAlias = null, + membership = Membership.JOINED, + joinRule = JoinRule.Knock, + ) ) ) ).isEqualTo( @@ -64,9 +70,9 @@ class RoomPreviewMapperTest { roomType = RoomType.Room, isHistoryWorldReadable = true, isJoined = true, - isInvited = true, + isInvited = false, isPublic = false, - canKnock = false, + canKnock = true, ) ) }