-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve one-on-one conversation to either proteus or mls (#2012)
- Loading branch information
Showing
17 changed files
with
822 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
.../src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/mls/OneOnOneMigrator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.conversation.mls | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.StorageFailure | ||
import com.wire.kalium.logic.data.conversation.Conversation | ||
import com.wire.kalium.logic.data.conversation.ConversationGroupRepository | ||
import com.wire.kalium.logic.data.conversation.ConversationRepository | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.message.MessageRepository | ||
import com.wire.kalium.logic.data.user.OtherUser | ||
import com.wire.kalium.logic.data.user.UserRepository | ||
import com.wire.kalium.logic.data.user.type.isTeammate | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.logic.functional.foldToEitherWhileRight | ||
import com.wire.kalium.logic.functional.map | ||
|
||
interface OneOnOneMigrator { | ||
suspend fun migrateToProteus(user: OtherUser): Either<CoreFailure, ConversationId> | ||
suspend fun migrateToMLS(user: OtherUser): Either<CoreFailure, ConversationId> | ||
} | ||
|
||
internal class OneOnOneMigratorImpl( | ||
private val getResolvedMLSOneOnOne: MLSOneOnOneConversationResolver, | ||
private val conversationGroupRepository: ConversationGroupRepository, | ||
private val conversationRepository: ConversationRepository, | ||
private val messageRepository: MessageRepository, | ||
private val userRepository: UserRepository | ||
) : OneOnOneMigrator { | ||
|
||
override suspend fun migrateToProteus(user: OtherUser): Either<CoreFailure, ConversationId> = | ||
conversationRepository.getOneOnOneConversationsWithOtherUser(user.id, Conversation.Protocol.PROTEUS).flatMap { conversationIds -> | ||
if (conversationIds.isNotEmpty()) { | ||
val conversationId = conversationIds.first() | ||
Either.Right(conversationId) | ||
} else { | ||
Either.Left(StorageFailure.DataNotFound) | ||
} | ||
}.fold({ failure -> | ||
if (failure is StorageFailure.DataNotFound && user.userType.isTeammate()) { | ||
conversationGroupRepository.createGroupConversation(usersList = listOf(user.id)).map { it.id } | ||
} else { | ||
Either.Left(failure) | ||
} | ||
}, { | ||
Either.Right(it) | ||
}).flatMap { conversationId -> | ||
if (user.activeOneOnOneConversationId != conversationId) { | ||
userRepository.updateActiveOneOnOneConversation(user.id, conversationId) | ||
} | ||
Either.Right(conversationId) | ||
} | ||
|
||
override suspend fun migrateToMLS(user: OtherUser): Either<CoreFailure, ConversationId> { | ||
return getResolvedMLSOneOnOne(user.id) | ||
.flatMap { mlsConversation -> | ||
if (user.activeOneOnOneConversationId == mlsConversation) { | ||
return@flatMap Either.Right(mlsConversation) | ||
} | ||
migrateOneOnOneHistory(user, mlsConversation) | ||
.flatMap { | ||
userRepository.updateActiveOneOnOneConversation( | ||
conversationId = mlsConversation, | ||
userId = user.id | ||
).map { | ||
mlsConversation | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun migrateOneOnOneHistory(user: OtherUser, targetConversation: ConversationId): Either<CoreFailure, Unit> { | ||
return conversationRepository.getOneOnOneConversationsWithOtherUser( | ||
otherUserId = user.id, | ||
protocol = Conversation.Protocol.PROTEUS | ||
).flatMap { proteusOneOnOneConversations -> | ||
// We can theoretically have more than one proteus 1-1 conversation with | ||
// team members since there was no backend safeguards against this | ||
proteusOneOnOneConversations.foldToEitherWhileRight(Unit) { proteusOneOnOneConversation, _ -> | ||
messageRepository.moveMessagesToAnotherConversation( | ||
originalConversation = proteusOneOnOneConversation, | ||
targetConversation = targetConversation | ||
) | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
.../src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/mls/OneOnOneResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.conversation.mls | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.user.OtherUser | ||
import com.wire.kalium.logic.data.user.SupportedProtocol | ||
import com.wire.kalium.logic.data.user.UserRepository | ||
import com.wire.kalium.logic.feature.protocol.OneOnOneProtocolSelector | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.flatMapLeft | ||
import com.wire.kalium.logic.functional.foldToEitherWhileRight | ||
import com.wire.kalium.logic.functional.map | ||
import com.wire.kalium.logic.kaliumLogger | ||
|
||
interface OneOnOneResolver { | ||
suspend fun resolveAllOneOnOneConversations(): Either<CoreFailure, Unit> | ||
suspend fun resolveOneOnOneConversationWithUser(user: OtherUser): Either<CoreFailure, ConversationId> | ||
} | ||
|
||
internal class OneOnOneResolverImpl( | ||
private val userRepository: UserRepository, | ||
private val oneOnOneProtocolSelector: OneOnOneProtocolSelector, | ||
private val oneOnOneMigrator: OneOnOneMigrator, | ||
) : OneOnOneResolver { | ||
|
||
override suspend fun resolveAllOneOnOneConversations(): Either<CoreFailure, Unit> { | ||
val usersWithOneOnOne = userRepository.getUsersWithOneOnOneConversation() | ||
kaliumLogger.i("Resolving one-on-one protocol for ${usersWithOneOnOne.size} user(s)") | ||
return usersWithOneOnOne.foldToEitherWhileRight(Unit) { item, _ -> | ||
resolveOneOnOneConversationWithUser(item).map { } | ||
} | ||
} | ||
|
||
override suspend fun resolveOneOnOneConversationWithUser(user: OtherUser): Either<CoreFailure, ConversationId> = | ||
oneOnOneProtocolSelector.getProtocolForUser(user.id).flatMap { supportedProtocol -> | ||
when (supportedProtocol) { | ||
SupportedProtocol.PROTEUS -> oneOnOneMigrator.migrateToProteus(user) | ||
SupportedProtocol.MLS -> oneOnOneMigrator.migrateToMLS(user) | ||
} | ||
}.flatMapLeft { | ||
if (it is CoreFailure.NoCommonProtocolFound) { | ||
// TODO mark conversation as read only | ||
Either.Right(Unit) | ||
} | ||
Either.Left(it) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.