Skip to content

Commit

Permalink
fix: persist mls conversation when mls disabled (#3205) (#3209)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Żerko <[email protected]>
  • Loading branch information
github-actions[bot] and Garzas authored Jan 9, 2025
1 parent e3639ec commit f506f90
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,24 @@ internal class ConversationDataSource internal constructor(
selfUserTeamId: String?,
originatedFromEvent: Boolean
): Either<CoreFailure, Boolean> = 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
}
Expand All @@ -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 ->
Expand All @@ -492,8 +491,12 @@ internal class ConversationDataSource internal constructor(
private suspend fun mlsGroupState(
groupId: GroupID,
originatedFromEvent: Boolean = false
): Either<CoreFailure, ConversationEntity.GroupState> = 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 {
Expand All @@ -503,7 +506,7 @@ internal class ConversationDataSource internal constructor(
ConversationEntity.GroupState.PENDING_JOIN
}
}
}
})

private suspend fun hasEstablishedMLSGroup(groupID: GroupID): Either<CoreFailure, Boolean> =
mlsClientProvider.getMLSClient()
Expand All @@ -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<CoreFailure, ConversationEntity.GroupState>?): Boolean =
groupState?.fold({ true }, { false }) != true

@DelicateKaliumApi("This function does not get values from cache")
override suspend fun getProteusSelfConversationId(): Either<StorageFailure, ConversationId> =
wrapStorageRequest { conversationDAO.getSelfConversationId(ConversationEntity.Protocol.PROTEUS) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit f506f90

Please sign in to comment.