From 3e13fc141b78984e5d9a9e00f23ba7880faed24d Mon Sep 17 00:00:00 2001 From: Tomas Timinskas Date: Tue, 29 Oct 2024 11:11:48 -0300 Subject: [PATCH] - Fix restore progress - Fix updating owner during restore - Fix for failed transactions showing as succeeded --- .../sphinx/example/wrapper_mqtt/NewPayment.kt | 3 +- .../concept_connect_manager/ConnectManager.kt | 4 +-- .../ConnectManagerImpl.kt | 19 +++++++----- .../feature_repository/SphinxRepository.kt | 31 ++++++++++--------- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/sphinx/application/common/wrappers/wrapper-mqtt/src/main/java/chat/sphinx/example/wrapper_mqtt/NewPayment.kt b/sphinx/application/common/wrappers/wrapper-mqtt/src/main/java/chat/sphinx/example/wrapper_mqtt/NewPayment.kt index 2602b5b7..7d371ee7 100644 --- a/sphinx/application/common/wrappers/wrapper-mqtt/src/main/java/chat/sphinx/example/wrapper_mqtt/NewPayment.kt +++ b/sphinx/application/common/wrappers/wrapper-mqtt/src/main/java/chat/sphinx/example/wrapper_mqtt/NewPayment.kt @@ -12,7 +12,8 @@ data class Payment( val rhash: String?, val ts: Long?, val remote: Boolean?, - val msg_idx: Long? + val msg_idx: Long?, + val error: String? ) { companion object { fun String.toPaymentsList(moshi: Moshi): List? { diff --git a/sphinx/application/data/concepts/concept-connect-manager/src/main/java/chat/sphinx/example/concept_connect_manager/ConnectManager.kt b/sphinx/application/data/concepts/concept-connect-manager/src/main/java/chat/sphinx/example/concept_connect_manager/ConnectManager.kt index 2d27d0d3..0c7375b1 100644 --- a/sphinx/application/data/concepts/concept-connect-manager/src/main/java/chat/sphinx/example/concept_connect_manager/ConnectManager.kt +++ b/sphinx/application/data/concepts/concept-connect-manager/src/main/java/chat/sphinx/example/concept_connect_manager/ConnectManager.kt @@ -37,8 +37,8 @@ abstract class ConnectManager { abstract fun setNetworkType(isTestEnvironment: Boolean) abstract fun setOwnerDeviceId(deviceId: String, pushKey: String) abstract fun processChallengeSignature(challenge: String): String? - abstract fun fetchFirstMessagesPerKey(lastMsgIdx: Long, firstForEachScid: Long?) - abstract fun fetchMessagesOnRestoreAccount(totalHighestIndex: Long?) + abstract fun fetchFirstMessagesPerKey(lastMsgIdx: Long, totalCount: Long?) + abstract fun fetchMessagesOnRestoreAccount(totalHighestIndex: Long?, totalMsgsCount: Long?) abstract fun getAllMessagesCount() abstract fun initializeMqttAndSubscribe( serverUri: String, diff --git a/sphinx/application/data/features/feature-connect-manager/src/main/java/chat/sphinx/feature_connect_manager/ConnectManagerImpl.kt b/sphinx/application/data/features/feature-connect-manager/src/main/java/chat/sphinx/feature_connect_manager/ConnectManagerImpl.kt index 985b2110..6dc32ba2 100644 --- a/sphinx/application/data/features/feature-connect-manager/src/main/java/chat/sphinx/feature_connect_manager/ConnectManagerImpl.kt +++ b/sphinx/application/data/features/feature-connect-manager/src/main/java/chat/sphinx/feature_connect_manager/ConnectManagerImpl.kt @@ -664,7 +664,7 @@ class ConnectManagerImpl: ConnectManager() msgsCountsState.value?.first_for_each_scid_highest_index?.let { highestIndex -> if (nnHighestIndex < highestIndex) { - fetchFirstMessagesPerKey(nnHighestIndex.plus(1L),null) + fetchFirstMessagesPerKey(nnHighestIndex.plus(1L), msgsCountsState.value?.ok_key) } else { goToNextPhaseOrFinish() } @@ -676,7 +676,7 @@ class ConnectManagerImpl: ConnectManager() val minIndex = msgs.minByOrNull { it.index?.toLong() ?: 0L }?.index?.toULong() minIndex?.let { nnMinIndex -> calculateMessageRestore() - fetchMessagesOnRestoreAccount(nnMinIndex.minus(1u).toLong()) + fetchMessagesOnRestoreAccount(nnMinIndex.minus(1u).toLong(), msgsCountsState.value?.total) notifyListeners { onRestoreMinIndex(nnMinIndex.toLong()) @@ -929,11 +929,11 @@ class ConnectManagerImpl: ConnectManager() return null } - override fun fetchFirstMessagesPerKey(lastMsgIdx: Long, firstForEachScid: Long?) { + override fun fetchFirstMessagesPerKey(lastMsgIdx: Long, totalCount: Long?) { try { if (lastMsgIdx == 0L) { _restoreStateFlow.value = RestoreState.RestoringContacts - setContactKeyTotal(firstForEachScid) + setContactKeyTotal(totalCount) } val limit = MSG_FIRST_PER_KEY_LIMIT @@ -954,11 +954,14 @@ class ConnectManagerImpl: ConnectManager() } } - override fun fetchMessagesOnRestoreAccount(totalHighestIndex: Long?) { + override fun fetchMessagesOnRestoreAccount( + totalHighestIndex: Long?, + totalMsgsCount: Long? + ) { try { if (restoreStateFlow.value !is RestoreState.RestoringMessages) { _restoreStateFlow.value = RestoreState.RestoringMessages - setMessagesTotal(totalHighestIndex) + setMessagesTotal(totalMsgsCount) } val fetchMessages = fetchMsgsBatch( @@ -2370,8 +2373,8 @@ class ConnectManagerImpl: ConnectManager() } } - private fun setMessagesTotal(totalHighestIndex: Long?) { - totalHighestIndex?.let { + private fun setMessagesTotal(totalMsgs: Long?) { + totalMsgs?.let { restoreProgress.totalMessages = it.toInt() } } diff --git a/sphinx/application/data/features/feature-repository/src/main/java/chat/sphinx/feature_repository/SphinxRepository.kt b/sphinx/application/data/features/feature-repository/src/main/java/chat/sphinx/feature_repository/SphinxRepository.kt index 25b8ab21..a1321e8c 100644 --- a/sphinx/application/data/features/feature-repository/src/main/java/chat/sphinx/feature_repository/SphinxRepository.kt +++ b/sphinx/application/data/features/feature-repository/src/main/java/chat/sphinx/feature_repository/SphinxRepository.kt @@ -341,7 +341,10 @@ abstract class SphinxRepository( } is RestoreProcessState.RestoreMessages -> { delay(100L) - connectManager.fetchMessagesOnRestoreAccount(msgCounts?.total_highest_index) + connectManager.fetchMessagesOnRestoreAccount( + msgCounts?.total_highest_index, + msgCounts?.total + ) } else -> {} } @@ -867,18 +870,6 @@ abstract class SphinxRepository( } } -// override fun onRestoreNextPageMessages(highestIndex: Long, limit: Int) { -// applicationScope.launch(io) { -// val nextHighestIndex = highestIndex.minus(limit) -// if (nextHighestIndex > 0) { -// delay(200L) -// connectManager.fetchMessagesOnRestoreAccount(nextHighestIndex) -// } else { -// // Restore complete -// } -// } -// } - override fun onNewBalance(balance: Long) { applicationScope.launch(io) { @@ -1448,7 +1439,7 @@ abstract class SphinxRepository( payment_request = null, date = payment.ts?.toDateTime(), reply_uuid = null, - error_message = null + error_message = payment.error ) } }.orEmpty() @@ -1674,6 +1665,12 @@ abstract class SphinxRepository( msgSender.alias?.takeIf { it.isNotEmpty() && it != owner.alias?.value } ?.let { queries.contactUpdateAlias(it.toContactAlias(), owner.id) + + connectManager.ownerInfoStateFlow.value?.let { ownerInfo -> + connectManager.setOwnerInfo( + ownerInfo.copy(alias = it) + ) + } } } @@ -2852,6 +2849,12 @@ abstract class SphinxRepository( updatedAt = now ) + connectManager.ownerInfoStateFlow.value?.let { ownerInfo -> + connectManager.setOwnerInfo( + ownerInfo.copy(alias = alias.value) + ) + } + if (updatedOwner != null) { applicationScope.launch(mainImmediate) { contactLock.withLock {