From f48d4c021a36e9b07ddb5faeb55ef0570f913aa1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 29 Oct 2020 14:10:41 +0100 Subject: [PATCH] Cleanup and split long lines --- .../crypto/store/db/RealmCryptoStore.kt | 4 +- .../settings/devtools/KeyRequestViewModel.kt | 129 +++++++++--------- .../settings/devtools/KeyRequestsFragment.kt | 13 +- 3 files changed, 70 insertions(+), 76 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 7644ab5cc2..5d19e6d607 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -1124,7 +1124,7 @@ internal class RealmCryptoStore @Inject constructor( content = ContentMapper.map(event.content) ).apply { sendState = SendState.SYNCED - decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) + decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) decryptionErrorCode = event.mCryptoError?.name } realm.insertOrUpdate(entity) @@ -1143,7 +1143,7 @@ internal class RealmCryptoStore @Inject constructor( content = ContentMapper.map(event.content) ).apply { sendState = SendState.SYNCED - decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) + decryptionResultJson = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).toJson(event.mxDecryptionResult) decryptionErrorCode = event.mCryptoError?.name } realm.insertOrUpdate(entity) diff --git a/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestViewModel.kt index aca374afd9..e64f673309 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestViewModel.kt @@ -29,6 +29,7 @@ import com.airbnb.mvrx.Uninitialized import com.airbnb.mvrx.ViewModelContext import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject +import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.VectorViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.platform.VectorViewModelAction @@ -54,7 +55,7 @@ sealed class KeyRequestEvents : VectorViewEvents { } data class KeyRequestViewState( - val exporting: Async = Uninitialized + val exporting: Async = Uninitialized ) : MvRxState class KeyRequestViewModel @AssistedInject constructor( @@ -78,77 +79,75 @@ class KeyRequestViewModel @AssistedInject constructor( override fun handle(action: KeyRequestAction) { when (action) { - is KeyRequestAction.ExportAudit -> { - setState { - copy(exporting = Loading()) - } - viewModelScope.launch(Dispatchers.IO) { - try { - // this can take long - val eventList = session.cryptoService().getGossipingEvents() - // clean it a bit to - val stringBuilder = StringBuilder() - eventList.forEach { - val clearType = it.getClearType() - stringBuilder.append("[${it.ageLocalTs}] : $clearType from:${it.senderId} - ") - when (clearType) { - EventType.ROOM_KEY_REQUEST -> { - val content = it.getClearContent().toModel() - stringBuilder.append("reqId:${content?.requestId} action:${content?.action} ") - if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) { - stringBuilder.append("sessionId: ${content.body?.sessionId} ") - } - stringBuilder.append("requestedBy: ${content?.requestingDeviceId} ") - stringBuilder.append("\n") - } - EventType.FORWARDED_ROOM_KEY -> { - val encryptedContent = it.content.toModel() - val content = it.getClearContent().toModel() + is KeyRequestAction.ExportAudit -> exportAudit(action) + }.exhaustive + } - stringBuilder.append("sessionId:${content?.sessionId} From Device (sender key):${encryptedContent?.senderKey} ") - span("\nFrom Device (sender key):") { - textStyle = "bold" - } - stringBuilder.append("\n") + private fun exportAudit(action: KeyRequestAction.ExportAudit) { + setState { + copy(exporting = Loading()) + } + viewModelScope.launch(Dispatchers.IO) { + try { + // this can take long + val eventList = session.cryptoService().getGossipingEvents() + // clean it a bit to + val raw = buildString { + eventList.forEach { + val clearType = it.getClearType() + append("[${it.ageLocalTs}] : $clearType from:${it.senderId} - ") + when (clearType) { + EventType.ROOM_KEY_REQUEST -> { + val content = it.getClearContent().toModel() + append("reqId:${content?.requestId} action:${content?.action} ") + if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) { + append("sessionId: ${content.body?.sessionId} ") } - EventType.ROOM_KEY -> { - val content = it.getClearContent() - stringBuilder.append("sessionId:${content?.get("session_id")} roomId:${content?.get("room_id")} dest:${content?.get("_dest") ?: "me"}") - stringBuilder.append("\n") - } - EventType.SEND_SECRET -> { - val content = it.getClearContent().toModel() - stringBuilder.append("requestId:${content?.requestId} From Device:${it.mxDecryptionResult?.payload?.get("sender_device")}") - stringBuilder.append("\n") - } - EventType.REQUEST_SECRET -> { - val content = it.getClearContent().toModel() - stringBuilder.append("reqId:${content?.requestId} action:${content?.action} ") - if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) { - stringBuilder.append("secretName:${content.secretName} ") - } - stringBuilder.append("requestedBy:${content?.requestingDeviceId}") - stringBuilder.append("\n") - } - EventType.ENCRYPTED -> { - stringBuilder.append("Failed to Derypt \n") - } - else -> { - stringBuilder.append("?? \n") + append("requestedBy: ${content?.requestingDeviceId}") + } + EventType.FORWARDED_ROOM_KEY -> { + val encryptedContent = it.content.toModel() + val content = it.getClearContent().toModel() + + append("sessionId:${content?.sessionId} From Device (sender key):${encryptedContent?.senderKey}") + span("\nFrom Device (sender key):") { + textStyle = "bold" } } + EventType.ROOM_KEY -> { + val content = it.getClearContent() + append("sessionId:${content?.get("session_id")} roomId:${content?.get("room_id")} dest:${content?.get("_dest") ?: "me"}") + } + EventType.SEND_SECRET -> { + val content = it.getClearContent().toModel() + append("requestId:${content?.requestId} From Device:${it.mxDecryptionResult?.payload?.get("sender_device")}") + } + EventType.REQUEST_SECRET -> { + val content = it.getClearContent().toModel() + append("reqId:${content?.requestId} action:${content?.action} ") + if (content?.action == GossipingToDeviceObject.ACTION_SHARE_REQUEST) { + append("secretName:${content.secretName} ") + } + append("requestedBy:${content?.requestingDeviceId}") + } + EventType.ENCRYPTED -> { + append("Failed to Decrypt") + } + else -> { + append("??") + } } - val raw = stringBuilder.toString() - setState { - copy(exporting = Success("")) - } - _viewEvents.post(KeyRequestEvents.SaveAudit(action.uri, raw)) - } catch (error: Throwable) { - setState { - copy(exporting = Fail(error)) - } + append("\n") } } + setState { + copy(exporting = Success(Unit)) + } + _viewEvents.post(KeyRequestEvents.SaveAudit(action.uri, raw)) + } catch (error: Throwable) { + setState { + copy(exporting = Fail(error)) + } } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestsFragment.kt index 3f4d24c6ea..ff91d9bba3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devtools/KeyRequestsFragment.kt @@ -30,6 +30,7 @@ import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState import com.google.android.material.tabs.TabLayoutMediator import im.vector.app.R +import im.vector.app.core.extensions.exhaustive import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.platform.VectorBaseFragment @@ -103,17 +104,11 @@ class KeyRequestsFragment @Inject constructor( when (it) { is KeyRequestEvents.SaveAudit -> { tryOrNull { - val os = requireContext().contentResolver?.openOutputStream(it.uri) - if (os == null) { - false - } else { - os.write(it.raw.toByteArray()) - os.flush() - true - } + requireContext().contentResolver?.openOutputStream(it.uri) + ?.use { os -> os.write(it.raw.toByteArray()) } } } - } + }.exhaustive } }