Skip to content

Commit

Permalink
resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mchenani committed May 24, 2024
1 parent b070fcb commit 5cc7bd6
Showing 1 changed file with 77 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.wire.kalium.logic.data.id.toApi
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.data.id.toModel
import com.wire.kalium.logic.data.message.MessageContent.MemberChange.FailedToAdd
import com.wire.kalium.logic.data.mls.CipherSuite
import com.wire.kalium.logic.data.service.ServiceId
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
Expand Down Expand Up @@ -130,31 +131,21 @@ internal class ConversationGroupRepositoryImpl(
}

when (apiResult) {
is Either.Left -> {
val canRetryOnce = apiResult.value.hasUnreachableDomainsError && lastUsersAttempt is LastUsersAttempt.None
if (canRetryOnce) {
extractValidUsersForRetryableError(apiResult.value, usersList)
.flatMap { (validUsers, failedUsers, failType) ->
// edge case, in case backend goes 🍌 and returns non-matching domains
if (failedUsers.isEmpty()) Either.Left(apiResult.value)
}
}
}
is Either.Right -> {
handleCreateConversationSuccess(
apiResult,
usersList,
failedUsersList,
selfTeamId
)
}
is Either.Left -> handleCreateConverstionFailure(
apiResult = apiResult,
usersList = usersList,
name = name,
options = options,
lastUsersAttempt = lastUsersAttempt
)

is Either.Right -> handleGroupConversationCreated(apiResult.value, selfTeamId, usersList, lastUsersAttempt)
}
}

