From d240e3c453804d260aa81193c1e6945d147d8016 Mon Sep 17 00:00:00 2001
From: ariskotsomitopoulos <aris.kotsomitopoulos@gmail.com>
Date: Wed, 20 Apr 2022 16:40:12 +0300
Subject: [PATCH 1/5] Enhance crypto posthog errors with more details

---
 .../analytics/DecryptionFailureTracker.kt     | 20 +++++++++----------
 .../timeline/factory/TimelineItemFactory.kt   |  1 +
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
index 6b2ceb1444..308077810d 100644
--- a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
+++ b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
@@ -29,6 +29,7 @@ import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import org.matrix.android.sdk.api.session.crypto.MXCryptoError
 import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
+import timber.log.Timber
 import javax.inject.Inject
 import javax.inject.Singleton
 
@@ -38,7 +39,7 @@ private data class DecryptionFailure(
         val failedEventId: String,
         val error: MXCryptoError.ErrorType
 )
-
+private typealias DetailedErrorName  = Pair<String, Error.Name>
 private const val GRACE_PERIOD_MILLIS = 4_000
 private const val CHECK_INTERVAL = 2_000L
 
@@ -112,7 +113,7 @@ class DecryptionFailureTracker @Inject constructor(
 
     private fun checkFailures() {
         val now = clock.epochMillis()
-        val aggregatedErrors: Map<Error.Name, List<String>>
+        val aggregatedErrors: Map<DetailedErrorName, List<String>>
         synchronized(failures) {
             val toReport = mutableListOf<DecryptionFailure>()
             failures.removeAll { failure ->
@@ -136,20 +137,19 @@ class DecryptionFailureTracker @Inject constructor(
                     // for now we ignore events already reported even if displayed again?
                     .filter { alreadyReported.contains(it).not() }
                     .forEach { failedEventId ->
-                        analyticsTracker.capture(Error(failedEventId, Error.Domain.E2EE, aggregation.key))
+                        analyticsTracker.capture(Error(aggregation.key.first, Error.Domain.E2EE, aggregation.key.second))
                         alreadyReported.add(failedEventId)
                     }
         }
     }
 
-    private fun MXCryptoError.ErrorType.toAnalyticsErrorName(): Error.Name {
+    private fun MXCryptoError.ErrorType.toAnalyticsErrorName(): DetailedErrorName {
+        val detailed = "$name | mxc_crypto_error_type"
         return when (this) {
-            MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID -> Error.Name.OlmKeysNotSentError
-            MXCryptoError.ErrorType.OLM                        -> {
-                Error.Name.OlmUnspecifiedError
-            }
-            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX      -> Error.Name.OlmIndexError
-            else                                               -> Error.Name.UnknownError
+            MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID -> Pair(detailed, Error.Name.OlmKeysNotSentError)
+            MXCryptoError.ErrorType.OLM                        -> Pair(detailed, Error.Name.OlmUnspecifiedError)
+            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX      -> Pair(detailed, Error.Name.OlmIndexError)
+            else                                               -> Pair(detailed, Error.Name.UnknownError)
         }
     }
 }
diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
index b5d620658e..45ab4e4ba3 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
@@ -138,6 +138,7 @@ class TimelineItemFactory @Inject constructor(
                     }
                 }.also {
                     if (it != null && event.isEncrypted()) {
+//                        Timber.i("----> ${event.eventId} encryption error found")
                         decryptionFailureTracker.e2eEventDisplayedInTimeline(event)
                     }
                 }

From cf8031bbc64d2c0947655180c1b5adbddd7a4e25 Mon Sep 17 00:00:00 2001
From: ariskotsomitopoulos <aris.kotsomitopoulos@gmail.com>
Date: Thu, 21 Apr 2022 17:14:25 +0300
Subject: [PATCH 2/5] Remove unused comment

---
 .../home/room/detail/timeline/factory/TimelineItemFactory.kt     | 1 -
 1 file changed, 1 deletion(-)

diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
index 45ab4e4ba3..b5d620658e 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/TimelineItemFactory.kt
@@ -138,7 +138,6 @@ class TimelineItemFactory @Inject constructor(
                     }
                 }.also {
                     if (it != null && event.isEncrypted()) {
-//                        Timber.i("----> ${event.eventId} encryption error found")
                         decryptionFailureTracker.e2eEventDisplayedInTimeline(event)
                     }
                 }

From cc705d5458bd288e4335b67c7d2a7c72f43a51a9 Mon Sep 17 00:00:00 2001
From: ariskotsomitopoulos <aris.kotsomitopoulos@gmail.com>
Date: Thu, 21 Apr 2022 17:23:00 +0300
Subject: [PATCH 3/5] Remove unused import

---
 .../im/vector/app/features/analytics/DecryptionFailureTracker.kt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
index 308077810d..057930b4c8 100644
--- a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
+++ b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
@@ -29,7 +29,6 @@ import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.launch
 import org.matrix.android.sdk.api.session.crypto.MXCryptoError
 import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
-import timber.log.Timber
 import javax.inject.Inject
 import javax.inject.Singleton
 

From e368046b8435461d61daaaa821e009c12fe8d4ac Mon Sep 17 00:00:00 2001
From: ariskotsomitopoulos <aris.kotsomitopoulos@gmail.com>
Date: Thu, 21 Apr 2022 19:31:12 +0300
Subject: [PATCH 4/5] Add KEYS_WITHHELD error type to analytics

---
 .../features/analytics/DecryptionFailureTracker.kt   | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
index 057930b4c8..b23365c056 100644
--- a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
+++ b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
@@ -38,7 +38,8 @@ private data class DecryptionFailure(
         val failedEventId: String,
         val error: MXCryptoError.ErrorType
 )
-private typealias DetailedErrorName  = Pair<String, Error.Name>
+private typealias DetailedErrorName = Pair<String, Error.Name>
+
 private const val GRACE_PERIOD_MILLIS = 4_000
 private const val CHECK_INTERVAL = 2_000L
 
@@ -145,10 +146,11 @@ class DecryptionFailureTracker @Inject constructor(
     private fun MXCryptoError.ErrorType.toAnalyticsErrorName(): DetailedErrorName {
         val detailed = "$name | mxc_crypto_error_type"
         return when (this) {
-            MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID -> Pair(detailed, Error.Name.OlmKeysNotSentError)
-            MXCryptoError.ErrorType.OLM                        -> Pair(detailed, Error.Name.OlmUnspecifiedError)
-            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX      -> Pair(detailed, Error.Name.OlmIndexError)
-            else                                               -> Pair(detailed, Error.Name.UnknownError)
+            MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID,
+            MXCryptoError.ErrorType.KEYS_WITHHELD         -> Pair(detailed, Error.Name.OlmKeysNotSentError)
+            MXCryptoError.ErrorType.OLM                   -> Pair(detailed, Error.Name.OlmUnspecifiedError)
+            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX -> Pair(detailed, Error.Name.OlmIndexError)
+            else                                          -> Pair(detailed, Error.Name.UnknownError)
         }
     }
 }

From 1f9b9571fd46e51e2b25d022dcb422419ea1fc67 Mon Sep 17 00:00:00 2001
From: ariskotsomitopoulos <aris.kotsomitopoulos@gmail.com>
Date: Wed, 27 Apr 2022 12:42:23 +0300
Subject: [PATCH 5/5] Enhance code format

---
 .../features/analytics/DecryptionFailureTracker.kt    | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
index b23365c056..ec34ff7421 100644
--- a/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
+++ b/vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
@@ -145,12 +145,13 @@ class DecryptionFailureTracker @Inject constructor(
 
     private fun MXCryptoError.ErrorType.toAnalyticsErrorName(): DetailedErrorName {
         val detailed = "$name | mxc_crypto_error_type"
-        return when (this) {
+        val errorName = when (this) {
             MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID,
-            MXCryptoError.ErrorType.KEYS_WITHHELD         -> Pair(detailed, Error.Name.OlmKeysNotSentError)
-            MXCryptoError.ErrorType.OLM                   -> Pair(detailed, Error.Name.OlmUnspecifiedError)
-            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX -> Pair(detailed, Error.Name.OlmIndexError)
-            else                                          -> Pair(detailed, Error.Name.UnknownError)
+            MXCryptoError.ErrorType.KEYS_WITHHELD         -> Error.Name.OlmKeysNotSentError
+            MXCryptoError.ErrorType.OLM                   -> Error.Name.OlmUnspecifiedError
+            MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX -> Error.Name.OlmIndexError
+            else                                          -> Error.Name.UnknownError
         }
+        return DetailedErrorName(detailed, errorName)
     }
 }