diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCase.kt index b7fc56a550..8bbc0e12e0 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCase.kt @@ -25,6 +25,8 @@ import com.wire.kalium.logic.data.user.UserRepository import com.wire.kalium.logic.functional.flatMap import com.wire.kalium.logic.functional.fold import com.wire.kalium.logic.functional.map +import com.wire.kalium.util.KaliumDispatcherImpl +import kotlinx.coroutines.withContext import okio.Path interface UploadUserAvatarUseCase { @@ -40,17 +42,20 @@ interface UploadUserAvatarUseCase { internal class UploadUserAvatarUseCaseImpl( private val userDataSource: UserRepository, - private val assetDataSource: AssetRepository + private val assetDataSource: AssetRepository, + private val dispatcher: KaliumDispatcherImpl = KaliumDispatcherImpl ) : UploadUserAvatarUseCase { override suspend operator fun invoke(imageDataPath: Path, imageDataSize: Long): UploadAvatarResult { - return assetDataSource.uploadAndPersistPublicAsset("image/jpg", imageDataPath, imageDataSize).flatMap { asset -> - userDataSource.updateSelfUser(newAssetId = asset.key).map { asset } - }.fold({ - UploadAvatarResult.Failure(it) - }) { updatedAsset -> - UploadAvatarResult.Success(UserAssetId(updatedAsset.key, updatedAsset.domain)) - } // TODO(assets): remove old assets, non blocking this response, as will imply deleting locally and remotely + return withContext(dispatcher.io) { + assetDataSource.uploadAndPersistPublicAsset("image/jpg", imageDataPath, imageDataSize).flatMap { asset -> + userDataSource.updateSelfUser(newAssetId = asset.key).map { asset } + }.fold({ + UploadAvatarResult.Failure(it) + }) { updatedAsset -> + UploadAvatarResult.Success(UserAssetId(updatedAsset.key, updatedAsset.domain)) + } // TODO(assets): remove old assets, non blocking this response, as will imply deleting locally and remotely + } } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCaseTest.kt index 943cac7932..02108d9c82 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/UploadUserAvatarUseCaseTest.kt @@ -21,15 +21,9 @@ package com.wire.kalium.logic.feature.user import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.data.asset.AssetRepository import com.wire.kalium.logic.data.asset.UploadedAssetId -import com.wire.kalium.logic.data.user.ConnectionState -import com.wire.kalium.logic.data.user.SelfUser -import com.wire.kalium.logic.data.user.SupportedProtocol -import com.wire.kalium.logic.data.user.UserAssetId -import com.wire.kalium.logic.data.user.UserAvailabilityStatus -import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.data.user.UserRepository -import com.wire.kalium.logic.data.user.type.UserType import com.wire.kalium.logic.functional.Either +import com.wire.kalium.util.KaliumDispatcherImpl import io.mockative.Mock import io.mockative.any import io.mockative.coEvery @@ -106,29 +100,15 @@ class UploadUserAvatarUseCaseTest { @Mock val userRepository = mock(UserRepository::class) - private val uploadUserAvatarUseCase: UploadUserAvatarUseCase = UploadUserAvatarUseCaseImpl(userRepository, assetRepository) + val dispatcher = KaliumDispatcherImpl + + private val uploadUserAvatarUseCase: UploadUserAvatarUseCase = + UploadUserAvatarUseCaseImpl(userRepository, assetRepository, dispatcher) var userHomePath = "/Users/me".toPath() val fakeFileSystem = FakeFileSystem().also { it.createDirectories(userHomePath) } - private val dummySelfUser = SelfUser( - id = UserId("some_id", "some_domain"), - name = "some_name", - handle = "some_handle", - email = "some_email", - phone = null, - accentId = 1, - teamId = null, - connectionStatus = ConnectionState.ACCEPTED, - previewPicture = UserAssetId("value1", "domain"), - completePicture = UserAssetId("value2", "domain"), - userType = UserType.INTERNAL, - availabilityStatus = UserAvailabilityStatus.NONE, - expiresAt = null, - supportedProtocols = setOf(SupportedProtocol.PROTEUS) - ) - fun withStoredData(data: ByteArray, dataNamePath: Path): Arrangement { val fullDataPath = "$userHomePath/$dataNamePath".toPath() fakeFileSystem.write(fullDataPath) {