Keep every signed-in account live (#6299)

* Keep every signed-in account live

With the multi-account flag on, the accounts other than the active one are now
restored at launch, one after the other once the active account's UI is up.
They run their migrations, sync while the app is active, get a pusher (appended
so accounts on the same homeserver keep each other's) and count towards the app
badge, and background refresh and search backfill cover them too. Only the
active account's remote settings are ever applied.

Calls stay with the active account until the call service handles every account,
and an auth error on another account is only logged until accounts can be signed
out one at a time. With the flag off only the active account is live, as before.

* Sum the app badge across every signed in account

Each live account now reports its own unread count, and the app badge
shows their sum. Delivered notifications are only cleared for the
account whose rooms were read, so a room shared by two accounts keeps
the other account's notifications.

The room list observer for the badge and the notifications moves from
the UserSessionFlowCoordinator to the AppCoordinator's per-account
wiring. The flow keeps updating the chats tab badge.

* Let the user session manager manage every session

- The AppCoordinator tells the manager to pause, resume, go offline or
  run the search backfill, instead of looping over the sessions itself.
- The manager also restores the other accounts, preparing each with the
  AppCoordinator's migrations and wiring, and resumes them while the
  services are running. With a single account nothing changes.
- Wiring an account again (e.g. after clearing the cache) replaces its
  observers rather than adding to them.
- Add addAccountFlowEnabled, a developer-only switch for the upcoming
  add account flow. It isn't a user preference, so testers enabling
  multi-account can't add a second account yet.

* Apply review suggestions to keeping every account live

- Move the notification changes (pushers for every account, the summed
  app badge and per-account clearing) to their own PR, so notifications
  follow the active account again for now.
- Revert loading only the active account's remote settings: they load in
  the store again, as on develop, until the hooks are reworked.
- The user session manager reports the auth errors of every live session,
  so the AppCoordinator subscribes once instead of keeping per-account
  observers.
- Restoring a single account is private to the manager now.
- Rename otherSessionsRestoreTask to otherSessionsRestorationTask.

* Restore every signed in account through one path

The user session manager now restores every account in a single pass,
most recently selected first, instead of separate restores for the
active account and the others. The AppCoordinator shows the first
account that comes back and no longer reads the multi-account flag:
without it, the manager only ever makes one account live.

- Clearing the cache clears every live account, and the next pass
  replaces their sessions.
- Each account is migrated once per launch, and an account that just
  signed in is never migrated.
- A pass also runs after signing in, so the other accounts come live.
- Remove UserSessionManagerError, restoreActiveSession and
  restoreOtherSessions.

* Only clear the cache of the active account

Clearing the cache goes back to clearing the account shown in the UI.
Its session stays live until the next restore pass replaces it, and when
that restore fails another live account becomes the active one.

* Apply review suggestions to restoring every account

- Remove the search backfill guard: with no sessions the manager starts
  nothing and reports that nothing is running, so the task completes.
- Add a TODO to switch to the most recently used account when the
  active one signs out while others are signed in (element-meta#3325).
- Disable SwiftLint's type_body_length on the AppCoordinator again, it's
  just over the limit with develop's new migrations.
This commit is contained in:
Mauro
2026-10-13 19:06:05 +02:00
committed by GitHub
parent ae9e0cc176
commit 7752ae3f6a
7 changed files with 545 additions and 100 deletions
@@ -19,6 +19,10 @@ final class UserSessionManager: UserSessionManagerProtocol {
private let appSettings: AppSettings
private let sessionsSubject = CurrentValueSubject<[UserSessionProtocol], Never>([])
/// Whether the services are running, so that the sessions restored meanwhile are resumed too.
private var areServicesRunning = false
/// Accounts whose caches were cleared. Their old session stays live until restoring replaces it.
private var clearedUserIDs: Set<String> = []
/// Every signed in account, most recently selected first, with its session once it's live.
/// Assigning `nil` removes an account rather than clearing its session.
@@ -38,10 +42,32 @@ final class UserSessionManager: UserSessionManagerProtocol {
accounts.values.first ?? nil
}
var callbacksPublisher: AnyPublisher<UserSessionManagerCallback, Never> {
sessionsSubject
.map { userSessions in
Publishers.MergeMany(userSessions.map { userSession in
let userID = userSession.clientProxy.userID
return userSession.callbacks.map { callback in
switch callback {
case .didReceiveAuthError(let isSoftLogout):
UserSessionManagerCallback.didReceiveAuthError(userID: userID, isSoftLogout: isSoftLogout)
}
}
})
}
.switchToLatest()
.eraseToAnyPublisher()
}
var clientSessionDelegate: ClientSessionDelegate {
userSessionStore.clientSessionDelegate
}
/// Whether an account has a session that's live and up to date (its caches weren't cleared).
private var hasLiveSession: Bool {
accounts.contains { userID, userSession in userSession != nil && !clearedUserIDs.contains(userID) }
}
init(userSessionStore: UserSessionStoreProtocol, appSettings: AppSettings) {
self.userSessionStore = userSessionStore
self.appSettings = appSettings
@@ -66,22 +92,36 @@ final class UserSessionManager: UserSessionManagerProtocol {
// MARK: - Lifecycle
func restoreActiveSession() async -> Result<UserSessionProtocol, UserSessionManagerError> {
guard !accounts.isEmpty else {
return .failure(.noAccounts)
}
while let userID = accounts.keys.first {
switch await userSessionStore.restoreUserSession(userID: userID) {
case .success(let userSession):
return .success(userSession)
case .failure(let error):
MXLog.error("Failed restoring \(userID), falling back to the next account: \(error)")
accounts.removeValue(forKey: userID)
func restoreSessions(prepare: @MainActor (UserSessionProtocol) async -> Void, didAdd: @MainActor (UserSessionProtocol) -> Void) async {
for userID in accounts.keys where session(for: userID) == nil || clearedUserIDs.contains(userID) {
// Every live session syncs, so without multi-account only one account is ever made live.
guard appSettings.multiAccountEnabled || !hasLiveSession else { return }
// Signing out or clearing the cache cancels this, after which nothing may go live.
guard !Task.isCancelled else { return }
guard let userSession = await restoreUserSession(userID: userID) else { continue }
guard !Task.isCancelled else { return }
await prepare(userSession)
guard !Task.isCancelled else { return }
add(userSession)
didAdd(userSession)
if areServicesRunning {
await userSession.clientProxy.resumeServices()
}
// Lets the work queued for this session (e.g. its first sync) start before the next account's restore.
await Task.yield()
}
}
func clearCaches(userID: String) async {
guard let userSession = session(for: userID) else { return }
return .failure(.failedRestoringSessions)
// Marked before clearing, so a restore that starts meanwhile replaces the session.
clearedUserIDs.insert(userID)
await userSession.clientProxy.clearCaches()
}
func userSession(for client: ClientProtocol, sessionDirectories: SessionDirectories, passphrase: Data) async -> Result<UserSessionProtocol, UserSessionStoreError> {
@@ -90,6 +130,7 @@ final class UserSessionManager: UserSessionManagerProtocol {
func add(_ userSession: UserSessionProtocol) {
let userID = userSession.clientProxy.userID
clearedUserIDs.remove(userID)
if accounts.keys.contains(userID) {
accounts[userID] = userSession
@@ -106,11 +147,62 @@ final class UserSessionManager: UserSessionManagerProtocol {
}
userSessionStore.logout(userSession: userSession)
clearedUserIDs.remove(userID)
accounts.removeValue(forKey: userID)
}
func reset() {
userSessionStore.reset()
clearedUserIDs.removeAll()
accounts.removeAll()
}
// MARK: - Services
var isSearchBackfillRunning: Bool {
sessionsSubject.value.contains { $0.clientProxy.isSearchBackfillRunning }
}
func resumeServices() async {
areServicesRunning = true
for userSession in sessionsSubject.value {
await userSession.clientProxy.resumeServices()
}
}
func pauseServices() async {
areServicesRunning = false
for userSession in sessionsSubject.value {
await userSession.clientProxy.pauseServices()
}
}
func configurePresence(_ presence: ClientProxyPresence, sendImmediately: Bool) async {
for userSession in sessionsSubject.value {
_ = await userSession.clientProxy.configurePresence(presence, sendImmediately: sendImmediately)
}
}
func startSearchBackfill(strategy: SearchBackfillStrategy) {
sessionsSubject.value.forEach { $0.clientProxy.startSearchBackfill(strategy: strategy) }
}
func stopSearchBackfill() {
sessionsSubject.value.forEach { $0.clientProxy.stopSearchBackfill() }
}
// MARK: - Private
/// Restores an account's session, removing the account when that fails (the store deletes its data).
private func restoreUserSession(userID: String) async -> UserSessionProtocol? {
switch await userSessionStore.restoreUserSession(userID: userID) {
case .success(let userSession):
return userSession
case .failure(let error):
MXLog.error("Failed restoring \(userID): \(error)")
clearedUserIDs.remove(userID)
accounts.removeValue(forKey: userID)
return nil
}
}
}