From 68262eeb0e9851131f61052ffe7e9a27b093d607 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:35:08 +0200 Subject: [PATCH 01/11] feat: add mappers from api to model and from model to dao for access update event --- .../data/conversation/ConversationMapper.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) 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 732e116824..0f86a17808 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 @@ -85,6 +85,11 @@ interface ConversationMapper { fun legalHoldStatusFromEntity(legalHoldStatus: ConversationEntity.LegalHoldStatus): Conversation.LegalHoldStatus fun fromConversationEntityType(type: ConversationEntity.Type): Conversation.Type + + fun fromModelToDAOAccess(accessList: Set): List + fun fromModelToDAOAccessRole(accessRoleList: Set): List + fun fromApiModel(accessList: Set): Set + fun fromApiModel(accessRoleList: Set): Set } @Suppress("TooManyFunctions", "LongParameterList") @@ -461,6 +466,18 @@ internal class ConversationMapperImpl( override fun fromConversationEntityType(type: ConversationEntity.Type): Conversation.Type { return type.fromDaoModelToType() } + + override fun fromModelToDAOAccess(accessList: Set): List = + accessList.map { it.toDAO() } + + override fun fromModelToDAOAccessRole(accessRoleList: Set): List = + accessRoleList.map { it.toDAO() } + + override fun fromApiModel(accessList: Set): Set = + accessList.map { it.toModel() }.toSet() + + override fun fromApiModel(accessRoleList: Set): Set = + accessRoleList.map { it.toModel() }.toSet() } internal fun ConversationResponse.toConversationType(selfUserTeamId: TeamId?): ConversationEntity.Type { @@ -553,6 +570,22 @@ private fun Conversation.Access.toDAO(): ConversationEntity.Access = when (this) Conversation.Access.CODE -> ConversationEntity.Access.CODE } +private fun ConversationAccessDTO.toModel(): Conversation.Access = when (this) { + ConversationAccessDTO.PRIVATE -> Conversation.Access.PRIVATE + ConversationAccessDTO.CODE -> Conversation.Access.CODE + ConversationAccessDTO.INVITE -> Conversation.Access.INVITE + ConversationAccessDTO.SELF_INVITE -> Conversation.Access.SELF_INVITE + ConversationAccessDTO.LINK -> Conversation.Access.LINK +} + +private fun ConversationAccessRoleDTO.toModel(): Conversation.AccessRole = when (this) { + ConversationAccessRoleDTO.TEAM_MEMBER -> Conversation.AccessRole.TEAM_MEMBER + ConversationAccessRoleDTO.NON_TEAM_MEMBER -> Conversation.AccessRole.NON_TEAM_MEMBER + ConversationAccessRoleDTO.GUEST -> Conversation.AccessRole.GUEST + ConversationAccessRoleDTO.SERVICE -> Conversation.AccessRole.SERVICE + ConversationAccessRoleDTO.EXTERNAL -> Conversation.AccessRole.EXTERNAL +} + internal fun Conversation.Protocol.toApi(): ConvProtocol = when (this) { Conversation.Protocol.PROTEUS -> ConvProtocol.PROTEUS Conversation.Protocol.MIXED -> ConvProtocol.MIXED From 45455d62e8f6bd1fb0cab834950cdf5341040858 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:35:35 +0200 Subject: [PATCH 02/11] feat: add correct receiving values for access update (access and accessRole) --- .../com/wire/kalium/logic/data/event/Event.kt | 5 ++++- .../wire/kalium/logic/data/event/EventMapper.kt | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/Event.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/Event.kt index ea9fdebba3..ab0f10ef26 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/Event.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/Event.kt @@ -23,6 +23,8 @@ import com.wire.kalium.logger.obfuscateDomain import com.wire.kalium.logger.obfuscateId import com.wire.kalium.logic.data.client.Client import com.wire.kalium.logic.data.conversation.ClientId +import com.wire.kalium.logic.data.conversation.Conversation.Access +import com.wire.kalium.logic.data.conversation.Conversation.AccessRole import com.wire.kalium.logic.data.conversation.Conversation.Member import com.wire.kalium.logic.data.conversation.Conversation.Protocol import com.wire.kalium.logic.data.conversation.Conversation.ReceiptMode @@ -124,7 +126,8 @@ sealed class Event(open val id: String) { data class AccessUpdate( override val id: String, override val conversationId: ConversationId, - val data: ConversationResponse, + val access: Set, + val accessRole: Set, val qualifiedFrom: UserId, ) : Conversation(id, conversationId) { diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt index d907751c97..5e0b2eca92 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt @@ -23,6 +23,7 @@ import com.wire.kalium.logic.data.client.ClientMapper import com.wire.kalium.logic.data.connection.ConnectionMapper import com.wire.kalium.logic.data.conversation.ClientId import com.wire.kalium.logic.data.conversation.Conversation +import com.wire.kalium.logic.data.conversation.ConversationMapper import com.wire.kalium.logic.data.conversation.ConversationRoleMapper import com.wire.kalium.logic.data.conversation.MemberMapper import com.wire.kalium.logic.data.conversation.MutedConversationStatus @@ -64,7 +65,8 @@ class EventMapper( private val selfUserId: UserId, private val receiptModeMapper: ReceiptModeMapper = MapperProvider.receiptModeMapper(), private val clientMapper: ClientMapper = MapperProvider.clientMapper(), - private val qualifiedIdMapper: QualifiedIdMapper = MapperProvider.qualifiedIdMapper(selfUserId) + private val qualifiedIdMapper: QualifiedIdMapper = MapperProvider.qualifiedIdMapper(selfUserId), + private val conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) ) { fun fromDTO(eventResponse: EventResponse, isLive: Boolean): List { // TODO(edge-case): Multiple payloads in the same event have the same ID, is this an issue when marking lastProcessedEventId? @@ -94,7 +96,7 @@ class EventMapper( is EventContentDTO.User.LegalHoldDisabledDTO -> legalHoldDisabled(id, eventContentDTO) is EventContentDTO.FeatureConfig.FeatureConfigUpdatedDTO -> featureConfig(id, eventContentDTO) is EventContentDTO.Unknown -> unknown(id, eventContentDTO) - is EventContentDTO.Conversation.AccessUpdate -> unknown(id, eventContentDTO) + is EventContentDTO.Conversation.AccessUpdate -> conversationAccessUpdate(id, eventContentDTO) is EventContentDTO.Conversation.DeletedConversationDTO -> conversationDeleted(id, eventContentDTO) is EventContentDTO.Conversation.ConversationRenameDTO -> conversationRenamed(id, eventContentDTO) is EventContentDTO.Team.MemberLeave -> teamMemberLeft(id, eventContentDTO) @@ -194,6 +196,17 @@ class EventMapper( dateTime = eventContentDTO.time ) + private fun conversationAccessUpdate( + id: String, + eventContentDTO: EventContentDTO.Conversation.AccessUpdate + ): Event = Event.Conversation.AccessUpdate( + id = id, + conversationId = eventContentDTO.qualifiedConversation.toModel(), + access = conversationMapper.fromApiModel(eventContentDTO.data.access), + accessRole = conversationMapper.fromApiModel(eventContentDTO.data.accessRole), + qualifiedFrom = eventContentDTO.qualifiedFrom.toModel() + ) + private fun conversationReceiptModeUpdate( id: String, eventContentDTO: EventContentDTO.Conversation.ReceiptModeUpdate, From b4e2aae9ab34e39c45592af3670730097624c105 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:35:59 +0200 Subject: [PATCH 03/11] feat: create handler for access update event received and its usage --- .../kalium/logic/feature/UserSessionScope.kt | 10 ++++- .../receiver/ConversationEventReceiver.kt | 6 ++- .../ConversationAccessUpdateHandler.kt | 45 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt 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 0f0655d59b..ddf85ed91e 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 @@ -381,6 +381,7 @@ import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiver import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiverImpl import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandler import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandlerImpl +import com.wire.kalium.logic.sync.receiver.conversation.ConversationAccessUpdateHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandlerImpl import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler @@ -1466,6 +1467,12 @@ class UserSessionScope internal constructor( callRepository = callRepository ) + private val conversationAccessUpdateHandler: ConversationAccessUpdateHandler + get() = ConversationAccessUpdateHandler( + conversationDAO = userStorage.database.conversationDAO, + selfUserId = userId + ) + private val conversationEventReceiver: ConversationEventReceiver by lazy { ConversationEventReceiverImpl( newMessageHandler, @@ -1481,7 +1488,8 @@ class UserSessionScope internal constructor( conversationCodeUpdateHandler, conversationCodeDeletedHandler, typingIndicatorHandler, - protocolUpdateEventHandler + protocolUpdateEventHandler, + conversationAccessUpdateHandler ) } override val coroutineContext: CoroutineContext = SupervisorJob() diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt index 6381ff6c8c..22d6d13a99 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt @@ -22,6 +22,7 @@ import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.data.event.Event import com.wire.kalium.logic.data.event.EventDeliveryInfo import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.sync.receiver.conversation.ConversationAccessUpdateHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler import com.wire.kalium.logic.sync.receiver.conversation.MLSWelcomeEventHandler @@ -56,7 +57,8 @@ internal class ConversationEventReceiverImpl( private val codeUpdatedHandler: CodeUpdatedHandler, private val codeDeletedHandler: CodeDeletedHandler, private val typingIndicatorHandler: TypingIndicatorHandler, - private val protocolUpdateEventHandler: ProtocolUpdateEventHandler + private val protocolUpdateEventHandler: ProtocolUpdateEventHandler, + private val conversationAccessUpdateHandler: ConversationAccessUpdateHandler ) : ConversationEventReceiver { override suspend fun onEvent(event: Event.Conversation, deliveryInfo: EventDeliveryInfo): Either { // TODO: Make sure errors are accounted for by each handler. @@ -109,7 +111,7 @@ internal class ConversationEventReceiverImpl( } is Event.Conversation.AccessUpdate -> { - /* no-op */ + conversationAccessUpdateHandler.handle(event) Either.Right(Unit) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt new file mode 100644 index 0000000000..85d5e79866 --- /dev/null +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt @@ -0,0 +1,45 @@ +/* + * Wire + * Copyright (C) 2024 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ +package com.wire.kalium.logic.sync.receiver.conversation + +import com.wire.kalium.logic.data.conversation.ConversationMapper +import com.wire.kalium.logic.data.event.Event +import com.wire.kalium.logic.data.id.toDao +import com.wire.kalium.logic.data.user.UserId +import com.wire.kalium.logic.di.MapperProvider +import com.wire.kalium.persistence.dao.conversation.ConversationDAO + +interface ConversationAccessUpdateHandler { + suspend fun handle(event: Event.Conversation.AccessUpdate) +} + +@Suppress("FunctionNaming") +fun ConversationAccessUpdateHandler( + selfUserId: UserId, + conversationDAO: ConversationDAO, + conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) +) = object : ConversationAccessUpdateHandler { + + override suspend fun handle(event: Event.Conversation.AccessUpdate) { + conversationDAO.updateAccess( + conversationID = event.conversationId.toDao(), + accessList = conversationMapper.fromModelToDAOAccess(event.access), + accessRoleList = conversationMapper.fromModelToDAOAccessRole(event.accessRole) + ) + } +} From 79a0ff8b9a21aea7f7d293ef040b4436d7d5e9f5 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:41:03 +0200 Subject: [PATCH 04/11] feat: add return of Either for access update handler --- .../receiver/ConversationEventReceiver.kt | 6 +----- .../ConversationAccessUpdateHandler.kt | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt index 22d6d13a99..6a2675d514 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt @@ -110,11 +110,7 @@ internal class ConversationEventReceiverImpl( Either.Right(Unit) } - is Event.Conversation.AccessUpdate -> { - conversationAccessUpdateHandler.handle(event) - Either.Right(Unit) - } - + is Event.Conversation.AccessUpdate -> conversationAccessUpdateHandler.handle(event) is Event.Conversation.ConversationMessageTimer -> conversationMessageTimerEventHandler.handle(event) is Event.Conversation.CodeDeleted -> codeDeletedHandler.handle(event) is Event.Conversation.CodeUpdated -> codeUpdatedHandler.handle(event) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt index 85d5e79866..3f98986ab0 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt @@ -17,15 +17,18 @@ */ package com.wire.kalium.logic.sync.receiver.conversation +import com.wire.kalium.logic.StorageFailure import com.wire.kalium.logic.data.conversation.ConversationMapper import com.wire.kalium.logic.data.event.Event import com.wire.kalium.logic.data.id.toDao import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.di.MapperProvider +import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.wrapStorageRequest import com.wire.kalium.persistence.dao.conversation.ConversationDAO interface ConversationAccessUpdateHandler { - suspend fun handle(event: Event.Conversation.AccessUpdate) + suspend fun handle(event: Event.Conversation.AccessUpdate): Either } @Suppress("FunctionNaming") @@ -35,11 +38,12 @@ fun ConversationAccessUpdateHandler( conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) ) = object : ConversationAccessUpdateHandler { - override suspend fun handle(event: Event.Conversation.AccessUpdate) { - conversationDAO.updateAccess( - conversationID = event.conversationId.toDao(), - accessList = conversationMapper.fromModelToDAOAccess(event.access), - accessRoleList = conversationMapper.fromModelToDAOAccessRole(event.accessRole) - ) - } + override suspend fun handle(event: Event.Conversation.AccessUpdate): Either = + wrapStorageRequest { + conversationDAO.updateAccess( + conversationID = event.conversationId.toDao(), + accessList = conversationMapper.fromModelToDAOAccess(event.access), + accessRoleList = conversationMapper.fromModelToDAOAccessRole(event.accessRole) + ) + } } From 783d2c5d5cbde177bff94d3948e544d4fa5ca51d Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:43:25 +0200 Subject: [PATCH 05/11] feat: rename handler to remove redundant conversation --- .../com/wire/kalium/logic/feature/UserSessionScope.kt | 6 +++--- .../kalium/logic/sync/receiver/ConversationEventReceiver.kt | 6 +++--- ...rsationAccessUpdateHandler.kt => AccessUpdateHandler.kt} | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) rename logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/{ConversationAccessUpdateHandler.kt => AccessUpdateHandler.kt} (93%) 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 ddf85ed91e..0f4f559b8f 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 @@ -381,7 +381,7 @@ import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiver import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiverImpl import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandler import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandlerImpl -import com.wire.kalium.logic.sync.receiver.conversation.ConversationAccessUpdateHandler +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandlerImpl import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler @@ -1467,8 +1467,8 @@ class UserSessionScope internal constructor( callRepository = callRepository ) - private val conversationAccessUpdateHandler: ConversationAccessUpdateHandler - get() = ConversationAccessUpdateHandler( + private val conversationAccessUpdateHandler: AccessUpdateHandler + get() = AccessUpdateHandler( conversationDAO = userStorage.database.conversationDAO, selfUserId = userId ) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt index 6a2675d514..3612c7f689 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt @@ -22,7 +22,7 @@ import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.data.event.Event import com.wire.kalium.logic.data.event.EventDeliveryInfo import com.wire.kalium.logic.functional.Either -import com.wire.kalium.logic.sync.receiver.conversation.ConversationAccessUpdateHandler +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler import com.wire.kalium.logic.sync.receiver.conversation.MLSWelcomeEventHandler @@ -58,7 +58,7 @@ internal class ConversationEventReceiverImpl( private val codeDeletedHandler: CodeDeletedHandler, private val typingIndicatorHandler: TypingIndicatorHandler, private val protocolUpdateEventHandler: ProtocolUpdateEventHandler, - private val conversationAccessUpdateHandler: ConversationAccessUpdateHandler + private val accessUpdateHandler: AccessUpdateHandler ) : ConversationEventReceiver { override suspend fun onEvent(event: Event.Conversation, deliveryInfo: EventDeliveryInfo): Either { // TODO: Make sure errors are accounted for by each handler. @@ -110,7 +110,7 @@ internal class ConversationEventReceiverImpl( Either.Right(Unit) } - is Event.Conversation.AccessUpdate -> conversationAccessUpdateHandler.handle(event) + is Event.Conversation.AccessUpdate -> accessUpdateHandler.handle(event) is Event.Conversation.ConversationMessageTimer -> conversationMessageTimerEventHandler.handle(event) is Event.Conversation.CodeDeleted -> codeDeletedHandler.handle(event) is Event.Conversation.CodeUpdated -> codeUpdatedHandler.handle(event) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt similarity index 93% rename from logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt rename to logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt index 3f98986ab0..ace244248d 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/ConversationAccessUpdateHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt @@ -27,16 +27,16 @@ import com.wire.kalium.logic.functional.Either import com.wire.kalium.logic.wrapStorageRequest import com.wire.kalium.persistence.dao.conversation.ConversationDAO -interface ConversationAccessUpdateHandler { +interface AccessUpdateHandler { suspend fun handle(event: Event.Conversation.AccessUpdate): Either } @Suppress("FunctionNaming") -fun ConversationAccessUpdateHandler( +fun AccessUpdateHandler( selfUserId: UserId, conversationDAO: ConversationDAO, conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) -) = object : ConversationAccessUpdateHandler { +) = object : AccessUpdateHandler { override suspend fun handle(event: Event.Conversation.AccessUpdate): Either = wrapStorageRequest { From 26aadb8c9d881e2a9aa1321e44bdcb8c5d6cec93 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 12:59:35 +0200 Subject: [PATCH 06/11] feat: rename clashing function naming --- .../kalium/logic/data/conversation/ConversationMapper.kt | 8 ++++---- .../com/wire/kalium/logic/data/event/EventMapper.kt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) 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 0f86a17808..5888013bf6 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 @@ -88,8 +88,8 @@ interface ConversationMapper { fun fromModelToDAOAccess(accessList: Set): List fun fromModelToDAOAccessRole(accessRoleList: Set): List - fun fromApiModel(accessList: Set): Set - fun fromApiModel(accessRoleList: Set): Set + fun fromApiModelModelAccess(accessList: Set): Set + fun fromApiModelModelAccessRole(accessRoleList: Set): Set } @Suppress("TooManyFunctions", "LongParameterList") @@ -473,10 +473,10 @@ internal class ConversationMapperImpl( override fun fromModelToDAOAccessRole(accessRoleList: Set): List = accessRoleList.map { it.toDAO() } - override fun fromApiModel(accessList: Set): Set = + override fun fromApiModelModelAccess(accessList: Set): Set = accessList.map { it.toModel() }.toSet() - override fun fromApiModel(accessRoleList: Set): Set = + override fun fromApiModelModelAccessRole(accessRoleList: Set): Set = accessRoleList.map { it.toModel() }.toSet() } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt index 5e0b2eca92..47343560bb 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt @@ -202,8 +202,8 @@ class EventMapper( ): Event = Event.Conversation.AccessUpdate( id = id, conversationId = eventContentDTO.qualifiedConversation.toModel(), - access = conversationMapper.fromApiModel(eventContentDTO.data.access), - accessRole = conversationMapper.fromApiModel(eventContentDTO.data.accessRole), + access = conversationMapper.fromApiModelModelAccess(eventContentDTO.data.access), + accessRole = conversationMapper.fromApiModelModelAccessRole(eventContentDTO.data.accessRole), qualifiedFrom = eventContentDTO.qualifiedFrom.toModel() ) From 14faff7dde338e88ca2be34f318d92e0e4cd85b8 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 13:03:10 +0200 Subject: [PATCH 07/11] test: add tests for receiving access update event --- .../wire/kalium/logic/framework/TestEvent.kt | 15 +++-- .../receiver/ConversationEventReceiverTest.kt | 66 +++++++++++++++---- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/framework/TestEvent.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/framework/TestEvent.kt index 2ffcb8c3a9..48cde9de04 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/framework/TestEvent.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/framework/TestEvent.kt @@ -139,6 +139,14 @@ object TestEvent { senderUserId = TestUser.USER_ID ) + fun accessUpdate(eventId: String = "eventId") = Event.Conversation.AccessUpdate( + id = eventId, + conversationId = TestConversation.ID, + access = setOf(Conversation.Access.PRIVATE), + accessRole = setOf(Conversation.AccessRole.TEAM_MEMBER, Conversation.AccessRole.SERVICE), + qualifiedFrom = TestUser.USER_ID + ) + fun teamMemberLeave(eventId: String = "eventId") = Event.Team.MemberLeave( eventId, teamId = "teamId", @@ -200,13 +208,6 @@ object TestEvent { timestampIso = "2022-03-30T15:36:00.000Z" ) - fun newAccessUpdateEvent() = Event.Conversation.AccessUpdate( - id = "eventId", - conversationId = TestConversation.ID, - data = TestConversation.CONVERSATION_RESPONSE, - qualifiedFrom = TestUser.USER_ID, - ) - fun codeUpdated() = Event.Conversation.CodeUpdated( id = "eventId", conversationId = TestConversation.ID, diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt index ed9df56c93..15c1f5bdd7 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt @@ -23,6 +23,7 @@ import com.wire.kalium.logic.data.conversation.Conversation import com.wire.kalium.logic.framework.TestEvent import com.wire.kalium.logic.framework.TestUser import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler import com.wire.kalium.logic.sync.receiver.conversation.MLSWelcomeEventHandler @@ -208,17 +209,6 @@ class ConversationEventReceiverTest { result.shouldSucceed() } - @Test - fun givenAccessUpdateEvent_whenOnEventInvoked_thenReturnSuccess() = runTest { - val accessUpdateEvent = TestEvent.newAccessUpdateEvent() - - val (_, featureConfigEventReceiver) = Arrangement().arrange() - - val result = featureConfigEventReceiver.onEvent(accessUpdateEvent, TestEvent.liveDeliveryInfo) - - result.shouldSucceed() - } - @Test fun givenConversationMessageTimerEvent_whenOnEventInvoked_thenPropagateConversationMessageTimerEventHandlerResult() = runTest { @@ -339,6 +329,48 @@ class ConversationEventReceiverTest { result.shouldFail() } + @Test + fun givenAccessUpdateEventAndHandlingSucceeds_whenOnEventInvoked_thenSuccessHandlerResult() = runTest { + // given + val accessUpdateEvent = TestEvent.accessUpdate() + val (arrangement, handler) = Arrangement() + .withConversationAccessUpdateEventSucceeded(Either.Right(Unit)) + .arrange() + + // when + val result = handler.onEvent( + event = accessUpdateEvent, + deliveryInfo = TestEvent.liveDeliveryInfo + ) + + // then + result.shouldSucceed() + coVerify { + arrangement.accessUpdateEventHandler.handle(eq(accessUpdateEvent)) + }.wasInvoked(once) + } + + @Test + fun givenAccessUpdateEventAndHandlingFails_whenOnEventInvoked_thenHandlerPropagateFails() = runTest { + // given + val accessUpdateEvent = TestEvent.accessUpdate() + val (arrangement, handler) = Arrangement() + .withConversationAccessUpdateEventSucceeded(Either.Left(StorageFailure.Generic(RuntimeException("some error")))) + .arrange() + + // when + val result = handler.onEvent( + event = accessUpdateEvent, + deliveryInfo = TestEvent.liveDeliveryInfo + ) + + // then + result.shouldFail() + coVerify { + arrangement.accessUpdateEventHandler.handle(eq(accessUpdateEvent)) + }.wasInvoked(once) + } + private class Arrangement : CodeUpdatedHandlerArrangement by CodeUpdatedHandlerArrangementImpl(), CodeDeletedHandlerArrangement by CodeDeletedHandlerArrangementImpl() { @@ -379,6 +411,9 @@ class ConversationEventReceiverTest { @Mock val protocolUpdateEventHandler = mock(ProtocolUpdateEventHandler::class) + @Mock + val accessUpdateEventHandler = mock(AccessUpdateHandler::class) + private val conversationEventReceiver: ConversationEventReceiver = ConversationEventReceiverImpl( newMessageHandler = newMessageEventHandler, newConversationHandler = newConversationEventHandler, @@ -393,7 +428,8 @@ class ConversationEventReceiverTest { codeUpdatedHandler = codeUpdatedHandler, codeDeletedHandler = codeDeletedHandler, typingIndicatorHandler = typingIndicatorHandler, - protocolUpdateEventHandler = protocolUpdateEventHandler + protocolUpdateEventHandler = protocolUpdateEventHandler, + accessUpdateHandler = accessUpdateEventHandler ) fun arrange(block: suspend Arrangement.() -> Unit = {}) = run { @@ -425,6 +461,12 @@ class ConversationEventReceiverTest { }.returns(result) } + suspend fun withConversationAccessUpdateEventSucceeded(result: Either) = apply { + coEvery { + accessUpdateEventHandler.handle(any()) + }.returns(result) + } + suspend fun withMLSWelcomeEventSucceeded() = apply { coEvery { mlsWelcomeEventHandler.handle(any()) From eee3c6a06b904a83c1765f0854ebfc49d146d1d7 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 13:15:40 +0200 Subject: [PATCH 08/11] chore: rename mapper method naming --- .../kalium/logic/data/conversation/ConversationMapper.kt | 8 ++++---- .../com/wire/kalium/logic/data/event/EventMapper.kt | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) 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 5888013bf6..1fc0dce8fb 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 @@ -88,8 +88,8 @@ interface ConversationMapper { fun fromModelToDAOAccess(accessList: Set): List fun fromModelToDAOAccessRole(accessRoleList: Set): List - fun fromApiModelModelAccess(accessList: Set): Set - fun fromApiModelModelAccessRole(accessRoleList: Set): Set + fun fromApiModelToAccessModel(accessList: Set): Set + fun fromApiModelToAccessRoleModel(accessRoleList: Set): Set } @Suppress("TooManyFunctions", "LongParameterList") @@ -473,10 +473,10 @@ internal class ConversationMapperImpl( override fun fromModelToDAOAccessRole(accessRoleList: Set): List = accessRoleList.map { it.toDAO() } - override fun fromApiModelModelAccess(accessList: Set): Set = + override fun fromApiModelToAccessModel(accessList: Set): Set = accessList.map { it.toModel() }.toSet() - override fun fromApiModelModelAccessRole(accessRoleList: Set): Set = + override fun fromApiModelToAccessRoleModel(accessRoleList: Set): Set = accessRoleList.map { it.toModel() }.toSet() } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt index 47343560bb..ba1fb539fd 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/event/EventMapper.kt @@ -202,8 +202,8 @@ class EventMapper( ): Event = Event.Conversation.AccessUpdate( id = id, conversationId = eventContentDTO.qualifiedConversation.toModel(), - access = conversationMapper.fromApiModelModelAccess(eventContentDTO.data.access), - accessRole = conversationMapper.fromApiModelModelAccessRole(eventContentDTO.data.accessRole), + access = conversationMapper.fromApiModelToAccessModel(eventContentDTO.data.access), + accessRole = conversationMapper.fromApiModelToAccessRoleModel(eventContentDTO.data.accessRole), qualifiedFrom = eventContentDTO.qualifiedFrom.toModel() ) From d2d0480967449acb44eb42a1a525859b90db64a6 Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 13:16:21 +0200 Subject: [PATCH 09/11] test: add test for new mappings in ConversationMapper --- .../conversation/ConversationMapperTest.kt | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) 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 335b64421f..4861d10b8c 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 @@ -234,6 +234,110 @@ class ConversationMapperTest { assertEquals(ConversationEntity.Type.GROUP, result) } + @Test + fun givenAccessList_whenMappingFromModelToDAOAccess_thenCorrectValuesShouldBeReturned() { + // given + val accessList = setOf( + Conversation.Access.PRIVATE, + Conversation.Access.CODE, + Conversation.Access.INVITE, + Conversation.Access.LINK, + Conversation.Access.SELF_INVITE + ) + + val expected = listOf( + ConversationEntity.Access.PRIVATE, + ConversationEntity.Access.CODE, + ConversationEntity.Access.INVITE, + ConversationEntity.Access.LINK, + ConversationEntity.Access.SELF_INVITE + ) + + // when + val result = conversationMapper.fromModelToDAOAccess(accessList) + + // then + assertEquals(expected, result) + } + + @Test + fun givenAccessRoleList_whenMappingFromModelToDAOAccessRole_thenCorrectValuesShouldBeReturned() { + // given + val accessRoleList = setOf( + Conversation.AccessRole.SERVICE, + Conversation.AccessRole.GUEST, + Conversation.AccessRole.TEAM_MEMBER, + Conversation.AccessRole.NON_TEAM_MEMBER, + Conversation.AccessRole.EXTERNAL + ) + + val expected = listOf( + ConversationEntity.AccessRole.SERVICE, + ConversationEntity.AccessRole.GUEST, + ConversationEntity.AccessRole.TEAM_MEMBER, + ConversationEntity.AccessRole.NON_TEAM_MEMBER, + ConversationEntity.AccessRole.EXTERNAL + ) + + // when + val result = conversationMapper.fromModelToDAOAccessRole(accessRoleList) + + // then + assertEquals(expected, result) + } + + @Test + fun givenAccessList_whenMappingFromApiModelToAccessModel_thenCorrectValuesShouldBeReturned() { + // given + val accessList = setOf( + ConversationAccessDTO.PRIVATE, + ConversationAccessDTO.CODE, + ConversationAccessDTO.INVITE, + ConversationAccessDTO.LINK, + ConversationAccessDTO.SELF_INVITE + ) + + val expected = setOf( + Conversation.Access.PRIVATE, + Conversation.Access.CODE, + Conversation.Access.INVITE, + Conversation.Access.LINK, + Conversation.Access.SELF_INVITE + ) + + // when + val result = conversationMapper.fromApiModelToAccessModel(accessList) + + // then + assertEquals(expected, result) + } + + @Test + fun givenAccessRoleList_whenMappingFromApiModelToAccessModel_thenCorrectValuesShouldBeReturned() { + // given + val accessRoleList = setOf( + ConversationAccessRoleDTO.SERVICE, + ConversationAccessRoleDTO.GUEST, + ConversationAccessRoleDTO.TEAM_MEMBER, + ConversationAccessRoleDTO.NON_TEAM_MEMBER, + ConversationAccessRoleDTO.EXTERNAL + ) + + val expected = setOf( + Conversation.AccessRole.SERVICE, + Conversation.AccessRole.GUEST, + Conversation.AccessRole.TEAM_MEMBER, + Conversation.AccessRole.NON_TEAM_MEMBER, + Conversation.AccessRole.EXTERNAL + ) + + // when + val result = conversationMapper.fromApiModelToAccessRoleModel(accessRoleList) + + // then + assertEquals(expected, result) + } + private companion object { val ORIGINAL_CONVERSATION_ID = ConversationId("original", "oDomain") val SELF_USER_TEAM_ID = TeamId("teamID") From 809d79cc833c32b29ebb19076ed0d2dea301567a Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 15:13:59 +0200 Subject: [PATCH 10/11] chore: renaming of access update event handler --- .../com/wire/kalium/logic/feature/UserSessionScope.kt | 8 ++++---- .../logic/sync/receiver/ConversationEventReceiver.kt | 6 +++--- ...AccessUpdateHandler.kt => AccessUpdateEventHandler.kt} | 6 +++--- .../logic/sync/receiver/ConversationEventReceiverTest.kt | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) rename logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/{AccessUpdateHandler.kt => AccessUpdateEventHandler.kt} (94%) 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 0f4f559b8f..4c0c5e9ed4 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 @@ -381,7 +381,7 @@ import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiver import com.wire.kalium.logic.sync.receiver.UserPropertiesEventReceiverImpl import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandler import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandlerImpl -import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandlerImpl import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler @@ -1467,8 +1467,8 @@ class UserSessionScope internal constructor( callRepository = callRepository ) - private val conversationAccessUpdateHandler: AccessUpdateHandler - get() = AccessUpdateHandler( + private val conversationAccessUpdateEventHandler: AccessUpdateEventHandler + get() = AccessUpdateEventHandler( conversationDAO = userStorage.database.conversationDAO, selfUserId = userId ) @@ -1489,7 +1489,7 @@ class UserSessionScope internal constructor( conversationCodeDeletedHandler, typingIndicatorHandler, protocolUpdateEventHandler, - conversationAccessUpdateHandler + conversationAccessUpdateEventHandler ) } override val coroutineContext: CoroutineContext = SupervisorJob() diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt index 3612c7f689..d6e5ae16ed 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiver.kt @@ -22,7 +22,7 @@ import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.data.event.Event import com.wire.kalium.logic.data.event.EventDeliveryInfo import com.wire.kalium.logic.functional.Either -import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler import com.wire.kalium.logic.sync.receiver.conversation.MLSWelcomeEventHandler @@ -58,7 +58,7 @@ internal class ConversationEventReceiverImpl( private val codeDeletedHandler: CodeDeletedHandler, private val typingIndicatorHandler: TypingIndicatorHandler, private val protocolUpdateEventHandler: ProtocolUpdateEventHandler, - private val accessUpdateHandler: AccessUpdateHandler + private val accessUpdateEventHandler: AccessUpdateEventHandler ) : ConversationEventReceiver { override suspend fun onEvent(event: Event.Conversation, deliveryInfo: EventDeliveryInfo): Either { // TODO: Make sure errors are accounted for by each handler. @@ -110,7 +110,7 @@ internal class ConversationEventReceiverImpl( Either.Right(Unit) } - is Event.Conversation.AccessUpdate -> accessUpdateHandler.handle(event) + is Event.Conversation.AccessUpdate -> accessUpdateEventHandler.handle(event) is Event.Conversation.ConversationMessageTimer -> conversationMessageTimerEventHandler.handle(event) is Event.Conversation.CodeDeleted -> codeDeletedHandler.handle(event) is Event.Conversation.CodeUpdated -> codeUpdatedHandler.handle(event) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateEventHandler.kt similarity index 94% rename from logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt rename to logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateEventHandler.kt index ace244248d..8671cdd280 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandler.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateEventHandler.kt @@ -27,16 +27,16 @@ import com.wire.kalium.logic.functional.Either import com.wire.kalium.logic.wrapStorageRequest import com.wire.kalium.persistence.dao.conversation.ConversationDAO -interface AccessUpdateHandler { +interface AccessUpdateEventHandler { suspend fun handle(event: Event.Conversation.AccessUpdate): Either } @Suppress("FunctionNaming") -fun AccessUpdateHandler( +fun AccessUpdateEventHandler( selfUserId: UserId, conversationDAO: ConversationDAO, conversationMapper: ConversationMapper = MapperProvider.conversationMapper(selfUserId) -) = object : AccessUpdateHandler { +) = object : AccessUpdateEventHandler { override suspend fun handle(event: Event.Conversation.AccessUpdate): Either = wrapStorageRequest { diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt index 15c1f5bdd7..f106495aa4 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/ConversationEventReceiverTest.kt @@ -23,7 +23,7 @@ import com.wire.kalium.logic.data.conversation.Conversation import com.wire.kalium.logic.framework.TestEvent import com.wire.kalium.logic.framework.TestUser import com.wire.kalium.logic.functional.Either -import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateHandler +import com.wire.kalium.logic.sync.receiver.conversation.AccessUpdateEventHandler import com.wire.kalium.logic.sync.receiver.conversation.ConversationMessageTimerEventHandler import com.wire.kalium.logic.sync.receiver.conversation.DeletedConversationEventHandler import com.wire.kalium.logic.sync.receiver.conversation.MLSWelcomeEventHandler @@ -412,7 +412,7 @@ class ConversationEventReceiverTest { val protocolUpdateEventHandler = mock(ProtocolUpdateEventHandler::class) @Mock - val accessUpdateEventHandler = mock(AccessUpdateHandler::class) + val accessUpdateEventHandler = mock(AccessUpdateEventHandler::class) private val conversationEventReceiver: ConversationEventReceiver = ConversationEventReceiverImpl( newMessageHandler = newMessageEventHandler, @@ -429,7 +429,7 @@ class ConversationEventReceiverTest { codeDeletedHandler = codeDeletedHandler, typingIndicatorHandler = typingIndicatorHandler, protocolUpdateEventHandler = protocolUpdateEventHandler, - accessUpdateHandler = accessUpdateEventHandler + accessUpdateEventHandler = accessUpdateEventHandler ) fun arrange(block: suspend Arrangement.() -> Unit = {}) = run { From e38390a51540ef9d9dad4dac69a7b0cf061602fe Mon Sep 17 00:00:00 2001 From: alexandreferris Date: Fri, 30 Aug 2024 15:14:11 +0200 Subject: [PATCH 11/11] test: add tests for AccessUpdateHandler --- .../conversation/AccessUpdateHandlerTest.kt | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandlerTest.kt diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandlerTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandlerTest.kt new file mode 100644 index 0000000000..2b99f10d69 --- /dev/null +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/sync/receiver/conversation/AccessUpdateHandlerTest.kt @@ -0,0 +1,118 @@ +/* + * Wire + * Copyright (C) 2024 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + */ +package com.wire.kalium.logic.sync.receiver.conversation + +import com.wire.kalium.logic.StorageFailure +import com.wire.kalium.logic.data.conversation.Conversation.Access +import com.wire.kalium.logic.data.conversation.Conversation.AccessRole +import com.wire.kalium.logic.data.conversation.ConversationMapper +import com.wire.kalium.logic.data.id.PersistenceQualifiedId +import com.wire.kalium.logic.data.id.toDao +import com.wire.kalium.logic.framework.TestConversation +import com.wire.kalium.logic.framework.TestEvent +import com.wire.kalium.logic.framework.TestUser +import com.wire.kalium.logic.functional.Either +import com.wire.kalium.persistence.dao.conversation.ConversationDAO +import com.wire.kalium.persistence.dao.conversation.ConversationEntity +import io.mockative.Mock +import io.mockative.any +import io.mockative.coEvery +import io.mockative.coVerify +import io.mockative.every +import io.mockative.matches +import io.mockative.mock +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.runTest +import kotlin.test.Test + +class AccessUpdateHandlerTest { + + @Test + fun givenConversationAccessUpdateEvent_whenHandlingIt_thenShouldCallUpdateDatabase() = runTest { + // given + val event = TestEvent.accessUpdate() + + val (arrangement, eventHandler) = Arrangement() + .withMappingModelToDAOAccess( + event.access, + listOf(ConversationEntity.Access.PRIVATE) + ) + .withMappingModelToDAOAccessRole( + event.accessRole, + listOf(ConversationEntity.AccessRole.TEAM_MEMBER, ConversationEntity.AccessRole.SERVICE) + ) + .arrange() + + // when + eventHandler.handle(event) + + // then + coVerify { + arrangement.conversationDAO.updateAccess( + matches { + it == PersistenceQualifiedId( + value = TestConversation.ID.value, + domain = TestConversation.ID.domain + ) + }, + matches { + it.contains(ConversationEntity.Access.PRIVATE) + }, + matches { + it.contains(ConversationEntity.AccessRole.TEAM_MEMBER) && + it.contains(ConversationEntity.AccessRole.SERVICE) + } + ) + } + } + + private class Arrangement { + + @Mock + val conversationDAO = mock(ConversationDAO::class) + + @Mock + val conversationMapper = mock(ConversationMapper::class) + + init { + runBlocking { + coEvery { conversationDAO.updateAccess(any(), any(), any()) }.returns(Unit) + } + } + + private val accessUpdateEventHandler: AccessUpdateEventHandler = AccessUpdateEventHandler( + selfUserId = TestUser.USER_ID, + conversationDAO = conversationDAO, + conversationMapper = conversationMapper + ) + + fun withMappingModelToDAOAccess(param: Set, result: List) = apply { + every { + conversationMapper.fromModelToDAOAccess(param) + }.returns(result) + } + + fun withMappingModelToDAOAccessRole(param: Set, result: List) = apply { + every { + conversationMapper.fromModelToDAOAccessRole(param) + }.returns(result) + } + + fun arrange() = this to accessUpdateEventHandler + } +}