mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Flatten out the NSE file structure and switch room fetching to the notificationClient instead of the base one
- the notification client uses its internal in-memory store backed client which has up to date data after running the sliding sync loop - see https://github.com/matrix-org/matrix-rust-sdk/pull/4991 for more details
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// Copyright 2023, 2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
// Please see LICENSE files in the repository root for full details.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import MatrixRustSDK
|
||||
|
||||
final class NSEUserSession {
|
||||
let sessionDirectories: SessionDirectories
|
||||
|
||||
private let baseClient: Client
|
||||
private let notificationClient: NotificationClient
|
||||
private let userID: String
|
||||
private(set) lazy var mediaProvider: MediaProviderProtocol = MediaProvider(mediaLoader: MediaLoader(client: baseClient),
|
||||
imageCache: .onlyOnDisk,
|
||||
networkMonitor: nil)
|
||||
private let delegateHandle: TaskHandle?
|
||||
|
||||
init(credentials: KeychainCredentials,
|
||||
roomID: String,
|
||||
clientSessionDelegate: ClientSessionDelegate,
|
||||
appHooks: AppHooks,
|
||||
appSettings: CommonSettingsProtocol) async throws {
|
||||
sessionDirectories = credentials.restorationToken.sessionDirectories
|
||||
|
||||
userID = credentials.userID
|
||||
if credentials.restorationToken.passphrase != nil {
|
||||
MXLog.info("Restoring client with encrypted store.")
|
||||
}
|
||||
|
||||
let homeserverURL = credentials.restorationToken.session.homeserverUrl
|
||||
let clientBuilder = ClientBuilder
|
||||
.baseBuilder(setupEncryption: false,
|
||||
httpProxy: URL(string: homeserverURL)?.globalProxy,
|
||||
slidingSync: .restored,
|
||||
sessionDelegate: clientSessionDelegate,
|
||||
appHooks: appHooks,
|
||||
enableOnlySignedDeviceIsolationMode: appSettings.enableOnlySignedDeviceIsolationMode,
|
||||
requestTimeout: 15000,
|
||||
maxRequestRetryTime: 5000)
|
||||
.systemIsMemoryConstrained()
|
||||
.sessionPaths(dataPath: credentials.restorationToken.sessionDirectories.dataPath,
|
||||
cachePath: credentials.restorationToken.sessionDirectories.cachePath)
|
||||
.username(username: credentials.userID)
|
||||
.homeserverUrl(url: homeserverURL)
|
||||
.sessionPassphrase(passphrase: credentials.restorationToken.passphrase)
|
||||
|
||||
baseClient = try await clientBuilder.build()
|
||||
delegateHandle = baseClient.setDelegate(delegate: ClientDelegateWrapper())
|
||||
|
||||
try await baseClient.restoreSessionWith(session: credentials.restorationToken.session,
|
||||
roomLoadSettings: .one(roomId: roomID))
|
||||
|
||||
notificationClient = try await baseClient.notificationClient(processSetup: .multipleProcesses)
|
||||
}
|
||||
|
||||
func notificationItemProxy(roomID: String, eventID: String) async -> NotificationItemProxyProtocol? {
|
||||
do {
|
||||
let notification = try await notificationClient.getNotification(roomId: roomID, eventId: eventID)
|
||||
|
||||
guard let notification else {
|
||||
return nil
|
||||
}
|
||||
return NotificationItemProxy(notificationItem: notification,
|
||||
eventID: eventID,
|
||||
receiverID: userID,
|
||||
roomID: roomID)
|
||||
} catch {
|
||||
MXLog.error("Could not get notification's content creating an empty notification instead, error: \(error)")
|
||||
return EmptyNotificationItemProxy(eventID: eventID, roomID: roomID, receiverID: userID)
|
||||
}
|
||||
}
|
||||
|
||||
func roomForIdentifier(_ roomID: String) -> Room? {
|
||||
do {
|
||||
return try notificationClient.getRoom(roomId: roomID)
|
||||
} catch {
|
||||
MXLog.error("Failed retrieving room with error: \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
delegateHandle?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private class ClientDelegateWrapper: ClientDelegate {
|
||||
// MARK: - ClientDelegate
|
||||
|
||||
func didReceiveAuthError(isSoftLogout: Bool) {
|
||||
MXLog.error("Received authentication error, the NSE can't handle this.")
|
||||
}
|
||||
|
||||
func didRefreshTokens() {
|
||||
MXLog.info("Delegating session updates to the ClientSessionDelegate.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user