Skip to content

Commit

Permalink
fix: persist mls conversation when mls disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Dec 19, 2024
1 parent da1e7eb commit 7d6bcd6
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 @@ -430,21 +430,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 @@ -456,19 +459,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 @@ -491,8 +490,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 @@ -502,7 +505,7 @@ internal class ConversationDataSource internal constructor(
ConversationEntity.GroupState.PENDING_JOIN
}
}
}
})

private suspend fun hasEstablishedMLSGroup(groupID: GroupID): Either<CoreFailure, Boolean> =
mlsClientProvider.getMLSClient()
Expand All @@ -512,10 +515,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 7d6bcd6

Please sign in to comment.