Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Bitkit/Managers/PubkyProfileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion Bitkit/Views/Profile/EditProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Bitkit/Views/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
88 changes: 88 additions & 0 deletions BitkitTests/PubkyProfileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Comment thread
ovitrif marked this conversation as resolved.
Resolved
)
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<Void>.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()
Expand Down Expand Up @@ -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) }
}
}
1 change: 1 addition & 0 deletions changelog.d/next/1427.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disconnect now removes a profile that needs a newer Bitkit from this device instead of showing a storage error.
Loading