diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapper.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapper.kt index 6382559ee23..8ec1b717167 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapper.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapper.kt @@ -477,11 +477,11 @@ internal class ConversationMapperImpl( private fun ConversationResponse.getProtocolInfo(mlsGroupState: GroupState?): ProtocolInfo { return when (protocol) { ConvProtocol.MLS -> ProtocolInfo.MLS( - groupId ?: "", - mlsGroupState ?: GroupState.PENDING_JOIN, - epoch ?: 0UL, + groupId = groupId ?: "", + groupState = mlsGroupState ?: GroupState.PENDING_JOIN, + epoch = epoch ?: 0UL, keyingMaterialLastUpdate = DateTimeUtil.currentInstant(), - ConversationEntity.CipherSuite.fromTag(mlsCipherSuiteTag) + cipherSuite = ConversationEntity.CipherSuite.fromTag(mlsCipherSuiteTag) ) ConvProtocol.MIXED -> ProtocolInfo.Mixed( diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationRepository.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationRepository.kt index 2820fa3d8df..6277230a283 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationRepository.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationRepository.kt @@ -431,21 +431,24 @@ internal class ConversationDataSource internal constructor( selfUserTeamId: String?, originatedFromEvent: Boolean ): Either = wrapStorageRequest { - val isNewConversation = conversationDAO.getConversationById(conversation.id.toDao()) == null + val existingConversation = conversationDAO.getConversationById(conversation.id.toDao()) + val isNewConversation = existingConversation?.let { conversationEntity -> + (conversationEntity.protocolInfo as? ConversationEntity.ProtocolInfo.MLSCapable)?.groupState?.let { + it != ConversationEntity.GroupState.ESTABLISHED + } ?: false + } ?: true if (isNewConversation) { val mlsGroupState = conversation.groupId?.let { mlsGroupState(idMapper.fromGroupIDEntity(it), originatedFromEvent) } - if (shouldPersistMLSConversation(mlsGroupState)) { - conversationDAO.insertConversation( - conversationMapper.fromApiModelToDaoModel( - conversation, - mlsGroupState = mlsGroupState?.getOrNull(), - selfTeamIdProvider().getOrNull(), - ) - ) - memberDAO.insertMembersWithQualifiedId( - memberMapper.fromApiModelToDaoModel(conversation.members), idMapper.fromApiToDao(conversation.id) + conversationDAO.insertConversation( + conversationMapper.fromApiModelToDaoModel( + conversation, + mlsGroupState = mlsGroupState, + selfTeamIdProvider().getOrNull(), ) - } + ) + memberDAO.insertMembersWithQualifiedId( + memberMapper.fromApiModelToDaoModel(conversation.members), idMapper.fromApiToDao(conversation.id) + ) } isNewConversation } @@ -457,19 +460,15 @@ internal class ConversationDataSource internal constructor( invalidateMembers: Boolean ) = wrapStorageRequest { val conversationEntities = conversations - .mapNotNull { conversationResponse -> + .map { conversationResponse -> val mlsGroupState = conversationResponse.groupId?.let { mlsGroupState(idMapper.fromGroupIDEntity(it), originatedFromEvent) } - if (shouldPersistMLSConversation(mlsGroupState)) { - conversationMapper.fromApiModelToDaoModel( - conversationResponse, - mlsGroupState = mlsGroupState?.getOrNull(), - selfTeamIdProvider().getOrNull(), - ) - } else { - null - } + conversationMapper.fromApiModelToDaoModel( + conversationResponse, + mlsGroupState = mlsGroupState, + selfTeamIdProvider().getOrNull(), + ) } conversationDAO.insertConversations(conversationEntities) conversations.forEach { conversationsResponse -> @@ -492,8 +491,12 @@ internal class ConversationDataSource internal constructor( private suspend fun mlsGroupState( groupId: GroupID, originatedFromEvent: Boolean = false - ): Either = hasEstablishedMLSGroup(groupId) - .map { exists -> + ): ConversationEntity.GroupState = hasEstablishedMLSGroup(groupId) + .fold({ failure -> + kaliumLogger.withFeatureId(CONVERSATIONS) + .w("Error checking MLS group state, setting to ${ConversationEntity.GroupState.PENDING_JOIN}") + ConversationEntity.GroupState.PENDING_JOIN + }, { exists -> if (exists) { ConversationEntity.GroupState.ESTABLISHED } else { @@ -503,7 +506,7 @@ internal class ConversationDataSource internal constructor( ConversationEntity.GroupState.PENDING_JOIN } } - } + }) private suspend fun hasEstablishedMLSGroup(groupID: GroupID): Either = mlsClientProvider.getMLSClient() @@ -513,10 +516,6 @@ internal class ConversationDataSource internal constructor( } } - // if group state is not null and is left, then we don't want to persist the MLS conversation - private fun shouldPersistMLSConversation(groupState: Either?): Boolean = - groupState?.fold({ true }, { false }) != true - @DelicateKaliumApi("This function does not get values from cache") override suspend fun getProteusSelfConversationId(): Either = wrapStorageRequest { conversationDAO.getSelfConversationId(ConversationEntity.Protocol.PROTEUS) } diff --git a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/conversation/ConversationEntity.kt b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/conversation/ConversationEntity.kt index c1ef9973477..6f68b683f53 100644 --- a/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/conversation/ConversationEntity.kt +++ b/persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/conversation/ConversationEntity.kt @@ -75,7 +75,7 @@ data class ConversationEntity( companion object { fun fromTag(tag: Int?): CipherSuite = - if (tag != null) values().first { type -> type.cipherSuiteTag == tag } else UNKNOWN + if (tag != null) entries.first { type -> type.cipherSuiteTag == tag } else UNKNOWN } }