From d14f1dd1abff6022d8348f3d550822ece3524367 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 11 May 2020 14:54:41 +0200 Subject: [PATCH] Capability: do not update data if the corresponding request fails --- .../DefaultGetHomeServerCapabilitiesTask.kt | 26 ++++++++++++------- .../homeserver/GetCapabilitiesResult.kt | 4 +-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt index c625324774..6e7d4e5b32 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt @@ -19,7 +19,6 @@ package im.vector.matrix.android.internal.session.homeserver import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.data.Versions import im.vector.matrix.android.api.auth.data.isLoginAndRegistrationSupportedBySdk -import im.vector.matrix.android.api.extensions.orFalse import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilities import im.vector.matrix.android.internal.database.model.HomeServerCapabilitiesEntity import im.vector.matrix.android.internal.database.query.getOrCreate @@ -50,9 +49,11 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( return } - val uploadCapabilities = executeRequest(eventBus) { - apiCall = capabilitiesAPI.getUploadCapabilities() - } + val uploadCapabilities = runCatching { + executeRequest(eventBus) { + apiCall = capabilitiesAPI.getUploadCapabilities() + } + }.getOrNull() val capabilities = runCatching { executeRequest(eventBus) { @@ -66,22 +67,27 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( } }.getOrNull() - insertInDb(capabilities, uploadCapabilities, versions) } private suspend fun insertInDb(getCapabilitiesResult: GetCapabilitiesResult?, - getUploadCapabilitiesResult: GetUploadCapabilitiesResult, + getUploadCapabilitiesResult: GetUploadCapabilitiesResult?, getVersionResult: Versions?) { monarchy.awaitTransaction { realm -> val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) - homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword() + if (getCapabilitiesResult != null) { + homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword() + } - homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize - ?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN + if (getUploadCapabilitiesResult != null) { + homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize + ?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN + } - homeServerCapabilitiesEntity.lastVersionIdentityServerSupported = getVersionResult?.isLoginAndRegistrationSupportedBySdk().orFalse() + if (getVersionResult != null) { + homeServerCapabilitiesEntity.lastVersionIdentityServerSupported = getVersionResult.isLoginAndRegistrationSupportedBySdk() + } homeServerCapabilitiesEntity.lastUpdatedTimestamp = Date().time } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt index ffaa998789..9a625571a8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt @@ -53,6 +53,6 @@ internal data class ChangePassword( ) // The spec says: If not present, the client should assume that password changes are possible via the API -internal fun GetCapabilitiesResult?.canChangePassword(): Boolean { - return this?.capabilities?.changePassword?.enabled.orTrue() +internal fun GetCapabilitiesResult.canChangePassword(): Boolean { + return capabilities?.changePassword?.enabled.orTrue() }