From e23bbb6395e57435d19a755c7de47272802a1cce Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 08:49:07 +0100 Subject: [PATCH 1/6] chore: Update Add status in Dummy transfers --- .../database/dataset/DummyTransfer.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/dataset/DummyTransfer.kt b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/dataset/DummyTransfer.kt index 582806b2..10dd1323 100644 --- a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/dataset/DummyTransfer.kt +++ b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/dataset/DummyTransfer.kt @@ -20,6 +20,7 @@ package com.infomaniak.multiplatform_swisstransfer.database.dataset import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Container import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer +import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus object DummyTransfer { @@ -54,12 +55,14 @@ object DummyTransfer { override var isMailSent: Boolean = true override var downloadHost: String = "url" override var container: Container = this@DummyTransfer.container1 + override val transferStatus: TransferStatus = TransferStatus.READY } val transfer2 = object : Transfer by transfer1 { override val linkUUID: String = "transfer2" override val containerUUID: String = "container2" override val container: Container = container2 + override val transferStatus: TransferStatus = TransferStatus.WAIT_VIRUS_CHECK } val transfers = listOf(transfer1, transfer2) From d83be0c7f1506797cc78bee46bdcac1565d129a9 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 08:50:05 +0100 Subject: [PATCH 2/6] feat: Get not ready transfers --- .../database/controllers/TransferController.kt | 6 ++++++ .../database/models/transfers/TransferDB.kt | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt index 859a121b..4a7a7b38 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/controllers/TransferController.kt @@ -60,6 +60,12 @@ class TransferController(private val realmProvider: RealmProvider) { fun getTransfer(linkUUID: String): Transfer? = runThrowingRealm { return realm?.query("${TransferDB::linkUUID.name} == '$linkUUID'")?.first()?.find() } + + @Throws(RealmException::class) + fun getNotReadyTransfers(): List = runThrowingRealm { + val query = "${TransferDB.transferStatusPropertyName} != '${TransferStatus.READY.name}'" + return realm?.query(query)?.find() ?: emptyList() + } //endregion //region Upsert data diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/TransferDB.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/TransferDB.kt index 1a5bd885..0e1acbbe 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/TransferDB.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/models/transfers/TransferDB.kt @@ -58,7 +58,7 @@ class TransferDB() : Transfer, RealmObject { this.container = transfer.container?.let(::ContainerDB) this.transferDirectionValue = transferDirection.name - this.transferStatusValue = TransferStatus.READY.name + this.transferStatusValue = transfer.transferStatus?.name ?: TransferStatus.READY.name } constructor(linkUUID: String, uploadSession: UploadSession, transferStatus: TransferStatus) : this() { @@ -78,5 +78,6 @@ class TransferDB() : Transfer, RealmObject { internal companion object { val transferDirectionPropertyName = TransferDB::transferDirectionValue.name + val transferStatusPropertyName = TransferDB::transferStatusValue.name } } From df8533b0f05c27a8817a0b194594131e29d23fc5 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 08:50:37 +0100 Subject: [PATCH 3/6] test: Can get waiting transfers --- .../database/TransferControllerTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/TransferControllerTest.kt b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/TransferControllerTest.kt index aeb1ae1a..4df0263e 100644 --- a/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/TransferControllerTest.kt +++ b/STDatabase/src/commonTest/kotlin/com/infomaniak/multiplatform_swisstransfer/database/TransferControllerTest.kt @@ -19,6 +19,7 @@ package com.infomaniak.multiplatform_swisstransfer.database import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection +import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus import com.infomaniak.multiplatform_swisstransfer.database.controllers.TransferController import com.infomaniak.multiplatform_swisstransfer.database.dataset.DummyTransfer import kotlinx.coroutines.test.runTest @@ -68,6 +69,14 @@ class TransferControllerTest { canGetTransfersByDirection(TransferDirection.RECEIVED) } + @Test + fun canGetNotReadyTransfers() = runTest { + addTwoRandomTransfersInDatabase() + val transfers = transferController.getNotReadyTransfers() + assertEquals(transfers.count(), 1) + assertEquals(transfers.first().transferStatus, TransferStatus.WAIT_VIRUS_CHECK) + } + @Test fun canUpdateAnExistingTransfer() = runTest { // Insert a transfer From fdea28e2106b1a20814129aee5b2e06dd48c16f9 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 08:51:59 +0100 Subject: [PATCH 4/6] feat: Fetch waiting transfers from TransferManagers --- .../managers/TransferManager.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt index fc7a841e..8ad3fd5c 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt @@ -74,6 +74,18 @@ class TransferManager internal constructor( .flowOn(Dispatchers.IO) } + /** + * Update all pending transfers in database, most transfers are in [TransferStatus.WAIT_VIRUS_CHECK] status. + */ + suspend fun fetchWaitingTransfers(): Unit = withContext(Dispatchers.IO) { + val transfers = transferController.getNotReadyTransfers() + transfers.forEach { transfer -> + runCatching { + addTransferByLinkUUID(transfer.linkUUID, null) + } + } + } + /** * Retrieves a [TransferUi] by linkUUID. * @@ -109,11 +121,12 @@ class TransferManager internal constructor( UnknownException::class, RealmException::class, ) - suspend fun addTransferByLinkUUID(linkUUID: String, uploadSession: UploadSession): Unit = withContext(Dispatchers.IO) { + suspend fun addTransferByLinkUUID(linkUUID: String, uploadSession: UploadSession?): Unit = withContext(Dispatchers.IO) { runCatching { addTransfer(transferRepository.getTransferByLinkUUID(linkUUID).data, TransferDirection.SENT) }.onFailure { exception -> when { + uploadSession == null -> return@withContext exception is UnexpectedApiErrorFormatException && exception.bodyResponse.contains("wait_virus_check") -> { createTransferLocally(linkUUID, uploadSession) } From b9292080f4a0c7105b2c014a86e8fad0ff60f6b5 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 09:59:09 +0100 Subject: [PATCH 5/6] feat: Update a transfer form remote --- .../managers/TransferManager.kt | 23 +++++++++++++++++++ .../network/models/transfer/TransferApi.kt | 5 ++++ 2 files changed, 28 insertions(+) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt index 8ad3fd5c..257143c9 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt @@ -26,6 +26,8 @@ import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirectio import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus import com.infomaniak.multiplatform_swisstransfer.common.utils.mapToList import com.infomaniak.multiplatform_swisstransfer.database.controllers.TransferController +import com.infomaniak.multiplatform_swisstransfer.exceptions.NotFoundException +import com.infomaniak.multiplatform_swisstransfer.exceptions.NullPropertyException import com.infomaniak.multiplatform_swisstransfer.network.ApiClientProvider import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ApiException import com.infomaniak.multiplatform_swisstransfer.network.exceptions.NetworkException @@ -77,6 +79,7 @@ class TransferManager internal constructor( /** * Update all pending transfers in database, most transfers are in [TransferStatus.WAIT_VIRUS_CHECK] status. */ + @Throws(RealmException::class, CancellationException::class) suspend fun fetchWaitingTransfers(): Unit = withContext(Dispatchers.IO) { val transfers = transferController.getNotReadyTransfers() transfers.forEach { transfer -> @@ -86,6 +89,26 @@ class TransferManager internal constructor( } } + /** + * Update the local transfer with remote api + * + * @throws RealmException An error has occurred with realm database + * @throws NotFoundException Any transfer with [transferUUID] has been found + * @throws NullPropertyException The transferDirection of the transfer found is null + */ + @Throws(RealmException::class, NotFoundException::class, NullPropertyException::class, CancellationException::class) + suspend fun fetchTransfer(transferUUID: String): Unit { + val localTransfer = transferController.getTransfer(transferUUID) + ?: throw NotFoundException("No transfer found in DB with uuid = $transferUUID") + val transferDirection = localTransfer.transferDirection + ?: throw NullPropertyException("the transferDirection property cannot be null") + + runCatching { + val remoteTransfer = transferRepository.getTransferByLinkUUID(transferUUID).data ?: return + transferController.upsert(remoteTransfer, transferDirection) + } + } + /** * Retrieves a [TransferUi] by linkUUID. * diff --git a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/models/transfer/TransferApi.kt b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/models/transfer/TransferApi.kt index 5c54af91..27647321 100644 --- a/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/models/transfer/TransferApi.kt +++ b/STNetwork/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/network/models/transfer/TransferApi.kt @@ -18,10 +18,12 @@ package com.infomaniak.multiplatform_swisstransfer.network.models.transfer import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer +import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus import com.infomaniak.multiplatform_swisstransfer.network.serializers.DateToTimestampSerializer import com.infomaniak.multiplatform_swisstransfer.network.serializers.IntToBooleanSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient @Serializable class TransferApi : Transfer { @@ -45,4 +47,7 @@ class TransferApi : Transfer { val downloadUrl get() = "https://$downloadHost/api/download/??QuoiFeur " // TODO: Add download method url // https://dl-j5769qrh.swisstransfer.com/api/download/ec6bc7ac-96b3-4a6e-8d30-8e12e379d11a/97a742c9-b0f5-4fa5-b6bf-cb2c2d6bbe94 override var container: ContainerApi = ContainerApi() + + @Transient + override val transferStatus: TransferStatus = TransferStatus.READY } From 3e5a15413df189ff320f58864b617826d5038267 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 13 Nov 2024 10:04:10 +0100 Subject: [PATCH 6/6] chore: Update --- .../multiplatform_swisstransfer/managers/TransferManager.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt index 257143c9..98f76188 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt @@ -81,8 +81,7 @@ class TransferManager internal constructor( */ @Throws(RealmException::class, CancellationException::class) suspend fun fetchWaitingTransfers(): Unit = withContext(Dispatchers.IO) { - val transfers = transferController.getNotReadyTransfers() - transfers.forEach { transfer -> + transferController.getNotReadyTransfers().forEach { transfer -> runCatching { addTransferByLinkUUID(transfer.linkUUID, null) }