From e73f138151965f357157406f3a5175758c83c6be Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 26 Feb 2020 07:17:25 +0100 Subject: [PATCH 1/5] Improve CreateRoomParams API: update Javadoc and ensure the return values will not be discarded (#1070) --- CHANGES.md | 1 + .../room/model/create/CreateRoomParams.kt | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ffbaa42b3b..7d62e54caa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -28,6 +28,7 @@ Translations 🗣: SDK API changes ⚠️: - Get crypto methods through Session.cryptoService() - ProgressListener.onProgress() function will be invoked on the background thread instead of UI thread + - Improve CreateRoomParams API (#1070) Build 🧱: - 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 b69c189f89..536bf16dbc 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 @@ -17,6 +17,7 @@ package im.vector.matrix.android.api.session.room.model.create import android.util.Patterns +import androidx.annotation.CheckResult import com.squareup.moshi.Json import com.squareup.moshi.JsonClass import im.vector.matrix.android.api.MatrixPatterns.isUserId @@ -120,14 +121,15 @@ data class CreateRoomParams( @Json(name = "power_level_content_override") val powerLevelContentOverride: PowerLevelsContent? = null ) { - /** - * Set to true means that if cross-signing is enabled and we can get keys for every invited users, - * the encryption will be enabled on the created room - */ @Transient internal var enableEncryptionIfInvitedUsersSupportIt: Boolean = false private set + /** + * 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 + * @return this, to allow chaining methods + */ fun enableEncryptionIfInvitedUsersSupportIt(): CreateRoomParams { enableEncryptionIfInvitedUsersSupportIt = true return this @@ -137,7 +139,9 @@ data class CreateRoomParams( * Add the crypto algorithm to the room creation parameters. * * @param algorithm the algorithm + * @return a modified copy of the CreateRoomParams object, or this if there is no modification */ + @CheckResult fun enableEncryptionWithAlgorithm(algorithm: String = MXCRYPTO_ALGORITHM_MEGOLM): CreateRoomParams { return if (algorithm == MXCRYPTO_ALGORITHM_MEGOLM) { val contentMap = mapOf("algorithm" to algorithm) @@ -161,7 +165,9 @@ data class CreateRoomParams( * Force the history visibility in the room creation parameters. * * @param historyVisibility the expected history visibility, set null to remove any existing value. + * @return a modified copy of the CreateRoomParams object */ + @CheckResult fun setHistoryVisibility(historyVisibility: RoomHistoryVisibility?): CreateRoomParams { // Remove the existing value if any. val newInitialStates = initialStates @@ -187,7 +193,9 @@ data class CreateRoomParams( /** * Mark as a direct message room. + * @return a modified copy of the CreateRoomParams object */ + @CheckResult fun setDirectMessage(): CreateRoomParams { return copy( preset = CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT, @@ -217,6 +225,7 @@ data class CreateRoomParams( fun isDirect(): Boolean { return preset == CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT && isDirect == true + // TODO This test is not ok && (1 == getInviteCount() || 1 == getInvite3PidCount()) } @@ -232,7 +241,9 @@ data class CreateRoomParams( * ids might be a matrix id or an email address. * * @param ids the participant ids to add. + * @return a modified copy of the CreateRoomParams object */ + @CheckResult fun addParticipantIds(hsConfig: HomeServerConnectionConfig, userId: String, ids: List): CreateRoomParams { From fc95bf492624d2f2c39702767c6f4f9898e5e8e8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 26 Feb 2020 07:32:22 +0100 Subject: [PATCH 2/5] 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) { From 67180fd8db3eafd3c0b3bd0786f05a4b54f9878b Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 26 Feb 2020 07:38:10 +0100 Subject: [PATCH 3/5] New direct chat: selecting several participants was not adding the room to the direct chats list --- CHANGES.md | 1 + .../room/model/create/CreateRoomParams.kt | 16 ---------------- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7d62e54caa..37b6c63393 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,7 @@ Bugfix 🐛: - Leaving a room creates a stuck "leaving room" loading screen. (#1041) - Fix some invitation handling issues (#1013) - New direct chat: selecting a participant sometimes results in two breadcrumbs (#1022) + - New direct chat: selecting several participants was not adding the room to the direct chats list Translations 🗣: - 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 af2a5bf1e0..1abbe9ef3a 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 @@ -216,20 +216,6 @@ data class CreateRoomParams( ) } - /** - * @return the invite count - */ - private fun getInviteCount(): Int { - return invitedUserIds?.size ?: 0 - } - - /** - * @return the pid invite count - */ - private fun getInvite3PidCount(): Int { - return invite3pids?.size ?: 0 - } - /** * Tells if the created room can be a direct chat one. * @@ -238,8 +224,6 @@ data class CreateRoomParams( fun isDirect(): Boolean { return preset == CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT && isDirect == true - // TODO This test is not ok - && (1 == getInviteCount() || 1 == getInvite3PidCount()) } /** From 076ac23b38372b94fdbb0ba466b1ee91e7d8346c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 26 Feb 2020 07:59:54 +0100 Subject: [PATCH 4/5] Modification done by AndroidStudio 3.6 --- .idea/codeStyles/Project.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 40ee4ee5cf..b3719669fd 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,9 +1,6 @@