Skip to content

Commit

Permalink
feat: handle composite action confermation message [part-3] (#1901)
Browse files Browse the repository at this point in the history
* receive composite messages

* map composite to proto

* detekt

* detekt

* db migration

* db migration

* sqm

* map to composite entity

* detekt

* remove is pending state from the DB

* Update logic/src/commonMain/kotlin/com/wire/kalium/logic/data/message/MessageContent.kt

Co-authored-by: Vitor Hugo Schwaab <[email protected]>

* Update logic/src/commonMain/kotlin/com/wire/kalium/logic/data/message/MessageContent.kt

Co-authored-by: Vitor Hugo Schwaab <[email protected]>

* address PR comments

* detekt

* remove composite message preview mapping

* fix migration

* feat: use case to execute composite message action

* expose use case to message scope

* missing param

* rename use case

* docs

* map to proto

* merge issues

* address PR comments

* address PR comments

* detekt

* handle ButtonActionConfirmation event

* add unit test

* detekt

* detekt

* sent the message only to the message original sender

* detekt

---------

Co-authored-by: Vitor Hugo Schwaab <[email protected]>
  • Loading branch information
MohamadJaara and vitorhugods authored Jul 24, 2023
1 parent 709e72e commit be085c9
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import com.wire.kalium.logic.sync.receiver.EventReceiver
import com.wire.kalium.logic.sync.receiver.conversation.MemberJoinEventHandler
import com.wire.kalium.logic.sync.receiver.conversation.MemberLeaveEventHandler

interface CommitBundleEventReceiver : EventReceiver<Event.Conversation>
internal interface CommitBundleEventReceiver : EventReceiver<Event.Conversation>

class CommitBundleEventReceiverImpl(
internal class CommitBundleEventReceiverImpl(
private val memberJoinEventHandler: MemberJoinEventHandler,
private val memberLeaveEventHandler: MemberLeaveEventHandler
) : CommitBundleEventReceiver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private fun CoreFailure.getStrategy(retryOnClientMismatch: Boolean = true): Comm
}

@Suppress("TooManyFunctions", "LongParameterList")
class MLSConversationDataSource(
internal class MLSConversationDataSource(
private val keyPackageRepository: KeyPackageRepository,
private val mlsClientProvider: MLSClientProvider,
private val mlsMessageApi: MLSMessageApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,30 @@ package com.wire.kalium.logic.data.message

import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.MessageId
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.wrapStorageRequest
import com.wire.kalium.persistence.dao.message.CompositeMessageDAO

interface CompositeMessageRepository {
suspend fun markSelected(
messageId: String,
messageId: MessageId,
conversationId: ConversationId,
buttonId: String
): Either<StorageFailure, Unit>

suspend fun resetSelection(
messageId: String,
messageId: MessageId,
conversationId: ConversationId
): Either<StorageFailure, Unit>
}

internal class CompositeMessageDatasource internal constructor(
internal class CompositeMessageDataSource internal constructor(
private val compositeMessageDAO: CompositeMessageDAO
) : CompositeMessageRepository {
override suspend fun markSelected(
messageId: String,
messageId: MessageId,
conversationId: ConversationId,
buttonId: String
): Either<StorageFailure, Unit> = wrapStorageRequest {
Expand All @@ -53,7 +54,7 @@ internal class CompositeMessageDatasource internal constructor(
}

override suspend fun resetSelection(
messageId: String,
messageId: MessageId,
conversationId: ConversationId
): Either<StorageFailure, Unit> = wrapStorageRequest {
compositeMessageDAO.resetSelection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ import com.wire.kalium.logic.data.keypackage.KeyPackageLimitsProviderImpl
import com.wire.kalium.logic.data.keypackage.KeyPackageRepository
import com.wire.kalium.logic.data.logout.LogoutDataSource
import com.wire.kalium.logic.data.logout.LogoutRepository
import com.wire.kalium.logic.data.message.CompositeMessageDataSource
import com.wire.kalium.logic.data.message.CompositeMessageRepository
import com.wire.kalium.logic.data.message.IsMessageSentInSelfConversationUseCase
import com.wire.kalium.logic.data.message.IsMessageSentInSelfConversationUseCaseImpl
import com.wire.kalium.logic.data.message.MessageDataSource
Expand Down Expand Up @@ -306,6 +308,8 @@ import com.wire.kalium.logic.sync.receiver.conversation.message.MLSWrongEpochHan
import com.wire.kalium.logic.sync.receiver.conversation.message.NewMessageEventHandlerImpl
import com.wire.kalium.logic.sync.receiver.conversation.message.ProteusMessageUnpacker
import com.wire.kalium.logic.sync.receiver.conversation.message.ProteusMessageUnpackerImpl
import com.wire.kalium.logic.sync.receiver.handler.ButtonActionConfirmationHandler
import com.wire.kalium.logic.sync.receiver.handler.ButtonActionConfirmationHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.ClearConversationContentHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.DeleteForMeHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.DeleteMessageHandlerImpl
Expand Down Expand Up @@ -997,6 +1001,9 @@ class UserSessionScope internal constructor(
validateAssetMimeType
)

private val buttonActionConfirmationHandler: ButtonActionConfirmationHandler
get() = ButtonActionConfirmationHandlerImpl(compositeMessageRepository)

private val applicationMessageHandler: ApplicationMessageHandler
get() = ApplicationMessageHandlerImpl(
userRepository,
Expand All @@ -1016,6 +1023,7 @@ class UserSessionScope internal constructor(
DeleteMessageHandlerImpl(messageRepository, assetRepository, userId),
messageEncoder,
receiptMessageHandler,
buttonActionConfirmationHandler,
userId
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.wire.kalium.logic.sync.receiver.conversation.ReceiptModeUpdateEventHa
import com.wire.kalium.logic.sync.receiver.conversation.RenamedConversationEventHandler
import com.wire.kalium.logic.sync.receiver.conversation.message.NewMessageEventHandler

interface ConversationEventReceiver : EventReceiver<Event.Conversation>
internal interface ConversationEventReceiver : EventReceiver<Event.Conversation>

// Suppressed as it's an old issue
// TODO(refactor): Create a `MessageEventReceiver` to offload some logic from here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.functional.Either

fun interface EventReceiver<T : Event> {
internal fun interface EventReceiver<T : Event> {
suspend fun onEvent(event: T): Either<CoreFailure, Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.wire.kalium.logic.functional.onSuccess
import com.wire.kalium.logic.kaliumLogger
import kotlinx.coroutines.flow.first

interface TeamEventReceiver : EventReceiver<Event.Team>
internal interface TeamEventReceiver : EventReceiver<Event.Team>

internal class TeamEventReceiverImpl(
private val teamRepository: TeamRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import com.wire.kalium.logic.functional.onFailure
import com.wire.kalium.logic.functional.onSuccess
import com.wire.kalium.logic.kaliumLogger

interface UserEventReceiver : EventReceiver<Event.User>
internal interface UserEventReceiver : EventReceiver<Event.User>

@Suppress("LongParameterList")
class UserEventReceiverImpl internal constructor(
internal class UserEventReceiverImpl internal constructor(
private val clientRepository: ClientRepository,
private val connectionRepository: ConnectionRepository,
private val conversationRepository: ConversationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import com.wire.kalium.logic.functional.onFailure
import com.wire.kalium.logic.functional.onSuccess
import com.wire.kalium.logic.kaliumLogger

interface UserPropertiesEventReceiver : EventReceiver<Event.UserProperty>
internal interface UserPropertiesEventReceiver : EventReceiver<Event.UserProperty>

class UserPropertiesEventReceiverImpl internal constructor(
internal class UserPropertiesEventReceiverImpl internal constructor(
private val userConfigRepository: UserConfigRepository
) : UserPropertiesEventReceiver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.wire.kalium.logic.functional.getOrElse
import com.wire.kalium.logic.functional.map
import com.wire.kalium.logic.kaliumLogger
import com.wire.kalium.logic.sync.receiver.asset.AssetMessageHandler
import com.wire.kalium.logic.sync.receiver.handler.ButtonActionConfirmationHandler
import com.wire.kalium.logic.sync.receiver.handler.ClearConversationContentHandler
import com.wire.kalium.logic.sync.receiver.handler.DeleteForMeHandler
import com.wire.kalium.logic.sync.receiver.handler.DeleteMessageHandler
Expand Down Expand Up @@ -83,6 +84,7 @@ internal class ApplicationMessageHandlerImpl(
private val deleteMessageHandler: DeleteMessageHandler,
private val messageEncoder: MessageContentEncoder,
private val receiptMessageHandler: ReceiptMessageHandler,
private val buttonActionConfirmationHandler: ButtonActionConfirmationHandler,
private val selfUserId: UserId
) : ApplicationMessageHandler {

Expand Down Expand Up @@ -201,8 +203,11 @@ internal class ApplicationMessageHandlerImpl(
is MessageContent.LastRead -> lastReadContentHandler.handle(signaling, content)
is MessageContent.Cleared -> clearConversationContentHandler.handle(signaling, content)
is MessageContent.Receipt -> receiptMessageHandler.handle(signaling, content)
is MessageContent.ButtonAction -> TODO()
is MessageContent.ButtonActionConfirmation -> TODO()
is MessageContent.ButtonAction -> {
/* no-op */
// TODO(services): we need handle this event if kalium need to support services
}
is MessageContent.ButtonActionConfirmation -> buttonActionConfirmationHandler.handle(signaling.conversationId, content)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Wire
* Copyright (C) 2023 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.handler

import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.message.CompositeMessageRepository
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.functional.Either

internal interface ButtonActionConfirmationHandler {
suspend fun handle(
conversationId: ConversationId,
messageContent: MessageContent.ButtonActionConfirmation
): Either<StorageFailure, Unit>
}

internal class ButtonActionConfirmationHandlerImpl internal constructor(
private val compositeMessageRepository: CompositeMessageRepository
) : ButtonActionConfirmationHandler {

override suspend fun handle(
conversationId: ConversationId,
messageContent: MessageContent.ButtonActionConfirmation
): Either<StorageFailure, Unit> {
val messageId = messageContent.referencedMessageId

return if (messageContent.buttonId != null) {
compositeMessageRepository.markSelected(
messageId = messageId,
conversationId = conversationId,
buttonId = messageContent.buttonId
)
} else {
compositeMessageRepository.resetSelection(
messageId = messageId,
conversationId = conversationId
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.IsMessageSentInSelfConversationUseCase

interface ClearConversationContentHandler {
internal interface ClearConversationContentHandler {
suspend fun handle(
message: Message.Signaling,
messageContent: MessageContent.Cleared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.message.MessageRepository
import com.wire.kalium.logic.kaliumLogger

interface DeleteForMeHandler {
internal interface DeleteForMeHandler {
suspend fun handle(
message: Message.Signaling,
messageContent: MessageContent.DeleteForMe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.user.UserId

interface LastReadContentHandler {
internal interface LastReadContentHandler {
suspend fun handle(
message: Message.Signaling,
messageContent: MessageContent.LastRead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.wire.kalium.logic.kaliumLogger
import com.wire.kalium.persistence.dao.message.MessageEntity
import com.wire.kalium.util.DateTimeUtil

interface MessageTextEditHandler {
internal interface MessageTextEditHandler {
suspend fun handle(
message: Message.Signaling,
messageContent: MessageContent.TextEdited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.wire.kalium.logic.data.message.receipt.ReceiptRepository
import com.wire.kalium.logic.data.user.UserId
import kotlinx.datetime.Instant

interface ReceiptMessageHandler {
internal interface ReceiptMessageHandler {
suspend fun handle(
message: Message.Signaling,
messageContent: MessageContent.Receipt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -957,8 +957,7 @@ class MLSConversationRepositoryTest {
.wasNotInvoked()
}

class Arrangement {
val idMapper: IdMapper = IdMapperImpl()
private class Arrangement {

@Mock
val commitBundleEventReceiver = mock(classOf<CommitBundleEventReceiver>())
Expand All @@ -981,9 +980,6 @@ class MLSConversationRepositoryTest {
@Mock
val mlsMessageApi = mock(classOf<MLSMessageApi>())

@Mock
val conversationApi = mock(classOf<ConversationApi>())

@Mock
val mlsClient = mock(classOf<MLSClient>())

Expand Down
Loading

0 comments on commit be085c9

Please sign in to comment.