Skip to content

Commit

Permalink
feat: avatar upload improvements (WPB-11170) (#3089)
Browse files Browse the repository at this point in the history
* refactor: move dispatcher to io for upload avatar

* refactor: tests
  • Loading branch information
yamilmedina authored Nov 6, 2024
1 parent 4667ea7 commit 82f3626
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 82f3626

Please sign in to comment.