-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove conversation from folder [WPB-14630] (#3230)
* feat: remove conversation from folder * remove pragma key off * drop instead of move labeled conversations
- Loading branch information
Showing
11 changed files
with
387 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.../com/wire/kalium/logic/feature/conversation/folder/RemoveConversationFromFolderUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.flow.first | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* This use case will remove a conversation from the selected folder and if the folder is empty, it will remove the folder. | ||
*/ | ||
interface RemoveConversationFromFolderUseCase { | ||
/** | ||
* @param conversationId the id of the conversation | ||
* @param folderId the id of the folder | ||
* @return the [Result] indicating a successful operation, otherwise a [CoreFailure] | ||
*/ | ||
suspend operator fun invoke(conversationId: ConversationId, folderId: String): Result | ||
|
||
sealed interface Result { | ||
data object Success : Result | ||
data class Failure(val cause: CoreFailure) : Result | ||
} | ||
} | ||
|
||
internal class RemoveConversationFromFolderUseCaseImpl( | ||
private val conversationFolderRepository: ConversationFolderRepository, | ||
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl | ||
) : RemoveConversationFromFolderUseCase { | ||
override suspend fun invoke( | ||
conversationId: ConversationId, | ||
folderId: String | ||
): RemoveConversationFromFolderUseCase.Result = withContext(dispatchers.io) { | ||
conversationFolderRepository.removeConversationFromFolder(conversationId, folderId) | ||
.flatMap { | ||
if (conversationFolderRepository.observeConversationsFromFolder(folderId).first().isEmpty()) { | ||
conversationFolderRepository.removeFolder(folderId) | ||
} else { | ||
Either.Right(Unit) | ||
} | ||
} | ||
.flatMap { | ||
conversationFolderRepository.syncConversationFoldersFromLocal() | ||
} | ||
.fold({ | ||
RemoveConversationFromFolderUseCase.Result.Failure(it) | ||
}, { | ||
RemoveConversationFromFolderUseCase.Result.Success | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
.../wire/kalium/logic/feature/conversation/folder/RemoveConversationFromFolderUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/* | ||
* 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.ConversationDetailsWithEvents | ||
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.RemoveConversationFromFolderUseCase.Result | ||
import com.wire.kalium.logic.framework.TestConversationDetails | ||
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.flow.Flow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
|
||
class RemoveConversationFromFolderUseCaseTest { | ||
|
||
@Test | ||
fun givenValidConversationAndFolder_WhenRemoveAndSyncSuccessful_ThenReturnSuccess() = runTest { | ||
val testConversationId = ConversationId("conversation-value", "conversation-domain") | ||
val testFolderId = "test-folder-id" | ||
|
||
val (arrangement, removeConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, testFolderId, Either.Right(Unit)) | ||
.withObserveConversationsFromFolder(testFolderId, flowOf(emptyList())) | ||
.withRemoveFolder(testFolderId, Either.Right(Unit)) | ||
.withSyncFolders(Either.Right(Unit)) | ||
.arrange() | ||
|
||
val result = removeConversationUseCase(testConversationId, testFolderId) | ||
|
||
assertIs<Result.Success>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.observeConversationsFromFolder(testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeFolder(testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal() | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenFolderNotEmpty_WhenRemoveAndSyncSuccessful_ThenReturnSuccessWithoutFolderRemoval() = runTest { | ||
val testConversationId = ConversationId("conversation-value", "conversation-domain") | ||
val testFolderId = "test-folder-id" | ||
|
||
val (arrangement, removeConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, testFolderId, Either.Right(Unit)) | ||
.withObserveConversationsFromFolder( | ||
testFolderId, | ||
flowOf(listOf(ConversationDetailsWithEvents(TestConversationDetails.CONVERSATION_GROUP))) | ||
) | ||
.withSyncFolders(Either.Right(Unit)) | ||
.arrange() | ||
|
||
val result = removeConversationUseCase(testConversationId, testFolderId) | ||
|
||
assertIs<Result.Success>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.observeConversationsFromFolder(testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeFolder(testFolderId) | ||
}.wasNotInvoked() | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.syncConversationFoldersFromLocal() | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
@Test | ||
fun givenErrorDuringFolderRemoval_WhenObservedEmpty_ThenReturnFailure() = runTest { | ||
val testConversationId = ConversationId("conversation-value", "conversation-domain") | ||
val testFolderId = "test-folder-id" | ||
|
||
val (arrangement, removeConversationUseCase) = Arrangement() | ||
.withRemoveConversationFromFolder(testConversationId, testFolderId, Either.Right(Unit)) | ||
.withObserveConversationsFromFolder(testFolderId, flowOf(emptyList())) | ||
.withRemoveFolder(testFolderId, Either.Left(CoreFailure.Unknown(null))) | ||
.arrange() | ||
|
||
val result = removeConversationUseCase(testConversationId, testFolderId) | ||
|
||
assertIs<Result.Failure>(result) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeConversationFromFolder(testConversationId, testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.observeConversationsFromFolder(testFolderId) | ||
}.wasInvoked(exactly = once) | ||
|
||
coVerify { | ||
arrangement.conversationFolderRepository.removeFolder(testFolderId) | ||
}.wasInvoked(exactly = once) | ||
} | ||
|
||
private class Arrangement { | ||
@Mock | ||
val conversationFolderRepository = mock(ConversationFolderRepository::class) | ||
|
||
private val removeConversationFromFolderUseCase = RemoveConversationFromFolderUseCaseImpl( | ||
conversationFolderRepository | ||
) | ||
|
||
suspend fun withRemoveConversationFromFolder( | ||
conversationId: ConversationId, | ||
folderId: String, | ||
either: Either<CoreFailure, Unit> | ||
) = apply { | ||
coEvery { | ||
conversationFolderRepository.removeConversationFromFolder(conversationId, folderId) | ||
}.returns(either) | ||
} | ||
|
||
suspend fun withObserveConversationsFromFolder( | ||
folderId: String, | ||
flow: Flow<List<ConversationDetailsWithEvents>> | ||
) = apply { | ||
coEvery { | ||
conversationFolderRepository.observeConversationsFromFolder(folderId) | ||
}.returns(flow) | ||
} | ||
|
||
suspend fun withRemoveFolder( | ||
folderId: String, | ||
either: Either<CoreFailure, Unit> | ||
) = apply { | ||
coEvery { | ||
conversationFolderRepository.removeFolder(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 removeConversationFromFolderUseCase } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DROP TABLE LabeledConversation; | ||
|
||
CREATE TABLE LabeledConversation ( | ||
conversation_id TEXT AS QualifiedIDEntity NOT NULL, | ||
folder_id TEXT NOT NULL, | ||
|
||
FOREIGN KEY (folder_id) REFERENCES ConversationFolder(id) ON DELETE CASCADE ON UPDATE CASCADE, | ||
|
||
PRIMARY KEY (folder_id, conversation_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.