@@ -432,16 +432,19 @@ internal class ConversationDataSource internal constructor(
432
432
): Either <CoreFailure , Boolean > = wrapStorageRequest {
433
433
val isNewConversation = conversationDAO.getConversationById(conversation.id.toDao()) == null
434
434
if (isNewConversation) {
435
- conversationDAO.insertConversation(
436
- conversationMapper.fromApiModelToDaoModel(
437
- conversation,
438
- mlsGroupState = conversation.groupId?.let { mlsGroupState(idMapper.fromGroupIDEntity(it), originatedFromEvent) },
439
- selfTeamIdProvider().getOrNull(),
435
+ val mlsGroupState = conversation.groupId?.let { mlsGroupState(idMapper.fromGroupIDEntity(it), originatedFromEvent) }
436
+ if (shouldPersistMLSConversation(mlsGroupState)) {
437
+ conversationDAO.insertConversation(
438
+ conversationMapper.fromApiModelToDaoModel(
439
+ conversation,
440
+ mlsGroupState = mlsGroupState?.getOrNull(),
441
+ selfTeamIdProvider().getOrNull(),
442
+ )
440
443
)
441
- )
442
- memberDAO.insertMembersWithQualifiedId(
443
- memberMapper.fromApiModelToDaoModel(conversation.members), idMapper.fromApiToDao(conversation.id )
444
- )
444
+ memberDAO.insertMembersWithQualifiedId(
445
+ memberMapper.fromApiModelToDaoModel(conversation.members), idMapper.fromApiToDao(conversation.id)
446
+ )
447
+ }
445
448
}
446
449
isNewConversation
447
450
}
@@ -453,17 +456,19 @@ internal class ConversationDataSource internal constructor(
453
456
invalidateMembers : Boolean
454
457
) = wrapStorageRequest {
455
458
val conversationEntities = conversations
456
- .map { conversationResponse ->
457
- conversationMapper.fromApiModelToDaoModel(
458
- conversationResponse,
459
- mlsGroupState = conversationResponse.groupId?.let {
460
- mlsGroupState(
461
- idMapper.fromGroupIDEntity(it),
462
- originatedFromEvent
463
- )
464
- },
465
- selfTeamIdProvider().getOrNull(),
466
- )
459
+ .mapNotNull { conversationResponse ->
460
+ val mlsGroupState = conversationResponse.groupId?.let {
461
+ mlsGroupState(idMapper.fromGroupIDEntity(it), originatedFromEvent)
462
+ }
463
+ if (shouldPersistMLSConversation(mlsGroupState)) {
464
+ conversationMapper.fromApiModelToDaoModel(
465
+ conversationResponse,
466
+ mlsGroupState = mlsGroupState?.getOrNull(),
467
+ selfTeamIdProvider().getOrNull(),
468
+ )
469
+ } else {
470
+ null
471
+ }
467
472
}
468
473
conversationDAO.insertConversations(conversationEntities)
469
474
conversations.forEach { conversationsResponse ->
@@ -483,10 +488,11 @@ internal class ConversationDataSource internal constructor(
483
488
}
484
489
}
485
490
486
- private suspend fun mlsGroupState (groupId : GroupID , originatedFromEvent : Boolean = false): ConversationEntity .GroupState =
487
- hasEstablishedMLSGroup(groupId).fold({
488
- throw IllegalStateException (it.toString()) // TODO find a more fitting exception?
489
- }, { exists ->
491
+ private suspend fun mlsGroupState (
492
+ groupId : GroupID ,
493
+ originatedFromEvent : Boolean = false
494
+ ): Either <CoreFailure , ConversationEntity .GroupState > = hasEstablishedMLSGroup(groupId)
495
+ .map { exists ->
490
496
if (exists) {
491
497
ConversationEntity .GroupState .ESTABLISHED
492
498
} else {
@@ -496,7 +502,7 @@ internal class ConversationDataSource internal constructor(
496
502
ConversationEntity .GroupState .PENDING_JOIN
497
503
}
498
504
}
499
- })
505
+ }
500
506
501
507
private suspend fun hasEstablishedMLSGroup (groupID : GroupID ): Either <CoreFailure , Boolean > =
502
508
mlsClientProvider.getMLSClient()
@@ -506,6 +512,10 @@ internal class ConversationDataSource internal constructor(
506
512
}
507
513
}
508
514
515
+ // if group state is not null and is left, then we don't want to persist the MLS conversation
516
+ private fun shouldPersistMLSConversation (groupState : Either <CoreFailure , ConversationEntity .GroupState >? ): Boolean =
517
+ groupState?.fold({ true }, { false }) != true
518
+
509
519
@DelicateKaliumApi(" This function does not get values from cache" )
510
520
override suspend fun getProteusSelfConversationId (): Either <StorageFailure , ConversationId > =
511
521
wrapStorageRequest { conversationDAO.getSelfConversationId(ConversationEntity .Protocol .PROTEUS ) }
0 commit comments