From fea30f38f63eedb80216315827f50d7b59aca3e5 Mon Sep 17 00:00:00 2001 From: Jacob Persson <7156+typfel@users.noreply.github.com> Date: Thu, 31 Aug 2023 16:51:14 +0200 Subject: [PATCH] chore: default selfUser when creator field is missing (#2022) --- .../data/conversation/ConversationGroupRepository.kt | 2 +- .../kalium/logic/data/conversation/ConversationMapper.kt | 3 ++- .../logic/data/conversation/ConversationRepository.kt | 2 +- .../logic/data/conversation/MLSConversationRepository.kt | 3 ++- .../NewGroupConversationSystemMessagesCreator.kt | 2 +- .../kotlin/com/wire/kalium/logic/di/MapperProvider.kt | 3 ++- .../com/wire/kalium/logic/feature/UserSessionScope.kt | 3 ++- .../logic/feature/backup/RestoreWebBackupUseCase.kt | 8 ++++---- .../conversation/PersistMigratedConversationUseCase.kt | 4 +++- .../wire/kalium/logic/feature/migration/MigrationScope.kt | 4 +++- .../logic/data/conversation/ConversationMapperTest.kt | 2 ++ .../data/conversation/MLSConversationRepositoryTest.kt | 1 + .../logic/feature/backup/RestoreWebBackupUseCaseTest.kt | 2 +- .../authenticated/conversation/ConversationResponse.kt | 2 +- 14 files changed, 26 insertions(+), 15 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationGroupRepository.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationGroupRepository.kt index 7c8dec82e53..afd532fd898 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationGroupRepository.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/ConversationGroupRepository.kt @@ -98,7 +98,7 @@ internal class ConversationGroupRepositoryImpl( private val newGroupConversationSystemMessagesCreator: Lazy, private val selfUserId: UserId, private val teamIdProvider: SelfTeamIdProvider, - private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(), + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId), private val eventMapper: EventMapper = MapperProvider.eventMapper(), private val protocolInfoMapper: ProtocolInfoMapper = MapperProvider.protocolInfoMapper(), ) : ConversationGroupRepository { 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 2ab311c93b5..5e3f23fc35b 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 @@ -83,6 +83,7 @@ interface ConversationMapper { @Suppress("TooManyFunctions", "LongParameterList") internal class ConversationMapperImpl( + private val selfUserId: UserId, private val idMapper: IdMapper, private val conversationStatusMapper: ConversationStatusMapper, private val protocolInfoMapper: ProtocolInfoMapper, @@ -106,7 +107,7 @@ internal class ConversationMapperImpl( mutedStatus = conversationStatusMapper.fromMutedStatusApiToDaoModel(apiModel.members.self.otrMutedStatus), mutedTime = apiModel.members.self.otrMutedRef?.let { Instant.parse(it) }?.toEpochMilliseconds() ?: 0, removedBy = null, - creatorId = apiModel.creator, + creatorId = apiModel.creator ?: selfUserId.value, // NOTE mls 1-1 does not have the creator field set. lastReadDate = Instant.UNIX_FIRST_DATE, lastNotificationDate = null, lastModifiedDate = apiModel.lastEventTime.toInstant(), 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 45f06f6e6f4..52ecca98d72 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 @@ -253,7 +253,7 @@ internal class ConversationDataSource internal constructor( private val clientApi: ClientApi, private val conversationMetaDataDAO: ConversationMetaDataDAO, private val idMapper: IdMapper = MapperProvider.idMapper(), - private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(), + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId), private val memberMapper: MemberMapper = MapperProvider.memberMapper(), private val conversationStatusMapper: ConversationStatusMapper = MapperProvider.conversationStatusMapper(), private val conversationRoleMapper: ConversationRoleMapper = MapperProvider.conversationRoleMapper(), diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepository.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepository.kt index 43dcc03f326..7870eda7812 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepository.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepository.kt @@ -132,6 +132,7 @@ private fun CoreFailure.getStrategy( @Suppress("TooManyFunctions", "LongParameterList") internal class MLSConversationDataSource( + private val selfUserId: UserId, private val keyPackageRepository: KeyPackageRepository, private val mlsClientProvider: MLSClientProvider, private val mlsMessageApi: MLSMessageApi, @@ -143,7 +144,7 @@ internal class MLSConversationDataSource( private val epochsFlow: MutableSharedFlow, private val proposalTimersFlow: MutableSharedFlow, private val idMapper: IdMapper = MapperProvider.idMapper(), - private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(), + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId), private val mlsPublicKeysMapper: MLSPublicKeysMapper = MapperProvider.mlsPublicKeyMapper(), private val mlsCommitBundleMapper: MLSCommitBundleMapper = MapperProvider.mlsCommitBundleMapper() ) : MLSConversationRepository { diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/NewGroupConversationSystemMessagesCreator.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/NewGroupConversationSystemMessagesCreator.kt index dc19e42b954..d20972517d0 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/NewGroupConversationSystemMessagesCreator.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/conversation/NewGroupConversationSystemMessagesCreator.kt @@ -116,7 +116,7 @@ internal class NewGroupConversationSystemMessagesCreatorImpl( persistReadReceiptSystemMessage( conversationId = conversation.id.toModel(), - creatorId = qualifiedIdMapper.fromStringToQualifiedID(conversation.creator), + creatorId = conversation.creator?.let { qualifiedIdMapper.fromStringToQualifiedID(it) } ?: selfUserId, receiptMode = conversation.receiptMode == ReceiptMode.ENABLED ) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/di/MapperProvider.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/di/MapperProvider.kt index 07edb83f0c1..a04708ff6eb 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/di/MapperProvider.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/di/MapperProvider.kt @@ -118,8 +118,9 @@ internal object MapperProvider { ) fun memberMapper(): MemberMapper = MemberMapperImpl(idMapper(), conversationRoleMapper()) - fun conversationMapper(): ConversationMapper = + fun conversationMapper(selfUserId: UserId): ConversationMapper = ConversationMapperImpl( + selfUserId, idMapper(), ConversationStatusMapperImpl(idMapper()), ProtocolInfoMapperImpl(), diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt index 3a2066303cf..154ebfee7d2 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt @@ -528,6 +528,7 @@ class UserSessionScope internal constructor( private val mlsConversationRepository: MLSConversationRepository get() = MLSConversationDataSource( + userId, keyPackageRepository, mlsClientProvider, authenticatedNetworkContainer.mlsMessageApi, @@ -1373,7 +1374,7 @@ class UserSessionScope internal constructor( this ) - val migration get() = MigrationScope(userStorage.database) + val migration get() = MigrationScope(userId, userStorage.database) val debug: DebugScope get() = DebugScope( messageRepository, diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCase.kt index c89e3da87f5..99af987f549 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCase.kt @@ -63,12 +63,12 @@ interface RestoreWebBackupUseCase { @Suppress("TooManyFunctions", "LongParameterList", "NestedBlockDepth") internal class RestoreWebBackupUseCaseImpl( private val kaliumFileSystem: KaliumFileSystem, - private val userId: UserId, + private val selfUserId: UserId, private val persistMigratedMessages: PersistMigratedMessagesUseCase, private val restartSlowSyncProcessForRecovery: RestartSlowSyncProcessForRecoveryUseCase, private val migrationDAO: MigrationDAO, private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl, - private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper() + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) ) : RestoreWebBackupUseCase { override suspend operator fun invoke(backupRootPath: Path, metadata: BackupMetadata): RestoreBackupResult = @@ -100,7 +100,7 @@ internal class RestoreWebBackupUseCaseImpl( while (iterator.hasNext()) { try { val webConversation = iterator.next() - val migratedConversation = webConversation.toConversation(userId) + val migratedConversation = webConversation.toConversation(selfUserId) if (migratedConversation != null) { migratedConversations.add(migratedConversation) } @@ -128,7 +128,7 @@ internal class RestoreWebBackupUseCaseImpl( while (iterator.hasNext()) { try { val webContent = iterator.next() - val migratedMessage = webContent.toMigratedMessage(userId.domain) + val migratedMessage = webContent.toMigratedMessage(selfUserId.domain) if (migratedMessage != null) { migratedMessagesBatch.add(migratedMessage) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/PersistMigratedConversationUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/PersistMigratedConversationUseCase.kt index 1e4aa9fed8c..fe4dc48c5e4 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/PersistMigratedConversationUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/conversation/PersistMigratedConversationUseCase.kt @@ -22,6 +22,7 @@ import com.wire.kalium.logger.KaliumLogger.Companion.ApplicationFlow.CONVERSATIO import com.wire.kalium.logic.StorageFailure import com.wire.kalium.logic.data.conversation.Conversation import com.wire.kalium.logic.data.conversation.ConversationMapper +import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.di.MapperProvider import com.wire.kalium.logic.kaliumLogger import com.wire.kalium.logic.wrapStorageRequest @@ -46,8 +47,9 @@ fun interface PersistMigratedConversationUseCase { } internal class PersistMigratedConversationUseCaseImpl( + private val selfUserId: UserId, private val migrationDAO: MigrationDAO, - private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper() + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) ) : PersistMigratedConversationUseCase { val logger by lazy { kaliumLogger.withFeatureId(CONVERSATIONS) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/migration/MigrationScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/migration/MigrationScope.kt index 10ff919d385..de4e91eec69 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/migration/MigrationScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/migration/MigrationScope.kt @@ -18,15 +18,17 @@ package com.wire.kalium.logic.feature.migration +import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.conversation.PersistMigratedConversationUseCase import com.wire.kalium.logic.feature.conversation.PersistMigratedConversationUseCaseImpl import com.wire.kalium.persistence.db.UserDatabaseBuilder class MigrationScope( + private val selfUserId: UserId, private val userDatabase: UserDatabaseBuilder ) { val persistMigratedConversation: PersistMigratedConversationUseCase - get() = PersistMigratedConversationUseCaseImpl(userDatabase.migrationDAO) + get() = PersistMigratedConversationUseCaseImpl(selfUserId, userDatabase.migrationDAO) } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapperTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapperTest.kt index 93e6d116758..72d0c5a8cba 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapperTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/ConversationMapperTest.kt @@ -23,6 +23,7 @@ import com.wire.kalium.logic.data.id.IdMapper import com.wire.kalium.logic.data.id.TeamId import com.wire.kalium.logic.data.user.AvailabilityStatusMapper import com.wire.kalium.logic.data.user.type.DomainUserTypeMapper +import com.wire.kalium.logic.framework.TestUser import com.wire.kalium.network.api.base.authenticated.conversation.ConvProtocol import com.wire.kalium.network.api.base.authenticated.conversation.ConversationMemberDTO import com.wire.kalium.network.api.base.authenticated.conversation.ConversationMembersResponse @@ -74,6 +75,7 @@ class ConversationMapperTest { @BeforeTest fun setup() { conversationMapper = ConversationMapperImpl( + TestUser.SELF.id, idMapper, conversationStatusMapper, protocolInfoMapper, diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt index 1e2d96b92aa..4ad6c91e78c 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt @@ -1197,6 +1197,7 @@ class MLSConversationRepositoryTest { } fun arrange() = this to MLSConversationDataSource( + TestUser.SELF.id, keyPackageRepository, mlsClientProvider, mlsMessageApi, diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCaseTest.kt index e658b20cc6c..026f03aec9f 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/backup/RestoreWebBackupUseCaseTest.kt @@ -192,7 +192,7 @@ class RestoreWebBackupUseCaseTest { fun arrange() = this to RestoreWebBackupUseCaseImpl( kaliumFileSystem = fakeFileSystem, - userId = selfUserId, + selfUserId = selfUserId, migrationDAO = migrationDAO, persistMigratedMessages = persistMigratedMessagesUseCase, restartSlowSyncProcessForRecovery = restartSlowSyncProcessForRecoveryUseCase diff --git a/network/src/commonMain/kotlin/com/wire/kalium/network/api/base/authenticated/conversation/ConversationResponse.kt b/network/src/commonMain/kotlin/com/wire/kalium/network/api/base/authenticated/conversation/ConversationResponse.kt index 6d79062851b..6b759524718 100644 --- a/network/src/commonMain/kotlin/com/wire/kalium/network/api/base/authenticated/conversation/ConversationResponse.kt +++ b/network/src/commonMain/kotlin/com/wire/kalium/network/api/base/authenticated/conversation/ConversationResponse.kt @@ -37,7 +37,7 @@ import kotlinx.serialization.encoding.Encoder @Serializable data class ConversationResponse( @SerialName("creator") - val creator: String, + val creator: String?, @SerialName("members") val members: ConversationMembersResponse,