Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ContactPaymentSettingsRepo @Inject constructor(
}

private suspend fun enable(contacts: List<String>): Result<Unit> {
val signIn = pubkyRepo.currentSignIn()
val previous = settingsStore.data.first()
val canUsePrivateContactPayments = runSuspendCatching {
privatePaykitRepo.hasPrivatePaymentAccess()
Expand All @@ -92,16 +93,23 @@ class ContactPaymentSettingsRepo @Inject constructor(
publicPaykitRepo.syncPublishedEndpoints(publish = true).getOrThrow()

if (canUsePrivateContactPayments) {
privatePaykitRepo.enableSharingAndPrepareSavedContacts(
publicKeys = contacts,
).getOrThrow()
prepareCurrentSavedContacts(signIn).getOrThrow()
}
}.onFailure { rollbackEnabled(previous, contacts, it) }
}.onFailure { rollbackEnabled(previous, contacts, signIn, it) }
}

private suspend fun prepareCurrentSavedContacts(signIn: PubkySignIn?): Result<Unit> {
if (signIn == null || !pubkyRepo.isCurrent(signIn)) return Result.success(Unit)
return privatePaykitRepo.enableSharingAndPrepareSavedContacts(
savedPublicKeys = { pubkyRepo.contacts.value.map { it.publicKey }.toSet() },
isStillCurrent = { pubkyRepo.isCurrent(signIn) },
)
}

private suspend fun rollbackEnabled(
previous: SettingsData,
contacts: List<String>,
signIn: PubkySignIn?,
error: Throwable,
) {
runSuspendCatching {
Expand All @@ -128,9 +136,7 @@ class ContactPaymentSettingsRepo @Inject constructor(
markPublicPaykitRetry(error)
}
if (previous.sharesPrivatePaykitEndpoints) {
privatePaykitRepo.enableSharingAndPrepareSavedContacts(
publicKeys = contacts,
).onFailure(error::addSuppressed)
prepareCurrentSavedContacts(signIn).onFailure(error::addSuppressed)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,13 @@ class PrivatePaykitAddressReservationRepo @Inject constructor(
return lightningRepo.getAddressBalance(address).getOrDefault(0u) > 0u
}

suspend fun clearContactAssignments(excludingPublicKeys: Collection<String>) = withContext(ioDispatcher) {
suspend fun clearContactAssignments(
excludingPublicKeys: Collection<String>,
isStillCurrent: (() -> Boolean)? = null,
) = withContext(ioDispatcher) {
val savedKeys = excludingPublicKeys.mapNotNull { normalizedPublicKeyOrNull(it) }.toSet()
locked { current ->
if (isStillCurrent?.invoke() == false) return@locked
val next = current.copy(
contactAssignments = current.contactAssignments.filterKeys {
it in savedKeys
Expand All @@ -217,9 +221,13 @@ class PrivatePaykitAddressReservationRepo @Inject constructor(
}
}

suspend fun removeContactAssignments(publicKeys: Collection<String>) = withContext(ioDispatcher) {
suspend fun removeContactAssignments(
publicKeys: Collection<String>,
isStillCurrent: (() -> Boolean)? = null,
) = withContext(ioDispatcher) {
val removedKeys = publicKeys.mapNotNull { normalizedPublicKeyOrNull(it) }.toSet()
locked { current ->
if (isStillCurrent?.invoke() == false) return@locked
val next = current.copy(
contactAssignments = current.contactAssignments.filterKeys { it !in removedKeys },
contactAssignmentHistory = current.contactAssignmentHistory.filterKeys { it !in removedKeys },
Expand Down
Loading
Loading