From fc95bf492624d2f2c39702767c6f4f9898e5e8e8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 26 Feb 2020 07:32:22 +0100 Subject: [PATCH] Improve CreateRoomParams API: update some API for better chaining of builder like methods (#1070) --- .../room/model/create/CreateRoomParams.kt | 39 ++++++++++++------- .../createroom/CreateRoomViewModel.kt | 9 ++--- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomParams.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomParams.kt index 536bf16dbc..af2a5bf1e0 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomParams.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomParams.kt @@ -128,33 +128,46 @@ data class CreateRoomParams( /** * After calling this method, when the room will be created, if cross-signing is enabled and we can get keys for every invited users, * the encryption will be enabled on the created room + * @param value true to activate this behavior. * @return this, to allow chaining methods */ - fun enableEncryptionIfInvitedUsersSupportIt(): CreateRoomParams { - enableEncryptionIfInvitedUsersSupportIt = true + fun enableEncryptionIfInvitedUsersSupportIt(value: Boolean = true): CreateRoomParams { + enableEncryptionIfInvitedUsersSupportIt = value return this } /** * Add the crypto algorithm to the room creation parameters. * - * @param algorithm the algorithm + * @param enable true to enable encryption. + * @param algorithm the algorithm, default to [MXCRYPTO_ALGORITHM_MEGOLM], which is actually the only supported algorithm for the moment * @return a modified copy of the CreateRoomParams object, or this if there is no modification */ @CheckResult - fun enableEncryptionWithAlgorithm(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM): CreateRoomParams { + fun enableEncryptionWithAlgorithm(enable: Boolean = true, + algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM): CreateRoomParams { + // Remove the existing value if any. + val newInitialStates = initialStates + ?.filter { it.type != EventType.STATE_ROOM_ENCRYPTION } + return if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) { - val contentMap = mapOf("algorithm" to algorithm) + if (enable) { + val contentMap = mapOf("algorithm" to algorithm) - val algoEvent = Event( - type = EventType.STATE_ROOM_ENCRYPTION, - stateKey = "", - content = contentMap.toContent() - ) + val algoEvent = Event( + type = EventType.STATE_ROOM_ENCRYPTION, + stateKey = "", + content = contentMap.toContent() + ) - copy( - initialStates = initialStates.orEmpty().filter { it.type != EventType.STATE_ROOM_ENCRYPTION } + algoEvent - ) + copy( + initialStates = newInitialStates.orEmpty() + algoEvent + ) + } else { + return copy( + initialStates = newInitialStates + ) + } } else { Timber.e("Unsupported algorithm: $algorithm") this diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt index 6c750af5ac..d5a290c1f7 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -30,7 +30,6 @@ import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.room.model.RoomDirectoryVisibility import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams import im.vector.matrix.android.api.session.room.model.create.CreateRoomPreset -import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM import im.vector.riotx.core.platform.EmptyViewEvents import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity @@ -91,10 +90,10 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr visibility = if (state.isInRoomDirectory) RoomDirectoryVisibility.PUBLIC else RoomDirectoryVisibility.PRIVATE, // Public room preset = if (state.isPublic) CreateRoomPreset.PRESET_PUBLIC_CHAT else CreateRoomPreset.PRESET_PRIVATE_CHAT - ).let { - // Encryption - if (state.isEncrypted) it.enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM) else it - } + ) + // Encryption + .enableEncryptionWithAlgorithm(state.isEncrypted) + session.createRoom(createRoomParams, object : MatrixCallback { override fun onSuccess(data: String) {