mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 03:06:37 +00:00
cff46137b9
* Report user presence on app lifecycle transitions Adds a PresenceReporter driven by the app lifecycle handlers and the sharePresence setting: online when foreground-active and sharing, unavailable when backgrounded and sharing, offline when not sharing. Sends are immediate, deduped, and serialized latest-wins. Background app refresh and the NSE seed the process presence to offline so their sync requests carry no presence side effect. Presence rides on the SDK's process-global state, so every sync path is covered without touching sync lifecycle. Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> * Add presence reporter tests Pins the presence reporter invariants: the never-online sweep with sharing off, an immediate online send on foreground-active, dedupe of repeated states, latest-wins ordering of serialized sends, and the NSE offline seed. Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> * wip: address review comments Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> * refactor: remove obsolete presence heartbeat Remove the shared activity snapshot, heartbeat refresh, and notification-extension freshness policy while retaining the session-scoped presence service and its serialized lifecycle reporting. Restore the NSE's intermediate one-time offline seed so the cleanup remains buildable ahead of the final sync-presence correction. Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> * fix: make notification extension presence-neutral Configure the built NSE client with offline sync presence without sending an immediate presence update. Keep notification setup non-fatal when presence configuration fails and remove the temporary production test seam. Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> * chore: restore application lifecycle logging Restore the pre-existing foreground lifecycle diagnostics that were removed during the presence refactor. Keep presence reporting session-scoped and clarify that background refresh configures the client's sync presence without an immediate update. Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com> --------- Signed-off-by: Jared L <48422312+lhjt@users.noreply.github.com>
158 lines
6.5 KiB
Swift
158 lines
6.5 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-2025 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
|
|
|
|
// sourcery: AutoMockable
|
|
nonisolated protocol NSEUserSessionProtocol {
|
|
var inviteAvatarsVisibility: InviteAvatars { get async }
|
|
var mediaPreviewVisibility: MediaPreviews { get async }
|
|
var threadsEnabled: Bool { get }
|
|
}
|
|
|
|
final nonisolated class NSEUserSession: NSEUserSessionProtocol {
|
|
private let appSettings: CommonSettingsProtocol
|
|
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,
|
|
homeserverReachabilityPublisher: nil)
|
|
private let delegateHandle: TaskHandle?
|
|
|
|
var mediaPreviewVisibility: MediaPreviews {
|
|
get async {
|
|
do {
|
|
return try await baseClient.getMediaPreviewDisplayPolicy() ?? .on
|
|
} catch {
|
|
MXLog.error("Failed to get media preview visibility, defaulting to on. Error: \(error)")
|
|
return .on
|
|
}
|
|
}
|
|
}
|
|
|
|
var inviteAvatarsVisibility: InviteAvatars {
|
|
get async {
|
|
do {
|
|
return try await baseClient.getInviteAvatarsDisplayPolicy() ?? .on
|
|
} catch {
|
|
MXLog.error("Failed to get invite avatars visibility, defaulting to on. Error: \(error)")
|
|
return .on
|
|
}
|
|
}
|
|
}
|
|
|
|
var threadsEnabled: Bool {
|
|
appSettings.threadsEnabled
|
|
}
|
|
|
|
init(credentials: KeychainCredentials,
|
|
roomID: String,
|
|
clientSessionDelegate: ClientSessionDelegate,
|
|
appHooks: AppHooks,
|
|
appSettings: CommonSettingsProtocol) async throws {
|
|
userID = credentials.userID
|
|
self.appSettings = appSettings
|
|
|
|
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,
|
|
threadsEnabled: appSettings.threadsEnabled)
|
|
.systemIsMemoryConstrained()
|
|
.sqliteStore(config: .init(dataPath: credentials.restorationToken.sessionDirectories.dataPath,
|
|
cachePath: credentials.restorationToken.sessionDirectories.cachePath)
|
|
.passphrase(passphrase: credentials.restorationToken.passphrase))
|
|
.username(username: credentials.userID)
|
|
.homeserverUrl(url: homeserverURL)
|
|
|
|
baseClient = try await clientBuilder.build()
|
|
|
|
do {
|
|
try await baseClient.setPresence(presence: .offline, immediate: false)
|
|
} catch {
|
|
MXLog.error("Failed configuring offline presence before notification processing with error: \(error)")
|
|
}
|
|
delegateHandle = try 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 notificationStatus = try await notificationClient.getNotification(roomId: roomID, eventId: eventID)
|
|
|
|
switch notificationStatus {
|
|
case .event(let notification):
|
|
return NotificationItemProxy(notificationItem: notification,
|
|
receiverID: userID,
|
|
roomID: roomID)
|
|
case .eventNotFound:
|
|
MXLog.error("Notification event not found - roomID: \(roomID) eventID: \(eventID)")
|
|
return nil
|
|
case .eventFilteredOut:
|
|
MXLog.warning("Notification event filtered out - roomID: \(roomID) eventID: \(eventID)")
|
|
return nil
|
|
case .eventRedacted:
|
|
MXLog.warning("Notification event redacted - roomID: \(roomID) eventID: \(eventID)")
|
|
return nil
|
|
}
|
|
} catch {
|
|
MXLog.error("Could not get notification's content creating an empty notification instead, error: \(error)")
|
|
return EmptyNotificationItemProxy(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 final nonisolated class ClientDelegateWrapper: ClientDelegate {
|
|
// MARK: - ClientDelegate
|
|
|
|
func didReceiveAuthError(isSoftLogout: Bool) {
|
|
MXLog.error("Received authentication error, the NSE can't handle this.")
|
|
}
|
|
|
|
// periphery:ignore - required by the SDK's delegate protocol
|
|
func didRefreshTokens() {
|
|
MXLog.info("Delegating session updates to the ClientSessionDelegate.")
|
|
}
|
|
|
|
func onBackgroundTaskErrorReport(taskName: String, error: MatrixRustSDK.BackgroundTaskFailureReason) {
|
|
switch error {
|
|
case .panic(let message, let backtrace):
|
|
MXLog.error("Received background task panic: \(message ?? "Missing message")\nBacktrace:\n\(backtrace ?? "Missing backtrace")")
|
|
case .error(let error):
|
|
MXLog.error("Received background task error: \(error)")
|
|
case .earlyTermination:
|
|
MXLog.error("Received background task early termination")
|
|
}
|
|
}
|
|
}
|