diff --git a/Bitkit/Managers/PubkyProfileManager.swift b/Bitkit/Managers/PubkyProfileManager.swift index 06845ed59..e25680fd1 100644 --- a/Bitkit/Managers/PubkyProfileManager.swift +++ b/Bitkit/Managers/PubkyProfileManager.swift @@ -1630,7 +1630,7 @@ class PubkyProfileManager: ObservableObject { } private func signOut(cleanPrivatePaykitEndpoints: Bool, allowDuringProfileDeletion: Bool = false) async throws { - try await signOut(allowDuringProfileDeletion: allowDuringProfileDeletion) { + try await signOutOrForget(allowDuringProfileDeletion: allowDuringProfileDeletion) { if cleanPrivatePaykitEndpoints { try await Self.removePrivatePaykitEndpoints(context: "PubkyProfileManager.signOut") } @@ -1639,6 +1639,42 @@ class PubkyProfileManager: ObservableObject { } } + /// Waits for a restore already running, so an identity it restores is signed out rather than only forgotten. + func signOutOrForget( + allowDuringProfileDeletion: Bool, + forgetSessionAccess: @escaping @Sendable () async throws -> Void = { try await PubkyService.forgetSessionAccess() }, + signOutRestored: @escaping @Sendable () async throws -> Void + ) async throws { + while let initializationTask { + await initializationTask.value + } + let isRestored = publicKey != nil + try await signOut(allowDuringProfileDeletion: allowDuringProfileDeletion, performSessionCleanup: { + try await Self.signOutCleanup( + isRestored: isRestored, + forgetSessionAccess: forgetSessionAccess, + signOutRestored: signOutRestored + ) + }) + } + + /// An identity that never restored, such as one whose shared Paykit state a newer Bitkit saved, cannot read that state + /// to remove its endpoints or end its session, so Disconnect only forgets it on this device. A pending endpoint cleanup + /// is dropped with it, so it cannot run against the next identity. + nonisolated static func signOutCleanup( + isRestored: Bool, + forgetSessionAccess: @Sendable () async throws -> Void = { try await PubkyService.forgetSessionAccess() }, + dropPendingEndpointCleanup: @Sendable () -> Void = { PublicPaykitService.setCleanupPending(false) }, + signOutRestored: @Sendable () async throws -> Void + ) async throws { + guard isRestored else { + try await forgetSessionAccess() + dropPendingEndpointCleanup() + return + } + try await signOutRestored() + } + func signOut(performSessionCleanup: @escaping @Sendable () async throws -> Void) async throws { try await signOut(allowDuringProfileDeletion: false, performSessionCleanup: performSessionCleanup) } diff --git a/Bitkit/Views/Profile/EditProfileView.swift b/Bitkit/Views/Profile/EditProfileView.swift index 927d8d578..78dc75b1b 100644 --- a/Bitkit/Views/Profile/EditProfileView.swift +++ b/Bitkit/Views/Profile/EditProfileView.swift @@ -228,7 +228,7 @@ struct EditProfileView: View { try await pubkyProfile.signOut() navigation.path = [app.hasSeenProfileIntro ? .pubkyChoice : .profileIntro] } catch { - app.toast(type: .error, title: t("profile__sign_out_title"), description: error.localizedDescription) + app.toast(type: .error, title: t("profile__sign_out_title"), description: error.profileErrorDescription) } } diff --git a/Bitkit/Views/Profile/ProfileView.swift b/Bitkit/Views/Profile/ProfileView.swift index 68a967190..683419ed1 100644 --- a/Bitkit/Views/Profile/ProfileView.swift +++ b/Bitkit/Views/Profile/ProfileView.swift @@ -406,7 +406,7 @@ struct ProfileView: View { do { try await pubkyProfile.signOut() } catch { - app.toast(type: .error, title: t("profile__sign_out_title"), description: error.localizedDescription) + app.toast(type: .error, title: t("profile__sign_out_title"), description: error.profileErrorDescription) } isSigningOut = false } diff --git a/BitkitTests/PubkyProfileManagerTests.swift b/BitkitTests/PubkyProfileManagerTests.swift index d716caa9d..7d30a9e18 100644 --- a/BitkitTests/PubkyProfileManagerTests.swift +++ b/BitkitTests/PubkyProfileManagerTests.swift @@ -1135,6 +1135,81 @@ final class PubkyProfileManagerTests: XCTestCase { XCTAssertFalse(manager.sessionRestorationFailed) } + func testDisconnectOnlyForgetsAnIdentityThatNeverRestored() async throws { + let calls = LockedCalls() + + try await PubkyProfileManager.signOutCleanup( + isRestored: false, + forgetSessionAccess: { calls.append("forget") }, + dropPendingEndpointCleanup: { calls.append("dropCleanup") }, + signOutRestored: { calls.append("signOut") } + ) + try await PubkyProfileManager.signOutCleanup( + isRestored: true, + forgetSessionAccess: { calls.append("forget") }, + dropPendingEndpointCleanup: { calls.append("dropCleanup") }, + signOutRestored: { calls.append("signOut") } + ) + + XCTAssertEqual(calls.values, ["forget", "dropCleanup", "signOut"]) + } + + @MainActor + func testDisconnectForgetsAProfileThatNeedsAnUpdate() async throws { + try await withEmptyIdentityStorage { + let manager = RecoveryProfileManager() + let calls = LockedCalls() + await manager.initialize { .restorationNeedsUpdate } + XCTAssertTrue(manager.restorationNeedsUpdate) + + try await manager.signOutOrForget( + allowDuringProfileDeletion: false, + forgetSessionAccess: { calls.append("forget") }, + signOutRestored: { calls.append("signOut") } + ) + + XCTAssertEqual(calls.values, ["forget"]) + XCTAssertNil(manager.publicKeyForDisplay) + XCTAssertFalse(manager.restorationNeedsUpdate) + } + } + + @MainActor + func testDisconnectSignsOutAProfileRestoredWhileItWaits() async throws { + try await withEmptyIdentityStorage { + let manager = RecoveryProfileManager() + let calls = LockedCalls() + let started = expectation(description: "Restore started") + let (resume, continuation) = AsyncStream.makeStream() + defer { continuation.finish() } + let restore = Task { + await manager.initialize { + started.fulfill() + for await _ in resume {} + return .restored(publicKey: "restored-identity") + } + } + await fulfillment(of: [started], timeout: 2) + + let disconnect = Task { + try await manager.signOutOrForget( + allowDuringProfileDeletion: false, + forgetSessionAccess: { calls.append("forget") }, + signOutRestored: { calls.append("signOut") } + ) + } + for _ in 0 ..< 5 { + await Task.yield() + } + XCTAssertTrue(calls.values.isEmpty) + continuation.finish() + await restore.value + try await disconnect.value + + XCTAssertEqual(calls.values, ["signOut"]) + } + } + @MainActor func testRecoveryAsksForAnUpdateOnceAndStopsRetryingWhenPaykitStateIsNewer() async { let manager = RecoveryProfileManager() @@ -4428,3 +4503,16 @@ private actor SessionRecoveryAttempts { return count } } + +private final class LockedCalls: @unchecked Sendable { + private let lock = NSLock() + private var calls: [String] = [] + + var values: [String] { + lock.withLock { calls } + } + + func append(_ call: String) { + lock.withLock { calls.append(call) } + } +} diff --git a/changelog.d/next/1427.fixed.md b/changelog.d/next/1427.fixed.md new file mode 100644 index 000000000..81718cea2 --- /dev/null +++ b/changelog.d/next/1427.fixed.md @@ -0,0 +1 @@ +Disconnect now removes a profile that needs a newer Bitkit from this device instead of showing a storage error.