Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: persist mls conversation when mls disabled [WPB-15149] 🍒 #3209

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading