From 97dc07f8c9ad7797c4e774000fd722fb3795ad81 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 27 Sep 2021 09:52:54 +0200 Subject: [PATCH 1/2] Fix default encrypted for restricted + hide restricted rule if no current space selected --- changelog.d/4045.bugfix | 1 + .../createroom/CreateRoomController.kt | 6 ++++- .../createroom/CreateRoomFragment.kt | 5 +++-- .../createroom/CreateRoomViewModel.kt | 22 +++++++++++++++---- .../createroom/CreateRoomViewState.kt | 5 +++-- 5 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 changelog.d/4045.bugfix diff --git a/changelog.d/4045.bugfix b/changelog.d/4045.bugfix new file mode 100644 index 0000000000..c6798ae492 --- /dev/null +++ b/changelog.d/4045.bugfix @@ -0,0 +1 @@ +Align new room encryption default to Web \ No newline at end of file diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt index 2676096b6b..0d3066e43a 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt @@ -165,7 +165,11 @@ class CreateRoomController @Inject constructor( host.stringProvider.getString(R.string.create_room_encryption_description) } ) - switchChecked(viewState.isEncrypted) + if (viewState.isEncrypted != null) { + switchChecked(viewState.isEncrypted) + } else { + switchChecked(viewState.defaultEncrypted[viewState.roomJoinRules] ?: false) + } listener { value -> host.listener?.setIsEncrypted(value) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt index 70f041bd69..ac4c9db89f 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomFragment.kt @@ -163,8 +163,9 @@ class CreateRoomFragment @Inject constructor( } override fun selectVisibility() = withState(viewModel) { state -> - - val allowed = if (state.supportsRestricted) { + // If restricted is supported and the user is in the context of a parent space + // then show restricted option. + val allowed = if (state.supportsRestricted && state.parentSpaceId != null) { listOf(RoomJoinRules.INVITE, RoomJoinRules.PUBLIC, RoomJoinRules.RESTRICTED) } else { listOf(RoomJoinRules.INVITE, RoomJoinRules.PUBLIC) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt index 9a9812933b..45edc207ba 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -109,8 +109,13 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted private val init setState { copy( - isEncrypted = RoomJoinRules.INVITE == roomJoinRules && adminE2EByDefault, - hsAdminHasDisabledE2E = !adminE2EByDefault + hsAdminHasDisabledE2E = !adminE2EByDefault, + defaultEncrypted = mapOf( + RoomJoinRules.INVITE to adminE2EByDefault, + RoomJoinRules.PUBLIC to false, + RoomJoinRules.RESTRICTED to adminE2EByDefault + ) + ) } } @@ -286,8 +291,17 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted private val init disableFederation = state.disableFederation // Encryption - if (state.isEncrypted) { - enableEncryption() + // we ignore the isEncrypted for public room as the switch is hidden in this case + if (state.roomJoinRules != RoomJoinRules.PUBLIC && state.isEncrypted != null) { + // the user explicitly switch the toggle + if (state.isEncrypted) { + enableEncryption() + } + } else { + // based on default + if (state.defaultEncrypted[state.roomJoinRules] == true) { + enableEncryption() + } } } diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt index db56a19904..528adb6c63 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt @@ -28,7 +28,7 @@ data class CreateRoomViewState( val roomName: String = "", val roomTopic: String = "", val roomJoinRules: RoomJoinRules = RoomJoinRules.INVITE, - val isEncrypted: Boolean = false, + val isEncrypted: Boolean? = null, val showAdvanced: Boolean = false, val disableFederation: Boolean = false, val homeServerName: String = "", @@ -38,7 +38,8 @@ data class CreateRoomViewState( val parentSpaceSummary: RoomSummary? = null, val supportsRestricted: Boolean = false, val aliasLocalPart: String? = null, - val isSubSpace: Boolean = false + val isSubSpace: Boolean = false, + val defaultEncrypted: Map = emptyMap() ) : MvRxState { constructor(args: CreateRoomArgs) : this( From 2605433a3de7a92081047f44bb12aafe233f9c6e Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 30 Sep 2021 09:15:13 +0200 Subject: [PATCH 2/2] Code review --- .../createroom/CreateRoomController.kt | 8 +++----- .../createroom/CreateRoomViewModel.kt | 19 ++++++++----------- .../createroom/CreateRoomViewState.kt | 4 ++-- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt index 0d3066e43a..a6799ce730 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomController.kt @@ -29,6 +29,7 @@ import im.vector.app.features.form.formEditTextItem import im.vector.app.features.form.formEditableAvatarItem import im.vector.app.features.form.formSubmitButtonItem import im.vector.app.features.form.formSwitchItem +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure import org.matrix.android.sdk.api.session.room.model.RoomJoinRules import javax.inject.Inject @@ -165,11 +166,8 @@ class CreateRoomController @Inject constructor( host.stringProvider.getString(R.string.create_room_encryption_description) } ) - if (viewState.isEncrypted != null) { - switchChecked(viewState.isEncrypted) - } else { - switchChecked(viewState.defaultEncrypted[viewState.roomJoinRules] ?: false) - } + + switchChecked(viewState.isEncrypted ?: viewState.defaultEncrypted[viewState.roomJoinRules].orFalse()) listener { value -> host.listener?.setIsEncrypted(value) diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt index 45edc207ba..8ddb6d7a09 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewModel.kt @@ -36,6 +36,7 @@ import im.vector.app.features.settings.VectorPreferences import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.matrix.android.sdk.api.MatrixPatterns.getDomain +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.session.Session @@ -291,17 +292,13 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted private val init disableFederation = state.disableFederation // Encryption - // we ignore the isEncrypted for public room as the switch is hidden in this case - if (state.roomJoinRules != RoomJoinRules.PUBLIC && state.isEncrypted != null) { - // the user explicitly switch the toggle - if (state.isEncrypted) { - enableEncryption() - } - } else { - // based on default - if (state.defaultEncrypted[state.roomJoinRules] == true) { - enableEncryption() - } + val shouldEncrypt = when (state.roomJoinRules) { + // we ignore the isEncrypted for public room as the switch is hidden in this case + RoomJoinRules.PUBLIC -> false + else -> state.isEncrypted ?: state.defaultEncrypted[state.roomJoinRules].orFalse() + } + if (shouldEncrypt) { + enableEncryption() } } diff --git a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt index 528adb6c63..95edd1efbe 100644 --- a/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt +++ b/vector/src/main/java/im/vector/app/features/roomdirectory/createroom/CreateRoomViewState.kt @@ -29,6 +29,7 @@ data class CreateRoomViewState( val roomTopic: String = "", val roomJoinRules: RoomJoinRules = RoomJoinRules.INVITE, val isEncrypted: Boolean? = null, + val defaultEncrypted: Map = emptyMap(), val showAdvanced: Boolean = false, val disableFederation: Boolean = false, val homeServerName: String = "", @@ -38,8 +39,7 @@ data class CreateRoomViewState( val parentSpaceSummary: RoomSummary? = null, val supportsRestricted: Boolean = false, val aliasLocalPart: String? = null, - val isSubSpace: Boolean = false, - val defaultEncrypted: Map = emptyMap() + val isSubSpace: Boolean = false ) : MvRxState { constructor(args: CreateRoomArgs) : this(