Skip to content

Commit

Permalink
feat: move to folder [WPB-14627] (#3213)
Browse files Browse the repository at this point in the history
* feat: move conversation to folder

* feat: move to folder use case

* refactor and added tests

* tests fix
  • Loading branch information
Garzas authored Jan 13, 2025
1 parent 47d6069 commit 4362a23
Show file tree
Hide file tree
Showing 18 changed files with 593 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,17 @@ sealed class ConversationDetails(open val conversation: Conversation) {
override val conversation: Conversation,
val otherUser: OtherUser,
val userType: UserType,
val isFavorite: Boolean = false
val isFavorite: Boolean = false,
val folder: ConversationFolder? = null
) : ConversationDetails(conversation)

data class Group(
override val conversation: Conversation,
val hasOngoingCall: Boolean = false,
val isSelfUserMember: Boolean,
val selfRole: Conversation.Member.Role?,
val isFavorite: Boolean = false
val isFavorite: Boolean = false,
val folder: ConversationFolder? = null
// val isTeamAdmin: Boolean, TODO kubaz
) : ConversationDetails(conversation)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ internal class ConversationMapperImpl(
activeOneOnOneConversationId = userActiveOneOnOneConversationId?.toModel()
),
userType = domainUserTypeMapper.fromUserTypeEntity(userType),
isFavorite = isFavorite
isFavorite = isFavorite,
folder = folderId?.let { ConversationFolder(it, folderName ?: "", type = FolderType.USER) },
)
}

Expand All @@ -273,7 +274,8 @@ internal class ConversationMapperImpl(
hasOngoingCall = callStatus != null, // todo: we can do better!
isSelfUserMember = isMember,
selfRole = selfRole?.let { conversationRoleMapper.fromDAO(it) },
isFavorite = isFavorite
isFavorite = isFavorite,
folder = folderId?.let { ConversationFolder(it, folderName ?: "", type = FolderType.USER) },
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal interface ConversationFolderRepository {
suspend fun addConversationToFolder(conversationId: QualifiedID, folderId: String): Either<CoreFailure, Unit>
suspend fun removeConversationFromFolder(conversationId: QualifiedID, folderId: String): Either<CoreFailure, Unit>
suspend fun syncConversationFoldersFromLocal(): Either<CoreFailure, Unit>
suspend fun observeUserFolders(): Flow<Either<CoreFailure, List<ConversationFolder>>>
suspend fun observeFolders(): Flow<Either<CoreFailure, List<ConversationFolder>>>
}

internal class ConversationFolderDataSource internal constructor(
Expand Down Expand Up @@ -155,8 +155,8 @@ internal class ConversationFolderDataSource internal constructor(
}
}

override suspend fun observeUserFolders(): Flow<Either<CoreFailure, List<ConversationFolder>>> {
return conversationFolderDAO.observeUserFolders()
override suspend fun observeFolders(): Flow<Either<CoreFailure, List<ConversationFolder>>> {
return conversationFolderDAO.observeFolders()
.wrapStorageRequest()
.mapRight { folderEntities -> folderEntities.map { it.toModel() } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import com.wire.kalium.logic.feature.conversation.folder.AddConversationToFavori
import com.wire.kalium.logic.feature.conversation.folder.AddConversationToFavoritesUseCaseImpl
import com.wire.kalium.logic.feature.conversation.folder.GetFavoriteFolderUseCase
import com.wire.kalium.logic.feature.conversation.folder.GetFavoriteFolderUseCaseImpl
import com.wire.kalium.logic.feature.conversation.folder.MoveConversationToFolderUseCase
import com.wire.kalium.logic.feature.conversation.folder.MoveConversationToFolderUseCaseImpl
import com.wire.kalium.logic.feature.conversation.folder.ObserveConversationsFromFolderUseCase
import com.wire.kalium.logic.feature.conversation.folder.ObserveConversationsFromFolderUseCaseImpl
import com.wire.kalium.logic.feature.conversation.folder.ObserveUserFoldersUseCase
Expand Down Expand Up @@ -386,4 +388,6 @@ class ConversationScope internal constructor(
get() = RemoveConversationFromFavoritesUseCaseImpl(conversationFolderRepository)
val observeUserFolders: ObserveUserFoldersUseCase
get() = ObserveUserFoldersUseCaseImpl(conversationFolderRepository)
val moveConversationToFolder: MoveConversationToFolderUseCase
get() = MoveConversationToFolderUseCaseImpl(conversationFolderRepository)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

package com.wire.kalium.logic.feature.conversation.folder

import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.conversation.folders.ConversationFolderRepository
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.withContext

/**
* This use case will move a conversation to the selected folder.
*/
interface MoveConversationToFolderUseCase {
/**
* @param conversationId the id of the conversation
* @param folderId the id of the conversation folder
* @param previousFolderId the id of the previous folder, if any
* @return the [Result] indicating a successful operation, otherwise a [CoreFailure]
*/
suspend operator fun invoke(
conversationId: ConversationId,
folderId: String,
previousFolderId: String?
): Result

sealed interface Result {
data object Success : Result
data class Failure(val cause: CoreFailure) : Result
}
}

internal class MoveConversationToFolderUseCaseImpl(
private val conversationFolderRepository: ConversationFolderRepository,
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl
) : MoveConversationToFolderUseCase {
override suspend fun invoke(
conversationId: ConversationId,
folderId: String,
previousFolderId: String?
): MoveConversationToFolderUseCase.Result = withContext(dispatchers.io) {
(
previousFolderId?.let {
conversationFolderRepository.removeConversationFromFolder(conversationId, it)
} ?: Either.Right(Unit)
)
.flatMap {
conversationFolderRepository.addConversationToFolder(
conversationId,
folderId
)
}
.flatMap { conversationFolderRepository.syncConversationFoldersFromLocal() }
.fold({
MoveConversationToFolderUseCase.Result.Failure(it)
}, {
MoveConversationToFolderUseCase.Result.Success
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package com.wire.kalium.logic.feature.conversation.folder

import com.wire.kalium.logic.data.conversation.ConversationFolder
import com.wire.kalium.logic.data.conversation.FolderType
import com.wire.kalium.logic.data.conversation.folders.ConversationFolderRepository
import com.wire.kalium.logic.functional.mapRight
import com.wire.kalium.logic.functional.mapToRightOr
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
Expand All @@ -39,7 +41,15 @@ internal class ObserveUserFoldersUseCaseImpl(
) : ObserveUserFoldersUseCase {

override suspend operator fun invoke(): Flow<List<ConversationFolder>> {
return conversationFolderRepository.observeUserFolders()
return conversationFolderRepository.observeFolders()
.mapRight { folders ->
if (folders.isEmpty()) {
conversationFolderRepository.fetchConversationFolders()
emptyList()
} else {
folders.filter { it.type == FolderType.USER }
}
}
.mapToRightOr(emptyList())
.flowOn(dispatchers.io)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Wire
* Copyright (C) 2025 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.feature.conversation.folder

import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.conversation.folders.ConversationFolderRepository
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.feature.conversation.folder.MoveConversationToFolderUseCase.Result
import com.wire.kalium.logic.framework.TestConversation
import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.mock
import io.mockative.once
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertIs

class MoveConversationToFolderUseCaseTest {

@Test
fun givenValidConversationAndFolder_WhenMoveIsSuccessful_ThenReturnSuccess() = runTest {
val testConversationId = TestConversation.ID
val testFolderId = "test-folder-id"
val previousFolderId = "previous-folder-id"

val (arrangement, moveConversationUseCase) = Arrangement()
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit))
.withAddConversationToFolder(testConversationId, testFolderId, Either.Right(Unit))
.withSyncFolders(Either.Right(Unit))
.arrange()

val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId)

assertIs<Result.Success>(result)

coVerify {
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId)
}.wasInvoked(exactly = once)

coVerify {
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId)
}.wasInvoked(exactly = once)

coVerify {
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal()
}.wasInvoked(exactly = once)
}

@Test
fun givenValidConversationAndFolder_WhenRemoveFails_ThenReturnFailure() = runTest {
val testConversationId = TestConversation.ID
val testFolderId = "test-folder-id"
val previousFolderId = "previous-folder-id"

val (arrangement, moveConversationUseCase) = Arrangement()
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Left(CoreFailure.Unknown(null)))
.arrange()

val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId)

assertIs<Result.Failure>(result)

coVerify {
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId)
}.wasInvoked(exactly = once)
}

@Test
fun givenValidConversationAndFolder_WhenAddFails_ThenReturnFailure() = runTest {
val testConversationId = TestConversation.ID
val testFolderId = "test-folder-id"
val previousFolderId = "previous-folder-id"

val (arrangement, moveConversationUseCase) = Arrangement()
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit))
.withAddConversationToFolder(testConversationId, testFolderId, Either.Left(CoreFailure.Unknown(null)))
.arrange()

val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId)

assertIs<Result.Failure>(result)

coVerify {
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId)
}.wasInvoked(exactly = once)

coVerify {
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId)
}.wasInvoked(exactly = once)
}

