Skip to content

Commit

Permalink
finally works! made also the feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Jun 20, 2023
1 parent f6e863f commit a1f30b2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
10 changes: 10 additions & 0 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)

NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminate),
name: UIApplication.willTerminateNotification,
object: nil)
}

@objc
private func applicationWillTerminate() {
userSession?.clientProxy.stopSync()
}

@objc
Expand Down
4 changes: 4 additions & 0 deletions ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ final class AppSettings {
/// Tag describing which set of device specific rules a pusher executes.
@UserPreference(key: UserDefaultsKeys.pusherProfileTag, storageType: .userDefaults(store))
var pusherProfileTag: String?

/// Tag describing if the app and the NSE should use the encryption sync
@UserPreference(key: SharedUserDefaultsKeys.isEncryptionSyncEnabled, initialValue: true, storageType: .userDefaults(store))
var isEncryptionSyncEnabled

// MARK: - Other

Expand Down
21 changes: 14 additions & 7 deletions ElementX/Sources/Services/Client/ClientProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ClientProxy: ClientProxyProtocol {
private var roomListStateUpdateTaskHandle: TaskHandle?

private var encryptionSyncService: EncryptionSync?
private var isEncryptionSyncing = false

var roomSummaryProvider: RoomSummaryProviderProtocol?
var inviteSummaryProvider: RoomSummaryProviderProtocol?
Expand All @@ -72,7 +73,7 @@ class ClientProxy: ClientProxyProtocol {
deinit {
// These need to be inlined instead of using stopSync()
// as we can't call async methods safely from deinit
encryptionSyncService?.stop()
stopEncryptionSyncService()
client.setDelegate(delegate: nil)
roomListSyncTaskHandle?.cancel()
}
Expand Down Expand Up @@ -146,9 +147,11 @@ class ClientProxy: ClientProxyProtocol {
roomListSyncTaskHandle = nil
}

private var isEncryptionSyncing = false

private func stopEncryptionSyncService() {
guard ServiceLocator.shared.settings.isEncryptionSyncEnabled else {
return
}
MXLog.info("Stopping Encryption Sync service")
isEncryptionSyncing = false
guard let encryptionSyncService else {
return
Expand Down Expand Up @@ -394,19 +397,23 @@ class ClientProxy: ClientProxyProtocol {
}

private func startEncryptionSyncService() {
guard ServiceLocator.shared.settings.isEncryptionSyncEnabled else {
return
}
guard encryptionSyncService == nil else {
fatalError("This shouldn't be called more than once")
}
configureEncryptionSyncService()
}

private func configureEncryptionSyncService() {
do {
let listener = EncryptionSyncListenerProxy { [weak self] in
MXLog.info("Encryption Sync did terminate for user: \(self?.userID ?? "unknown")")
guard let self, isEncryptionSyncing else {
return
}
// To avoid the nested block on error
Task {
self.startEncryptionSyncService()
}
self.configureEncryptionSyncService()
}
let encryptionSync = try client.mainEncryptionSync(id: "Main App", listener: listener)
encryptionSync.reloadCaches()
Expand Down
23 changes: 16 additions & 7 deletions NSE/Sources/NotificationServiceExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import MatrixRustSDK
import UserNotifications

class NotificationServiceExtension: UNNotificationServiceExtension {
private static var userSession: [String: NSEUserSession] = [:]
private let settings = NSESettings()
private lazy var keychainController = KeychainController(service: .sessions,
accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
private var handler: ((UNNotificationContent) -> Void)?
private var modifiedContent: UNMutableNotificationContent?
private var userSession: NSEUserSession?

override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
Expand Down Expand Up @@ -72,9 +72,11 @@ class NotificationServiceExtension: UNNotificationServiceExtension {

do {
let userSession = try NSEUserSession(credentials: credentials)

self.userSession = userSession
var itemProxy = await userSession.notificationItemProxy(roomID: roomId, eventID: eventId)
if itemProxy.isEncrypted, let _ = try? userSession.startEncryptionSync() {
if settings.isEncryptionSyncEnabled,
itemProxy.isEncrypted,
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()
Expand Down Expand Up @@ -117,23 +119,30 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
}

handler?(modifiedContent)
handler = nil
self.modifiedContent = nil
cleanUp()
}

private func discard() {
MXLog.info("\(tag) discard")

handler?(UNMutableNotificationContent())
handler = nil
modifiedContent = nil
cleanUp()
}

private var tag: String {
"[NSE][\(Unmanaged.passUnretained(self).toOpaque())][\(Unmanaged.passUnretained(Thread.current).toOpaque())]"
}

private func cleanUp() {
handler = nil
modifiedContent = nil
if settings.isEncryptionSyncEnabled {
userSession?.stopEncryptionSync()
}
}

deinit {
cleanUp()
NSELogger.logMemory(with: tag)
MXLog.info("\(tag) deinit")
}
Expand Down
4 changes: 4 additions & 0 deletions NSE/Sources/Other/NSESettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ final class NSESettings {

/// UserDefaults to be used on reads and writes.
private static var store: UserDefaults! = UserDefaults(suiteName: suiteName)

/// Tag describing if the app and the NSE should use the encryption sync
@UserPreference(key: SharedUserDefaultsKeys.isEncryptionSyncEnabled, defaultValue: true, storageType: .userDefaults(store))
var isEncryptionSyncEnabled
}
6 changes: 5 additions & 1 deletion NSE/Sources/Other/NSEUserSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ final class NSEUserSession {
}
}

func stopEncryptionSync() {
encryptionSyncService?.stop()
}

deinit {
MXLog.info("NSE: NSEUserSession deinit called for user: \(userID)")
encryptionSyncService?.stop()
stopEncryptionSync()
}
}

0 comments on commit a1f30b2

Please sign in to comment.