Skip to content
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

Restore and transaction fixes #68

Merged
merged 1 commit into from
Oct 29, 2024
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 @@ -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<Payment>? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {}
}
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
)
}
}
}

Expand Down Expand Up @@ -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 {
Expand Down