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: ignored connection stays in conversations list [#WPB-WPB-15212] #3231

Merged
merged 1 commit into from
Jan 15, 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 @@ -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()
}
.onSuccess {
setConnectionStatus(userId, IGNORED)
}.map { Unit }

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
}
}
Loading