private suspend fun handleCreateConversationSuccess(
apiResult: Either.Right<ConversationResponse>,
private suspend fun handleGroupConversationCreated(
conversationResponse: ConversationResponse,
selfTeamId: TeamId?,
usersList: List<UserId>,
lastUsersAttempt: LastUsersAttempt,
): Either<CoreFailure, Conversation> {
Expand Down Expand Up @@ -184,7 +175,9 @@ internal class ConversationGroupRepositoryImpl(
Either.Right(Unit)
} else {
newGroupConversationSystemMessagesCreator.value.conversationFailedToAddMembers(
conversationEntity.id.toModel(), protocolSpecificAdditionFailures.toList(), FailedToAdd.Type.Unknown
conversationId = conversationEntity.id.toModel(),
userIdList = protocolSpecificAdditionFailures.toList(),
type = FailedToAdd.Type.Federation
)
}
}.flatMap {
Expand All @@ -211,6 +204,27 @@ internal class ConversationGroupRepositoryImpl(
}
}

private suspend fun handleCreateConverstionFailure(
apiResult: Either.Left<NetworkFailure>,
usersList: List<UserId>,
name: String?,
options: ConversationOptions,
lastUsersAttempt: LastUsersAttempt
): Either<CoreFailure, Conversation> {
val canRetryOnce = apiResult.value.hasUnreachableDomainsError && lastUsersAttempt is LastUsersAttempt.None
return if (canRetryOnce) {
extractValidUsersForRetryableError(apiResult.value, usersList)
.flatMap { (validUsers, failedUsers, failType) ->
// edge case, in case backend goes 🍌 and returns non-matching domains
if (failedUsers.isEmpty()) Either.Left(apiResult.value)

createGroupConversation(name, validUsers, options, LastUsersAttempt.Failed(failedUsers, failType))
}
} else {
Either.Left(apiResult.value)
}
}

override suspend fun addMembers(
userIdList: List<UserId>,
conversationId: ConversationId
Expand All @@ -225,11 +239,21 @@ internal class ConversationGroupRepositoryImpl(
tryAddMembersToCloudAndStorage(userIdList, conversationId, LastUsersAttempt.None)
.flatMap {
// best effort approach for migrated conversations, no retries
mlsConversationRepository.addMemberToMLSGroup(GroupID(protocol.groupId), userIdList)
mlsConversationRepository.addMemberToMLSGroup(
GroupID(protocol.groupId),
userIdList,
CipherSuite.fromTag(protocol.cipherSuite.cipherSuiteTag)
)
}

is ConversationEntity.ProtocolInfo.MLS -> {
tryAddMembersToMLSGroup(conversationId, protocol.groupId, userIdList, LastUsersAttempt.None)
tryAddMembersToMLSGroup(
conversationId,
protocol.groupId,
userIdList,
LastUsersAttempt.None,
cipherSuite = CipherSuite.fromTag(protocol.cipherSuite.cipherSuiteTag)
)
}
}
}
Expand All @@ -238,14 +262,22 @@ internal class ConversationGroupRepositoryImpl(
* Handle the error cases and retry for claimPackages offline and out of packages.
* Handle error case and retry for sendingCommit unreachable or missing legal hold consent.
*/
@Suppress("LongMethod")
private suspend fun tryAddMembersToMLSGroup(
conversationId: ConversationId,
groupId: String,
userIdList: List<UserId>,
lastUsersAttempt: LastUsersAttempt,
cipherSuite: CipherSuite,
remainingAttempts: Int = 2
): Either<CoreFailure, Unit> {
return when (val addingMemberResult = mlsConversationRepository.addMemberToMLSGroup(GroupID(groupId), userIdList)) {
return when (
val addingMemberResult = mlsConversationRepository.addMemberToMLSGroup(
GroupID(groupId),
userIdList,
cipherSuite
)
) {
is Either.Right -> handleMLSMembersNotAdded(conversationId, lastUsersAttempt)
is Either.Left -> {
addingMemberResult.value.handleMLSMembersFailed(
Expand All @@ -254,17 +286,20 @@ internal class ConversationGroupRepositoryImpl(
userIdList = userIdList,
lastUsersAttempt = lastUsersAttempt,
remainingAttempts = remainingAttempts,
cipherSuite = cipherSuite
)
}
}
}

@Suppress("LongMethod")
private suspend fun CoreFailure.handleMLSMembersFailed(
conversationId: ConversationId,
groupId: String,
userIdList: List<UserId>,
lastUsersAttempt: LastUsersAttempt,
remainingAttempts: Int,
cipherSuite: CipherSuite
): Either<CoreFailure, Unit> {
return when {
// claiming key packages offline or out of packages
Expand All @@ -278,7 +313,8 @@ internal class ConversationGroupRepositoryImpl(
failedUsers = lastUsersAttempt.failedUsers + failedUsers,
failType = FailedToAdd.Type.Federation,
),
remainingAttempts = remainingAttempts - 1
remainingAttempts = remainingAttempts - 1,
cipherSuite = cipherSuite
)
}

Expand All @@ -293,7 +329,8 @@ internal class ConversationGroupRepositoryImpl(
failedUsers = lastUsersAttempt.failedUsers + failedUsers,
failType = FailedToAdd.Type.Federation,
),
remainingAttempts = remainingAttempts - 1
remainingAttempts = remainingAttempts - 1,
cipherSuite = cipherSuite
)
}

Expand All @@ -309,7 +346,8 @@ internal class ConversationGroupRepositoryImpl(
failedUsers = lastUsersAttempt.failedUsers + failedUsers,
failType = FailedToAdd.Type.LegalHold,
),
remainingAttempts = remainingAttempts - 1
remainingAttempts = remainingAttempts - 1,
cipherSuite = cipherSuite
)
}
}
Expand Down Expand Up @@ -430,7 +468,7 @@ internal class ConversationGroupRepositoryImpl(
}
}
} else {
val failType = (lastUsersAttempt as? LastUsersAttempt.Failed)?.failType ?: FailedToAdd.Type.Unknown
val failType = apiResult.value.toFailedToAddType()
newGroupConversationSystemMessagesCreator.value.conversationFailedToAddMembers(
conversationId, userIdList + lastUsersAttempt.failedUsers, failType
).flatMap {
Expand Down Expand Up @@ -480,7 +518,11 @@ internal class ConversationGroupRepositoryImpl(

is ConversationEntity.ProtocolInfo.MLSCapable -> {
joinExistingMLSConversation(conversationId).flatMap {
mlsConversationRepository.addMemberToMLSGroup(GroupID(protocol.groupId), listOf(selfUserId))
mlsConversationRepository.addMemberToMLSGroup(
GroupID(protocol.groupId),
listOf(selfUserId),
CipherSuite.fromTag(protocol.cipherSuite.cipherSuiteTag)
)
}
}
}
Expand Down Expand Up @@ -600,6 +642,12 @@ internal class ConversationGroupRepositoryImpl(
Either.Right(ValidToInvalidUsers(userIdList, emptyList(), FailedToAdd.Type.Unknown))
}

private fun CoreFailure.toFailedToAddType() = when {
this is NetworkFailure.FederatedBackendFailure -> FailedToAdd.Type.Federation
this.isMissingLegalHoldConsentError -> FailedToAdd.Type.LegalHold
else -> FailedToAdd.Type.Unknown
}

/**
* Filter the initial [userIdList] into valid and invalid users where valid users are only team members.
*/
Expand Down

0 comments on commit 5cc7bd6

Please sign in to comment.