From 75c40388ae9f1319c1c496ac7c0586ce525df25e Mon Sep 17 00:00:00 2001
From: Valere <bill.carson@valrsoft.com>
Date: Thu, 12 Dec 2024 18:50:54 +0100
Subject: [PATCH 1/2] feat(posthog): wire support for extra utd error
 properties

---
 .../UserSessionFlowCoordinator.swift          | 36 ++++++++++---------
 .../Services/Analytics/AnalyticsService.swift | 19 ++++++----
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift
index acd4cc44e0..3642c5c548 100644
--- a/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift
+++ b/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift
@@ -5,6 +5,7 @@
 // Please see LICENSE in the repository root for full details.
 //
 
+import AnalyticsEvents
 import AVKit
 import Combine
 import MatrixRustSDK
@@ -382,24 +383,25 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
                     timeToDecryptMs = -1
                 }
                 
-                switch info.cause {
-                case .unknown:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .OlmKeysNotSentError, timeToDecryptMillis: timeToDecryptMs)
-                case .unknownDevice:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .ExpectedSentByInsecureDevice, timeToDecryptMillis: timeToDecryptMs)
-                case .unsignedDevice:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .ExpectedSentByInsecureDevice, timeToDecryptMillis: timeToDecryptMs)
-                case .verificationViolation:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .ExpectedVerificationViolation, timeToDecryptMillis: timeToDecryptMs)
-                case .sentBeforeWeJoined:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .ExpectedDueToMembership, timeToDecryptMillis: timeToDecryptMs)
-                case .historicalMessage:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .HistoricalMessage, timeToDecryptMillis: timeToDecryptMs)
-                case .withheldForUnverifiedOrInsecureDevice:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .RoomKeysWithheldForUnverifiedDevice, timeToDecryptMillis: timeToDecryptMs)
-                case .withheldBySender:
-                    analytics.trackError(context: nil, domain: .E2EE, name: .OlmKeysNotSentError, timeToDecryptMillis: timeToDecryptMs)
+                let errorName: AnalyticsEvent.Error.Name = switch info.cause {
+                case .unknown: .OlmKeysNotSentError
+                case .unknownDevice, .unsignedDevice: .ExpectedSentByInsecureDevice
+                case .verificationViolation: .ExpectedVerificationViolation
+                case .sentBeforeWeJoined: .ExpectedDueToMembership
+                case .historicalMessage: .HistoricalMessage
+                case .withheldForUnverifiedOrInsecureDevice: .RoomKeysWithheldForUnverifiedDevice
+                case .withheldBySender: .OlmKeysNotSentError
                 }
+                
+                analytics.trackError(context: nil,
+                                     domain: .E2EE,
+                                     name: errorName,
+                                     timeToDecryptMillis: timeToDecryptMs,
+                                     eventLocalAgeMillis: Int(truncatingIfNeeded: info.eventLocalAgeMillis),
+                                     isFederated: info.ownHomeserver != info.senderHomeserver,
+                                     isMatrixDotOrg: info.ownHomeserver == "matrix.org",
+                                     userTrustsOwnIdentity: info.userTrustsOwnIdentity,
+                                     wasVisibleToUser: nil)
             }
             .store(in: &cancellables)
                 
