Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove folder when conversation list is empty [WPB-15892] #3285

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.wire.kalium.network.exceptions.KaliumException
import com.wire.kalium.persistence.dao.conversation.folder.ConversationFolderDAO
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map

internal interface ConversationFolderRepository {
Expand Down Expand Up @@ -142,6 +143,12 @@ internal class ConversationFolderDataSource internal constructor(
.v("Removing conversation ${conversationId.toLogString()} from folder ${folderId.obfuscateId()}")
return wrapStorageRequest {
conversationFolderDAO.removeConversationFromFolder(conversationId.toDao(), folderId)
val conversations = conversationFolderDAO.observeConversationListFromFolder(folderId).first()
if (conversations.isEmpty()) {
conversationFolderDAO.removeFolder(folderId)
} else {
Unit
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class ConversationFolderRepositoryTest {
val arrangement = Arrangement()
.withRemoveConversationFromFolder()
.withGetFoldersWithConversations()
.withConversationsFromFolder(folderId, listOf())
.withUpdateLabels(NetworkResponse.Success(Unit, mapOf(), 200))

// when
Expand Down Expand Up @@ -245,6 +246,52 @@ class ConversationFolderRepositoryTest {
coVerify { arrangement.conversationFolderDAO.addFolder(eq(folder.toDao())) }.wasInvoked()
}

@Test
fun givenLastConversationRemovedFromFolder_whenRemovingConversation_thenFolderShouldBeDeleted() = runTest {
// given
val folderId = "folder1"
val conversationId = TestConversation.ID

val arrangement = Arrangement()
.withRemoveConversationFromFolder()
.withConversationsFromFolder(folderId, emptyList())

// when
val result = arrangement.repository.removeConversationFromFolder(conversationId, folderId)

// then
result.shouldSucceed()

coVerify { arrangement.conversationFolderDAO.removeConversationFromFolder(eq(conversationId.toDao()), eq(folderId)) }.wasInvoked()
coVerify { arrangement.conversationFolderDAO.removeFolder(eq(folderId)) }.wasInvoked()
}

@Test
fun givenRemainingConversationsInFolder_whenRemovingConversation_thenFolderShouldNotBeDeleted() = runTest {
// given
val folderId = "folder1"
val conversationId = TestConversation.ID
val remainingConversation = ConversationDetailsWithEventsEntity(
conversationViewEntity = TestConversation.VIEW_ENTITY,
lastMessage = null,
messageDraft = null,
unreadEvents = ConversationUnreadEventEntity(TestConversation.VIEW_ENTITY.id, mapOf())
)

val arrangement = Arrangement()
.withRemoveConversationFromFolder()
.withConversationsFromFolder(folderId, listOf(remainingConversation))

// when
val result = arrangement.repository.removeConversationFromFolder(conversationId, folderId)

// then
result.shouldSucceed()

coVerify { arrangement.conversationFolderDAO.removeConversationFromFolder(eq(conversationId.toDao()), eq(folderId)) }.wasInvoked()
coVerify { arrangement.conversationFolderDAO.removeFolder(eq(folderId)) }.wasNotInvoked()
}

private class Arrangement {

@Mock
Expand Down
Loading