Skip to content

Commit

Permalink
fix: update user connection status after cancelling request (#2416)
Browse files Browse the repository at this point in the history
* fix: update user connection status after cancelling request (#2412)

* trigger build

---------

Co-authored-by: Michał Saleniuk <[email protected]>
Co-authored-by: Michał Saleniuk <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent c663cf9 commit 76d6385
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.conversation.ConversationRepository
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.toApi
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.user.Connection
Expand Down Expand Up @@ -54,6 +53,7 @@ import com.wire.kalium.network.api.base.authenticated.connection.ConnectionApi
import com.wire.kalium.network.api.base.authenticated.connection.ConnectionDTO
import com.wire.kalium.network.api.base.authenticated.connection.ConnectionStateDTO
import com.wire.kalium.persistence.dao.ConnectionDAO
import com.wire.kalium.persistence.dao.ConnectionEntity
import com.wire.kalium.persistence.dao.UserDAO
import com.wire.kalium.persistence.dao.conversation.ConversationDAO
import com.wire.kalium.persistence.dao.conversation.ConversationEntity
Expand All @@ -74,7 +74,7 @@ interface ConnectionRepository {
suspend fun observeConnectionRequestsForNotification(): Flow<List<ConversationDetails>>
suspend fun setConnectionAsNotified(userId: UserId)
suspend fun setAllConnectionsAsNotified()
suspend fun deleteConnection(conversationId: ConversationId): Either<StorageFailure, Unit>
suspend fun deleteConnection(connection: Connection): Either<StorageFailure, Unit>
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -249,8 +249,9 @@ internal class ConnectionDataSource(
}
}

override suspend fun deleteConnection(conversationId: ConversationId) = wrapStorageRequest {
connectionDAO.deleteConnectionDataAndConversation(conversationId.toDao())
override suspend fun deleteConnection(connection: Connection) = wrapStorageRequest {
connectionDAO.deleteConnectionDataAndConversation(connection.qualifiedConversationId.toDao())
userDAO.upsertConnectionStatuses(mapOf(connection.qualifiedToId.toDao() to ConnectionEntity.State.CANCELLED))
}

/**
Expand All @@ -260,6 +261,6 @@ internal class ConnectionDataSource(
private suspend fun handleUserConnectionStatusPersistence(connection: Connection): Either<CoreFailure, Unit> =
when (connection.status) {
ACCEPTED, MISSING_LEGALHOLD_CONSENT, NOT_CONNECTED, PENDING, SENT, BLOCKED, IGNORED -> persistConnection(connection)
CANCELLED -> deleteConnection(connection.qualifiedConversationId)
CANCELLED -> deleteConnection(connection)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class FederationEventReceiverImpl internal constructor(
if (conversationDetails is ConversationDetails.Connection
&& conversationDetails.otherUser?.id?.domain == event.domain
) {
connectionRepository.deleteConnection(conversationDetails.conversationId)
connectionRepository.deleteConnection(conversationDetails.connection)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.id.SelfTeamIdProvider
import com.wire.kalium.logic.framework.TestConnection
import com.wire.kalium.logic.framework.TestConversation
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
Expand All @@ -44,6 +45,7 @@ import com.wire.kalium.network.api.base.model.UserProfileDTO
import com.wire.kalium.network.exceptions.KaliumException
import com.wire.kalium.network.utils.NetworkResponse
import com.wire.kalium.persistence.dao.ConnectionDAO
import com.wire.kalium.persistence.dao.ConnectionEntity
import com.wire.kalium.persistence.dao.QualifiedIDEntity
import com.wire.kalium.persistence.dao.UserDAO
import com.wire.kalium.persistence.dao.UserIDEntity
Expand Down Expand Up @@ -284,19 +286,27 @@ class ConnectionRepositoryTest {
@Test
fun givenConversationId_WhenDeletingConnection_thenDeleteConnectionDataAndConversationShouldBeTriggered() = runTest {
// given
val conversationId = com.wire.kalium.logic.data.id.QualifiedID("conversation_id", "domain_id")
val conversationId = TestConversation.ID
val (arrangement, connectionRepository) = Arrangement().arrange()
arrangement.withDeleteConnectionDataAndConversation(conversationId.toDao())
val connection = TestConnection.CONNECTION.copy(
conversationId = conversationId.value,
qualifiedConversationId = conversationId,
)

// when
val result = connectionRepository.deleteConnection(conversationId)
val result = connectionRepository.deleteConnection(connection)

// then
result.shouldSucceed()
verify(arrangement.connectionDAO)
.suspendFunction(arrangement.connectionDAO::deleteConnectionDataAndConversation)
.with(eq(conversationId.toDao()))
.wasInvoked(once)
verify(arrangement.userDAO)
.suspendFunction(arrangement.userDAO::upsertConnectionStatuses)
.with(eq(mapOf(connection.qualifiedToId.toDao() to ConnectionEntity.State.CANCELLED)))
.wasInvoked(once)
}

private class Arrangement :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package com.wire.kalium.logic.sync.receiver

import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.user.Connection
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.framework.TestConnection
import com.wire.kalium.logic.framework.TestConversationDetails
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.functional.Either
Expand Down Expand Up @@ -51,17 +52,24 @@ class FederationEventReceiverTest {
@Test
fun givenConversationsWithFederatedUsers_whenReceivingFederationDeleteEvent_thenAllConversationsWithThemShouldBeCleared() = runTest {
// Given
fun createConnection(conversationId: ConversationId, otherUserId: UserId) = TestConversationDetails.CONNECTION.copy(
conversationId = conversationId,
otherUser = TestUser.OTHER.copy(id = otherUserId),
connection = TestConnection.CONNECTION.copy(
qualifiedConversationId = conversationId,
conversationId = conversationId.value,
)
)
val defederatedConnections = List(defederatedUsersCount) {
TestConversationDetails.CONNECTION.copy(
createConnection(
conversationId = ConversationId("def_connection$it", defederatedDomain),
otherUser = TestUser.OTHER.copy(id = QualifiedID("connectionDefId$it", defederatedDomain))
otherUserId = UserId("connectionDefId$it", defederatedDomain)
)
}

val otherConnections = List(defederatedUsersCount) {
TestConversationDetails.CONNECTION.copy(
createConnection(
conversationId = ConversationId("other_connection$it", otherDomain),
otherUser = TestUser.OTHER.copy(id = QualifiedID("connectionOtherId$it", otherDomain))
otherUserId = UserId("connectionOtherId$it", otherDomain)
)
}

Expand Down Expand Up @@ -109,7 +117,7 @@ class FederationEventReceiverTest {

verify(arrangement.connectionRepository)
.suspendFunction(arrangement.connectionRepository::deleteConnection)
.with(matching<ConversationId> { it.domain == defederatedDomain })
.with(matching<Connection> { it.qualifiedConversationId.domain == defederatedDomain })
.wasInvoked(exactly = defederatedConnections.size.time)

verify(arrangement.connectionRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.connection.ConnectionRepository
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.Connection
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.feature.connection.AcceptConnectionRequestUseCaseTest
import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.any
import io.mockative.eq
import io.mockative.given
import io.mockative.matchers.Matcher
import io.mockative.mock
Expand All @@ -38,7 +34,7 @@ internal interface ConnectionRepositoryArrangement {
val connectionRepository: ConnectionRepository

fun withGetConnections(result: Either<StorageFailure, Flow<List<ConversationDetails>>>)
fun withDeleteConnection(result: Either<StorageFailure, Unit>, conversationId: Matcher<ConversationId> = any())
fun withDeleteConnection(result: Either<StorageFailure, Unit>, connection: Matcher<Connection> = any())
fun withConnectionList(connectionsFlow: Flow<List<ConversationDetails>>)
fun withUpdateConnectionStatus(result: Either<CoreFailure, Connection>)
}
Expand All @@ -58,11 +54,11 @@ internal open class ConnectionRepositoryArrangementImpl : ConnectionRepositoryAr

override fun withDeleteConnection(
result: Either<StorageFailure, Unit>,
conversationId: Matcher<ConversationId>,
connection: Matcher<Connection>,
) {
given(connectionRepository)
.suspendFunction(connectionRepository::deleteConnection)
.whenInvokedWith(conversationId)
.whenInvokedWith(connection)
.thenReturn(result)
}

Expand Down

0 comments on commit 76d6385

Please sign in to comment.