SDK Changes Refactor (#1312)

* internal id

* notification refactor

* client proxy refactor

* required self

* better identifier system

* using the event id when required

* tests fixed

* tested some stuff

* fixed merge conflict

* improved the test wait

* animation disabled

* Bump Rust SDK version to 1.0.98-alpha (#1310)

* code improvement

* pause sync

* pr suggestions

* result

* Apply suggestions from code review

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>

* enum based debug identifier

---------

Co-authored-by: aringenbach <80891108+aringenbach@users.noreply.github.com>
Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
Mauro
2023-07-12 12:28:41 +02:00
committed by GitHub
parent adb253ff66
commit 76506752b4
83 changed files with 551 additions and 661 deletions
+2 -20
View File
@@ -71,26 +71,9 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
do {
let userSession = try NSEUserSession(credentials: credentials)
let userSession = try NSEUserSession(credentials: credentials, isEncryptionSyncEnabled: settings.isEncryptionSyncEnabled)
self.userSession = userSession
var itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
if settings.isEncryptionSyncEnabled,
itemProxy?.isEncrypted == true,
let _ = try? userSession.startEncryptionSync() {
// TODO: The following wait with a timeout should be handled by the SDK
// We try to decrypt the notification for 10 seconds at most
let date = Date()
repeat {
// if the sync terminated we try one last time then we break from the loop
guard userSession.isSyncing else {
itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
break
}
itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
} while itemProxy?.isEncrypted == true && date.timeIntervalSinceNow > -10
}
guard let itemProxy else {
guard let itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId) else {
MXLog.info("\(tag) no notification for the event, discard")
return discard()
}
@@ -146,7 +129,6 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
private func cleanUp() {
handler = nil
modifiedContent = nil
userSession?.stopEncryptionSync()
}
deinit {
+13 -30
View File
@@ -18,56 +18,39 @@ import Foundation
import MatrixRustSDK
final class NSEUserSession {
private let client: ClientProtocol
private let baseClient: Client
private let notificationClient: NotificationClient
private let userID: String
private var encryptionSyncService: EncryptionSync?
private(set) lazy var mediaProvider: MediaProviderProtocol = MediaProvider(mediaLoader: MediaLoader(client: client),
private(set) lazy var mediaProvider: MediaProviderProtocol = MediaProvider(mediaLoader: MediaLoader(client: baseClient),
imageCache: .onlyOnDisk,
backgroundTaskService: nil)
var isSyncing: Bool {
encryptionSyncService != nil
}
init(credentials: KeychainCredentials) throws {
init(credentials: KeychainCredentials, isEncryptionSyncEnabled: Bool) throws {
userID = credentials.userID
let builder = ClientBuilder()
baseClient = try ClientBuilder()
.basePath(path: URL.sessionsBaseDirectory.path)
.username(username: credentials.userID)
.build()
client = try builder.build()
try client.restoreSession(session: credentials.restorationToken.session)
}
try baseClient.restoreSession(session: credentials.restorationToken.session)
func startEncryptionSync() throws {
let listener = EncryptionSyncListenerProxy { [weak self] reason in
MXLog.info("NSE: Encryption sync terminated for user: \(self?.userID ?? "unknown") with reason: \(reason)")
self?.encryptionSyncService = nil
}
encryptionSyncService = try client.notificationEncryptionSync(id: "NSE", listener: listener, numIters: 2)
MXLog.info("NSE: Encryption sync started for user: \(userID)")
notificationClient = baseClient
.notificationClient()
.retryDecryption(withCrossProcessLock: isEncryptionSyncEnabled)
.finish()
}
func notificationItemProxy(roomID: String, eventID: String) async -> NotificationItemProxyProtocol? {
await Task.dispatch(on: .global()) {
do {
guard let notification = try self.client.getNotificationItem(roomId: roomID, eventId: eventID, filterByPushRules: false) else {
guard let notification = try self.notificationClient.getNotification(roomId: roomID, eventId: eventID) else {
return nil
}
return NotificationItemProxy(notificationItem: notification, receiverID: self.userID)
return NotificationItemProxy(notificationItem: notification, receiverID: self.userID, roomID: roomID)
} catch {
MXLog.error("NSE: Could not get notification's content creating an empty notification instead, error: \(error)")
return EmptyNotificationItemProxy(eventID: eventID, roomID: roomID, receiverID: self.userID)
}
}
}
func stopEncryptionSync() {
encryptionSyncService?.stop()
}
deinit {
MXLog.info("NSE: NSEUserSession deinit called for user: \(userID)")
stopEncryptionSync()
}
}