|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package im.vector.app.features.analytics |
| 18 | + |
| 19 | +import im.vector.app.features.analytics.plan.Error |
| 20 | +import org.matrix.android.sdk.api.session.crypto.MXCryptoError |
| 21 | + |
| 22 | +data class DecryptionFailure( |
| 23 | + val timeStamp: Long, |
| 24 | + val roomId: String, |
| 25 | + val failedEventId: String, |
| 26 | + val error: MXCryptoError, |
| 27 | + val wasVisibleOnScreen: Boolean, |
| 28 | + val ownIdentityTrustedAtTimeOfDecryptionFailure: Boolean, |
| 29 | + // If this is set, it means that the event was decrypted but late. Will be -1 if |
| 30 | + // the event was not decrypted after the maximum wait time. |
| 31 | + val timeToDecryptMillis: Long? = null, |
| 32 | + val isMatrixDotOrg: Boolean, |
| 33 | + val isFederated: Boolean? = null, |
| 34 | + val eventLocalAgeAtDecryptionFailure: Long? = null |
| 35 | +) |
| 36 | + |
| 37 | +fun DecryptionFailure.toAnalyticsEvent(): Error { |
| 38 | + val errorMsg = (error as? MXCryptoError.Base)?.technicalMessage ?: error.message |
| 39 | + return Error( |
| 40 | + context = "mxc_crypto_error_type|${errorMsg}", |
| 41 | + domain = Error.Domain.E2EE, |
| 42 | + name = this.toAnalyticsErrorName(), |
| 43 | + // this is deprecated keep for backward compatibility |
| 44 | + cryptoModule = Error.CryptoModule.Rust, |
| 45 | + cryptoSDK = Error.CryptoSDK.Rust, |
| 46 | + eventLocalAgeMillis = eventLocalAgeAtDecryptionFailure?.toInt(), |
| 47 | + isFederated = isFederated, |
| 48 | + isMatrixDotOrg = isMatrixDotOrg, |
| 49 | + timeToDecryptMillis = timeToDecryptMillis?.toInt() ?: -1, |
| 50 | + wasVisibleToUser = wasVisibleOnScreen, |
| 51 | + userTrustsOwnIdentity = ownIdentityTrustedAtTimeOfDecryptionFailure, |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +private fun DecryptionFailure.toAnalyticsErrorName(): Error.Name { |
| 56 | + val error = this.error |
| 57 | + val name = if (error is MXCryptoError.Base) { |
| 58 | + when (error.errorType) { |
| 59 | + MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID, |
| 60 | + MXCryptoError.ErrorType.KEYS_WITHHELD -> Error.Name.OlmKeysNotSentError |
| 61 | + MXCryptoError.ErrorType.OLM -> Error.Name.OlmUnspecifiedError |
| 62 | + MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX -> Error.Name.OlmIndexError |
| 63 | + else -> Error.Name.UnknownError |
| 64 | + } |
| 65 | + } else { |
| 66 | + Error.Name.UnknownError |
| 67 | + } |
| 68 | + // check if it's an expected UTD! |
| 69 | + val localAge = this.eventLocalAgeAtDecryptionFailure |
| 70 | + val isHistorical = localAge != null && localAge < 0 |
| 71 | + if (isHistorical && !this.ownIdentityTrustedAtTimeOfDecryptionFailure) { |
| 72 | + return Error.Name.HistoricalMessage |
| 73 | + } |
| 74 | + |
| 75 | + return name |
| 76 | +} |
0 commit comments