diff --git a/ElementX/Sources/Services/Analytics/AnalyticsService.swift b/ElementX/Sources/Services/Analytics/AnalyticsService.swift
index 3ecbed8054..a3dc13d932 100644
--- a/ElementX/Sources/Services/Analytics/AnalyticsService.swift
+++ b/ElementX/Sources/Services/Analytics/AnalyticsService.swift
@@ -122,19 +122,26 @@ extension AnalyticsService {
     /// - Parameter name: The name of the error
     /// - Parameter timeToDecryptMillis: The time it took to decrypt the event in milliseconds, needs to be used only to track UTD errors, otherwise if the error is nort related to UTD it should be nil.
     /// Can be found in `UnableToDecryptInfo`. In case the `UnableToDecryptInfo` contains the value as nil, pass it as `-1`
-    func trackError(context: String?, domain: AnalyticsEvent.Error.Domain, name: AnalyticsEvent.Error.Name, timeToDecryptMillis: Int? = nil) {
+    func trackError(context: String?, domain: AnalyticsEvent.Error.Domain,
+                    name: AnalyticsEvent.Error.Name,
+                    timeToDecryptMillis: Int? = nil,
+                    eventLocalAgeMillis: Int? = nil,
+                    isFederated: Bool? = nil,
+                    isMatrixDotOrg: Bool? = nil,
+                    userTrustsOwnIdentity: Bool? = nil,
+                    wasVisibleToUser: Bool? = nil) {
         // CryptoModule is deprecated
         capture(event: AnalyticsEvent.Error(context: context,
                                             cryptoModule: .Rust,
                                             cryptoSDK: .Rust,
                                             domain: domain,
-                                            eventLocalAgeMillis: nil,
-                                            isFederated: nil,
-                                            isMatrixDotOrg: nil,
+                                            eventLocalAgeMillis: eventLocalAgeMillis,
+                                            isFederated: isFederated,
+                                            isMatrixDotOrg: isMatrixDotOrg,
                                             name: name,
                                             timeToDecryptMillis: timeToDecryptMillis,
-                                            userTrustsOwnIdentity: nil,
-                                            wasVisibleToUser: nil))
+                                            userTrustsOwnIdentity: userTrustsOwnIdentity,
+                                            wasVisibleToUser: wasVisibleToUser))
     }
     
     /// Track the creation of a room

From 4c89ecc41270d2d8c62d84c04082b93eec537f1f Mon Sep 17 00:00:00 2001
From: Stefan Ceriu <stefan.ceriu@gmail.com>
Date: Fri, 13 Dec 2024 12:36:48 +0200
Subject: [PATCH 2/2] Bump the RustSDK to v1.0.80

---
 ElementX.xcodeproj/project.pbxproj                            | 2 +-
 .../project.xcworkspace/xcshareddata/swiftpm/Package.resolved | 4 ++--
 project.yml                                                   | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/ElementX.xcodeproj/project.pbxproj b/ElementX.xcodeproj/project.pbxproj
index 0e4464da32..adcacb14c5 100644
--- a/ElementX.xcodeproj/project.pbxproj
+++ b/ElementX.xcodeproj/project.pbxproj
@@ -8351,7 +8351,7 @@
 			repositoryURL = "https://github.com/element-hq/matrix-rust-components-swift";
 			requirement = {
 				kind = exactVersion;
-				version = 1.0.79;
+				version = 1.0.80;
 			};
 		};
 		701C7BEF8F70F7A83E852DCC /* XCRemoteSwiftPackageReference "GZIP" */ = {
diff --git a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
index 5702b2baa4..5a7331fa71 100644
--- a/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
+++ b/ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
@@ -149,8 +149,8 @@
       "kind" : "remoteSourceControl",
       "location" : "https://github.com/element-hq/matrix-rust-components-swift",
       "state" : {
-        "revision" : "14fc58ea578bd370f78e45e83ef2f077a3bd683f",
-        "version" : "1.0.79"
+        "revision" : "342dc2f1b6553dba7ed5d6f0a330d77d7fae13c4",
+        "version" : "1.0.80"
       }
     },
     {
diff --git a/project.yml b/project.yml
index 15f4167ced..d5a50f45c2 100644
--- a/project.yml
+++ b/project.yml
@@ -61,7 +61,7 @@ packages:
   # Element/Matrix dependencies
   MatrixRustSDK:
     url: https://github.com/element-hq/matrix-rust-components-swift
-    exactVersion: 1.0.79
+    exactVersion: 1.0.80
     # path: ../matrix-rust-sdk
   Compound:
     url: https://github.com/element-hq/compound-ios