From ef0b438a89d4774d9c38454ca62bf6febcca5059 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 21 Jan 2020 15:42:08 +0100 Subject: [PATCH] Give the possibility to enable encryption when creating room (#837) --- CHANGES.md | 1 + .../roomdirectory/createroom/CreateRoomAction.kt | 1 + .../createroom/CreateRoomController.kt | 16 +++++++++++++--- .../createroom/CreateRoomFragment.kt | 4 ++++ .../createroom/CreateRoomViewModel.kt | 9 +++++++++ .../createroom/CreateRoomViewState.kt | 1 + vector/src/main/res/values/strings_riotX.xml | 3 +++ 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 54dd5e5247..adca5d9331 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ Changes in RiotX 0.14.0 (2020-XX-XX) Features ✨: - Enable encryption in unencrypted rooms, from the room settings (#212) + - Enable e2e by default when creating DM, and give the possibility to enable encryption when creating room (#837) Improvements 🙌: - Sharing things to RiotX: sort list by recent room first (#771) diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomAction.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomAction.kt index 333834ca3c..8986db180a 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomAction.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomAction.kt @@ -22,5 +22,6 @@ sealed class CreateRoomAction : VectorViewModelAction { data class SetName(val name: String) : CreateRoomAction() data class SetIsPublic(val isPublic: Boolean) : CreateRoomAction() data class SetIsInRoomDirectory(val isInRoomDirectory: Boolean) : CreateRoomAction() + data class SetIsEncrypted(val isEncrypted: Boolean) : CreateRoomAction() object Create : CreateRoomAction() } diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomController.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomController.kt index 2477e6fab0..92e178c628 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomController.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomController.kt @@ -39,9 +39,7 @@ class CreateRoomController @Inject constructor(private val stringProvider: Strin var index = 0 override fun buildModels(viewState: CreateRoomViewState) { - val asyncCreateRoom = viewState.asyncCreateRoomRequest - - when (asyncCreateRoom) { + when (val asyncCreateRoom = viewState.asyncCreateRoomRequest) { is Success -> { // Nothing to display, the screen will be closed } @@ -101,12 +99,24 @@ class CreateRoomController @Inject constructor(private val stringProvider: Strin listener?.setIsInRoomDirectory(value) } } + formSwitchItem { + id("encryption") + enabled(enableFormElement) + title(stringProvider.getString(R.string.create_room_encryption_title)) + summary(stringProvider.getString(R.string.create_room_encryption_description)) + switchChecked(viewState.isEncrypted) + + listener { value -> + listener?.setIsEncrypted(value) + } + } } interface Listener { fun onNameChange(newName: String) fun setIsPublic(isPublic: Boolean) fun setIsInRoomDirectory(isInRoomDirectory: Boolean) + fun setIsEncrypted(isEncrypted: Boolean) fun retry() } } diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt index aacc21916a..827db96783 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomFragment.kt @@ -85,6 +85,10 @@ class CreateRoomFragment @Inject constructor(private val createRoomController: C viewModel.handle(CreateRoomAction.SetIsInRoomDirectory(isInRoomDirectory)) } + override fun setIsEncrypted(isEncrypted: Boolean) { + viewModel.handle(CreateRoomAction.SetIsEncrypted(isEncrypted)) + } + override fun retry() { Timber.v("Retry") viewModel.handle(CreateRoomAction.Create) 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 457574e736..ee170de0e0 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 @@ -31,6 +31,7 @@ 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.riotx.core.platform.EmptyViewEvents +import im.vector.matrix.android.internal.crypto.MXCRYPTO_ALGORITHM_MEGOLM import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity @@ -62,6 +63,7 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr is CreateRoomAction.SetName -> setName(action) is CreateRoomAction.SetIsPublic -> setIsPublic(action) is CreateRoomAction.SetIsInRoomDirectory -> setIsInRoomDirectory(action) + is CreateRoomAction.SetIsEncrypted -> setIsEncrypted(action) is CreateRoomAction.Create -> doCreateRoom() } } @@ -72,6 +74,8 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr private fun setIsInRoomDirectory(action: CreateRoomAction.SetIsInRoomDirectory) = setState { copy(isInRoomDirectory = action.isInRoomDirectory) } + private fun setIsEncrypted(action: CreateRoomAction.SetIsEncrypted) = setState { copy(isEncrypted = action.isEncrypted) } + private fun doCreateRoom() = withState { state -> if (state.asyncCreateRoomRequest is Loading || state.asyncCreateRoomRequest is Success) { return@withState @@ -89,6 +93,11 @@ class CreateRoomViewModel @AssistedInject constructor(@Assisted initialState: Cr // Public room preset = if (state.isPublic) CreateRoomPreset.PRESET_PUBLIC_CHAT else CreateRoomPreset.PRESET_PRIVATE_CHAT + + // Encryption + if (state.isEncrypted) { + enableEncryptionWithAlgorithm(MXCRYPTO_ALGORITHM_MEGOLM) + } } session.createRoom(createRoomParams, object : MatrixCallback { diff --git a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewState.kt b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewState.kt index 363d31edc6..810319d54f 100644 --- a/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewState.kt +++ b/vector/src/main/java/im/vector/riotx/features/roomdirectory/createroom/CreateRoomViewState.kt @@ -24,5 +24,6 @@ data class CreateRoomViewState( val roomName: String = "", val isPublic: Boolean = false, val isInRoomDirectory: Boolean = false, + val isEncrypted: Boolean = false, val asyncCreateRoomRequest: Async = Uninitialized ) : MvRxState diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index 89127d4e25..5fc94d243d 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -3,6 +3,9 @@ + "Enable encryption" + "Once enabled, encryption cannot be disabled." + Your email domain is not authorized to register on this server Messages in this room are not end-to-end encrypted.