Skip to content

Commit

Permalink
fix: ignored connection stays in conversations list [#WPB-WPB-15212]
Browse files Browse the repository at this point in the history
  • Loading branch information
sbakhtiarov committed Jan 15, 2025
1 parent 4362a23 commit d8c7c5a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import com.wire.kalium.logic.functional.onSuccess
import com.wire.kalium.logic.kaliumLogger
import com.wire.kalium.logic.wrapApiRequest
import com.wire.kalium.logic.wrapStorageRequest
import com.wire.kalium.network.api.base.authenticated.connection.ConnectionApi
import com.wire.kalium.network.api.authenticated.connection.ConnectionDTO
import com.wire.kalium.network.api.authenticated.connection.ConnectionStateDTO
import com.wire.kalium.network.api.base.authenticated.connection.ConnectionApi
import com.wire.kalium.persistence.dao.ConnectionDAO
import com.wire.kalium.persistence.dao.ConnectionEntity
import com.wire.kalium.persistence.dao.UserDAO
Expand Down Expand Up @@ -150,15 +150,18 @@ internal class ConnectionDataSource(
override suspend fun ignoreConnectionRequest(userId: UserId): Either<CoreFailure, Unit> =
updateRemoteConnectionStatus(userId, IGNORED)
.flatMapLeft {
if (it is NetworkFailure.FederatedBackendFailure.FailedDomains)
wrapStorageRequest { connectionDAO.getConnectionByUser(userId.toDao()) }
.map { connectionEntity ->
val updatedConnection = connectionMapper.fromDaoToModel(connectionEntity).copy(status = IGNORED)
handleUserConnectionStatusPersistence(updatedConnection)
}
else it.left()
if (it is NetworkFailure.FederatedBackendFailure.FailedDomains) Either.Right(Unit) else it.left()
}
.map {
setConnectionStatus(userId, IGNORED)
}

private suspend fun setConnectionStatus(userId: UserId, status: ConnectionState): Either<CoreFailure, Unit> =
wrapStorageRequest { connectionDAO.getConnectionByUser(userId.toDao()) }
.map { connectionEntity ->
val updatedConnection = connectionMapper.fromDaoToModel(connectionEntity).copy(status = status)
handleUserConnectionStatusPersistence(updatedConnection)
}
.map { Unit }

/**
* Check if we can transition to the correct connection status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class ConnectionRepositoryTest {
}

@Test
fun givenAConnectionRequestIgnore_WhenSendingAConnectionStatusValid_thenTheConnectionShouldBePersisted() = runTest {
fun givenAConnectionRequestIgnore_WhenSendingAConnectionStatusValid_thenTheConnectionShouldBeUpdated() = runTest {
// given
val userId = NetworkUserId("user_id", "domain_id")
val (arrangement, connectionRepository) = Arrangement().arrange()
Expand All @@ -336,6 +336,29 @@ class ConnectionRepositoryTest {
}.wasInvoked(exactly = once)
}

@Test
fun givenAConnectionRequestIgnore_WhenSendingAConnectionStatusValid_thenTheConnectionShouldBePersisted() = runTest {
// given
val userId = NetworkUserId("user_id", "domain_id")
val (arrangement, connectionRepository) = Arrangement().arrange()
arrangement
.withGetConnectionByUser()
.withSuccessfulUpdateConnectionStatusResponse(userId)
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)

// when
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
result.shouldSucceed { arrangement.stubConnectionOne }

// then
coVerify {
arrangement.connectionDAO.insertConnection(arrangement.stubConnectionEntity.copy(
lastUpdateDate = any(),
status = ConnectionEntity.State.IGNORED
))
}.wasInvoked(exactly = once)
}

@Test
fun givenConnectionDoesNotExist_whenGettingConnection_thenErrorNotFoundShouldBeReturned() = runTest {
// given
Expand All @@ -354,7 +377,7 @@ class ConnectionRepositoryTest {
}

@Test
fun givenAConnectionRequestIgnore_WhenApiUpdateFailedWithFederatedFailedDomains_thenTheConnectionShouldBePersisted() = runTest {
fun givenAConnectionRequestIgnore_WhenApiUpdateFailedWithFederatedFailedDomains_thenTheConnectionShouldBeUpdated() = runTest {
// given
val userId = NetworkUserId("user_id", "domain_id")
val (arrangement, connectionRepository) = Arrangement().arrange()
Expand All @@ -376,6 +399,32 @@ class ConnectionRepositoryTest {
}.wasInvoked(exactly = once)
}

@Test
fun givenAConnectionRequestIgnore_WhenApiUpdateFailedWithFederatedFailedDomains_thenTheConnectionShouldBePersisted() = runTest {
// given
val userId = NetworkUserId("user_id", "domain_id")
val (arrangement, connectionRepository) = Arrangement().arrange()
arrangement
.withErrorUpdatingConnectionStatusResponse(
userId,
KaliumException.FederationUnreachableException(FederationUnreachableResponse())
)
.withConnectionEntityByUser()
.withSuccessfulFetchSelfUserConnectionsResponse(arrangement.stubUserProfileDTO)

// when
val result = connectionRepository.ignoreConnectionRequest(UserId(userId.value, userId.domain))
result.shouldSucceed { arrangement.stubConnectionOne }

// then
coVerify {
arrangement.connectionDAO.insertConnection(arrangement.stubConnectionEntity.copy(
lastUpdateDate = any(),
status = ConnectionEntity.State.IGNORED
))
}.wasInvoked(exactly = once)
}

@Test
fun givenAConnectionRequestIgnore_WhenApiUpdateFailedWithNonFederatedFailedDomains_thenTheConnectionNotShouldBePersisted() =
runTest {
Expand All @@ -392,8 +441,10 @@ class ConnectionRepositoryTest {

// then
coVerify {
arrangement.connectionApi.updateConnection(userId, ConnectionStateDTO.IGNORED)
}.wasInvoked(exactly = once)
arrangement.connectionDAO.insertConnection(arrangement.stubConnectionEntity.copy(
status = ConnectionEntity.State.IGNORED
))
}.wasInvoked(exactly = 0)
}

private class Arrangement :
Expand Down Expand Up @@ -622,6 +673,12 @@ class ConnectionRepositoryTest {
}.returns(connection)
}

suspend fun withGetConnectionByUser(): Arrangement = apply {
coEvery {
connectionDAO.getConnectionByUser(any())
}.returns(connectionEntity)
}

fun arrange() = this to connectionRepository
}
}

0 comments on commit d8c7c5a

Please sign in to comment.