@Test
fun givenValidConversationAndFolder_WhenSyncFails_ThenReturnFailure() = runTest {
val testConversationId = TestConversation.ID
val testFolderId = "test-folder-id"
val previousFolderId = "previous-folder-id"

val (arrangement, moveConversationUseCase) = Arrangement()
.withRemoveConversationFromFolder(testConversationId, previousFolderId, Either.Right(Unit))
.withAddConversationToFolder(testConversationId, testFolderId, Either.Right(Unit))
.withSyncFolders(Either.Left(CoreFailure.Unknown(null)))
.arrange()

val result = moveConversationUseCase(testConversationId, testFolderId, previousFolderId)

assertIs<Result.Failure>(result)

coVerify {
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, previousFolderId)
}.wasInvoked(exactly = once)

coVerify {
arrangement.conversationFolderRepository.addConversationToFolder(testConversationId, testFolderId)
}.wasInvoked(exactly = once)

coVerify {
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal()
}.wasInvoked(exactly = once)
}

private class Arrangement {
@Mock
val conversationFolderRepository = mock(ConversationFolderRepository::class)

private val moveConversationToFolderUseCase = MoveConversationToFolderUseCaseImpl(
conversationFolderRepository
)

suspend fun withRemoveConversationFromFolder(
conversationId: ConversationId,
folderId: String,
either: Either<CoreFailure, Unit>
) = apply {
coEvery {
conversationFolderRepository.removeConversationFromFolder(conversationId, folderId)
}.returns(either)
}

suspend fun withAddConversationToFolder(
conversationId: ConversationId,
folderId: String,
either: Either<CoreFailure, Unit>
) = apply {
coEvery {
conversationFolderRepository.addConversationToFolder(conversationId, folderId)
}.returns(either)
}

suspend fun withSyncFolders(either: Either<CoreFailure, Unit>) = apply {
coEvery {
conversationFolderRepository.syncConversationFoldersFromLocal()
}.returns(either)
}

fun arrange(block: Arrangement.() -> Unit = { }) = apply(block).let { this to moveConversationToFolderUseCase }
}
}

Loading

0 comments on commit 4362a23

Please sign in to comment.