-
Notifications
You must be signed in to change notification settings - Fork 4
fix: clean up deleted private contacts #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67362eb
fix: clean up deleted private contacts
ben-kaufman 75e8c9c
fix: harden contact deletion and payment authorization
ben-kaufman 171d449
fix: roll back failed contact re-add
ben-kaufman 8d62210
fix: show fixed subscription end date
ben-kaufman a182bae
fix: recheck hardware send authorization on retry
ben-kaufman 42a55f1
Merge master into fix/delete-private-contact
ben-kaufman 9419700
fix: preserve uncertain hardware payment retries
ben-kaufman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,7 +114,18 @@ | |
|
|
||
| @MainActor | ||
| class ContactsManager: ObservableObject { | ||
| @Published var contacts: [PubkyContact] = [] | ||
| private var contactsRevision = 0 | ||
| private var loadGeneration = 0 | ||
| private let contactRecords: @Sendable () async throws -> [ContactRecord] | ||
|
|
||
| init(contactRecords: @escaping @Sendable () async throws -> [ContactRecord] = PubkyService.contactRecords) { | ||
|
Check warning on line 121 in Bitkit/Managers/ContactsManager.swift
|
||
| self.contactRecords = contactRecords | ||
| } | ||
|
|
||
| @Published var contacts: [PubkyContact] = [] { | ||
| didSet { contactsRevision += 1 } | ||
| } | ||
|
|
||
| @Published var isLoading = false | ||
| @Published var hasLoaded = false | ||
| @Published var loadErrorMessage: String? | ||
|
|
@@ -136,6 +147,7 @@ | |
| } | ||
|
|
||
| func reset() { | ||
| loadGeneration += 1 | ||
| contacts = [] | ||
| isLoading = false | ||
| hasLoaded = false | ||
|
|
@@ -173,97 +185,107 @@ | |
| return | ||
| } | ||
|
|
||
| loadGeneration += 1 | ||
| let generation = loadGeneration | ||
| isLoading = true | ||
| loadErrorMessage = nil | ||
| defer { isLoading = false } | ||
| defer { | ||
| if generation == loadGeneration { isLoading = false } | ||
| } | ||
|
|
||
| Logger.info("Loading contacts for \(PubkyPublicKeyFormat.redacted(publicKey))", context: "ContactsManager") | ||
|
|
||
| do { | ||
| let records = try await Task.detached { | ||
| try await PubkyService.contactRecords() | ||
| }.value | ||
|
|
||
| Logger.debug("Loaded \(records.count) SDK contact records", context: "ContactsManager") | ||
| while generation == loadGeneration { | ||
| try Task.checkCancellation() | ||
| let revision = contactsRevision | ||
| do { | ||
| let records = try await contactRecords() | ||
|
|
||
| Logger.debug("Loaded \(records.count) SDK contact records", context: "ContactsManager") | ||
|
|
||
| let loadedResult: (contacts: [PubkyContact], failures: Int, | ||
| missingFailures: Int, firstError: Error?) = await withTaskGroup(of: Result<PubkyContact, Error>.self) { group in | ||
| let overrides = Self.loadContactProfileOverrides() | ||
| for record in records { | ||
| group.addTask { | ||
| do { | ||
| let contact = try await Self.contact(from: record, overrides: overrides, includePlaceholder: true) | ||
| return .success(contact) | ||
| } catch { | ||
| Logger.warn( | ||
| "Failed to load contact data for '\(PubkyPublicKeyFormat.redacted(record.publicKey))': \(error)", | ||
| context: "ContactsManager" | ||
| ) | ||
| return .failure(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let loadedResult: (contacts: [PubkyContact], failures: Int, | ||
| missingFailures: Int, firstError: Error?) = await withTaskGroup(of: Result<PubkyContact, Error>.self) { group in | ||
| let overrides = Self.loadContactProfileOverrides() | ||
| for record in records { | ||
| group.addTask { | ||
| do { | ||
| let contact = try await Self.contact(from: record, overrides: overrides, includePlaceholder: true) | ||
| return .success(contact) | ||
| } catch { | ||
| Logger.warn( | ||
| "Failed to load contact data for '\(PubkyPublicKeyFormat.redacted(record.publicKey))': \(error)", | ||
| context: "ContactsManager" | ||
| ) | ||
| return .failure(error) | ||
| var results: [PubkyContact] = [] | ||
| var failures = 0 | ||
| var missingFailures = 0 | ||
| var firstError: Error? | ||
|
|
||
| for await result in group { | ||
| switch result { | ||
| case let .success(contact): | ||
| results.append(contact) | ||
| case let .failure(error): | ||
| failures += 1 | ||
| if Self.isMissingContactsDataError(error) { | ||
| missingFailures += 1 | ||
| } | ||
| firstError = firstError ?? error | ||
| } | ||
| } | ||
|
|
||
| return (results, failures, missingFailures, firstError) | ||
| } | ||
|
|
||
| var results: [PubkyContact] = [] | ||
| var failures = 0 | ||
| var missingFailures = 0 | ||
| var firstError: Error? | ||
| guard contactsRevision == revision else { continue } | ||
|
|
||
| for await result in group { | ||
| switch result { | ||
| case let .success(contact): | ||
| results.append(contact) | ||
| case let .failure(error): | ||
| failures += 1 | ||
| if Self.isMissingContactsDataError(error) { | ||
| missingFailures += 1 | ||
| } | ||
| firstError = firstError ?? error | ||
| if !records.isEmpty, loadedResult.contacts.isEmpty { | ||
| if loadedResult.failures == loadedResult.missingFailures { | ||
| contacts = [] | ||
| hasLoaded = true | ||
| await PrivatePaykitService.shared.pruneUnsavedContactState(savedPublicKeys: []) | ||
| Logger.info("Contacts storage entries were missing, treating list as empty", context: "ContactsManager") | ||
| return | ||
| } | ||
| throw loadedResult.firstError ?? PubkyServiceError.profileNotFound | ||
| } | ||
|
|
||
| return (results, failures, missingFailures, firstError) | ||
| } | ||
| contacts = loadedResult.contacts.sorted { $0.displayName.localizedCaseInsensitiveCompare($1.displayName) == .orderedAscending } | ||
| hasLoaded = true | ||
| await PrivatePaykitService.shared | ||
| .pruneUnsavedContactState(savedPublicKeys: records.compactMap { PubkyPublicKeyFormat.normalized($0.publicKey) }) | ||
|
|
||
| if !records.isEmpty, loadedResult.contacts.isEmpty { | ||
| if loadedResult.failures == loadedResult.missingFailures { | ||
| await PrivatePaykitService.shared.pruneUnsavedContactState(savedPublicKeys: []) | ||
| if loadedResult.failures > 0 { | ||
| Logger.warn( | ||
| "Skipped \(loadedResult.failures) unreadable contacts while loading list", | ||
| context: "ContactsManager" | ||
| ) | ||
| } | ||
|
|
||
| Logger.info("Loaded \(contacts.count) contacts", context: "ContactsManager") | ||
| return | ||
| } catch { | ||
| guard contactsRevision == revision else { continue } | ||
| if Self.isMissingContactsDataError(error) { | ||
| contacts = [] | ||
| hasLoaded = true | ||
| Logger.info("Contacts storage entries were missing, treating list as empty", context: "ContactsManager") | ||
| loadErrorMessage = nil | ||
| await PrivatePaykitService.shared.pruneUnsavedContactState(savedPublicKeys: []) | ||
| Logger.info("Contacts storage missing, treating list as empty", context: "ContactsManager") | ||
| return | ||
| } | ||
| throw loadedResult.firstError ?? PubkyServiceError.profileNotFound | ||
| } | ||
|
|
||
| contacts = loadedResult.contacts.sorted { $0.displayName.localizedCaseInsensitiveCompare($1.displayName) == .orderedAscending } | ||
| await PrivatePaykitService.shared | ||
| .pruneUnsavedContactState(savedPublicKeys: records.compactMap { PubkyPublicKeyFormat.normalized($0.publicKey) }) | ||
| hasLoaded = true | ||
|
|
||
| if loadedResult.failures > 0 { | ||
| Logger.warn( | ||
| "Skipped \(loadedResult.failures) unreadable contacts while loading list", | ||
| context: "ContactsManager" | ||
| ) | ||
| } | ||
|
|
||
| Logger.info("Loaded \(contacts.count) contacts", context: "ContactsManager") | ||
| } catch { | ||
| if Self.isMissingContactsDataError(error) { | ||
| await PrivatePaykitService.shared.pruneUnsavedContactState(savedPublicKeys: []) | ||
| contacts = [] | ||
| hasLoaded = true | ||
| loadErrorMessage = nil | ||
| Logger.info("Contacts storage missing, treating list as empty", context: "ContactsManager") | ||
| return | ||
| } | ||
|
|
||
| Logger.error("Failed to load contacts: \(error)", context: "ContactsManager") | ||
| if contacts.isEmpty { | ||
| loadErrorMessage = error.localizedDescription | ||
| Logger.error("Failed to load contacts: \(error)", context: "ContactsManager") | ||
| if contacts.isEmpty { | ||
| loadErrorMessage = error.localizedDescription | ||
| } | ||
| throw error | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -297,7 +319,12 @@ | |
| } | ||
|
|
||
| let receiverPaths = try await Self.relevantReceiverPaths(for: prefixedKey) | ||
| _ = try await PubkyService.saveContact(publicKey: prefixedKey, label: profile.name, receiverPaths: receiverPaths) | ||
| _ = try await PubkyService.saveContact( | ||
| publicKey: prefixedKey, | ||
| label: profile.name, | ||
| receiverPaths: receiverPaths, | ||
| restorePrivateConnection: true | ||
| ) | ||
|
|
||
| Logger.info("Added contact \(PubkyPublicKeyFormat.redacted(prefixedKey))", context: "ContactsManager") | ||
|
|
||
|
|
@@ -342,7 +369,12 @@ | |
| do { | ||
| let profile = try await resolveContactProfile(publicKey: key, includePlaceholder: true) | ||
| let receiverPaths = try await Self.relevantReceiverPaths(for: key) | ||
| _ = try await PubkyService.saveContact(publicKey: key, label: profile.name, receiverPaths: receiverPaths) | ||
| _ = try await PubkyService.saveContact( | ||
| publicKey: key, | ||
| label: profile.name, | ||
| receiverPaths: receiverPaths, | ||
| restorePrivateConnection: true | ||
| ) | ||
| return .success(PubkyContact(publicKey: key, profile: profile)) | ||
| } catch is CancellationError { | ||
| return .failure(CancellationError()) | ||
|
|
@@ -423,12 +455,11 @@ | |
| try await Task.detached { | ||
| _ = try await PubkyService.removeContact(publicKey: prefixedKey) | ||
| }.value | ||
| await PrivatePaykitService.shared.removeSavedContact(publicKey: prefixedKey) | ||
| contacts.removeAll { $0.publicKey == prefixedKey } | ||
| Self.removeContactProfileOverride(publicKey: prefixedKey) | ||
| await PrivatePaykitService.shared.removeSavedContact(publicKey: prefixedKey) | ||
|
|
||
| Logger.info("Removed contact \(PubkyPublicKeyFormat.redacted(prefixedKey))", context: "ContactsManager") | ||
|
|
||
| contacts.removeAll { $0.publicKey == prefixedKey } | ||
| } | ||
|
|
||
| func deleteAllContacts() async throws { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.