Skip to content

Commit

Permalink
Merge branch 'testservice-release-cycle-4.6' into fix/set-correct-cs-…
Browse files Browse the repository at this point in the history
…when-kalium-start-migration
  • Loading branch information
mchenani authored Jun 17, 2024
2 parents 5fc6a32 + 8c9a70c commit 75db67d
Show file tree
Hide file tree
Showing 15 changed files with 925 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.wire.kalium.protobuf.encodeToByteArray
import com.wire.kalium.protobuf.messages.Asset
import com.wire.kalium.protobuf.messages.Button
import com.wire.kalium.protobuf.messages.ButtonAction
import com.wire.kalium.protobuf.messages.ButtonActionConfirmation
import com.wire.kalium.protobuf.messages.Calling
import com.wire.kalium.protobuf.messages.Cleared
import com.wire.kalium.protobuf.messages.ClientAction
Expand Down Expand Up @@ -132,7 +133,7 @@ class ProtoContentMapperImpl(
is MessageContent.Composite -> packComposite(readableContent, expectsReadConfirmation, legalHoldStatus)
is MessageContent.ButtonAction -> packButtonAction(readableContent)

is MessageContent.ButtonActionConfirmation -> TODO()
is MessageContent.ButtonActionConfirmation -> packButtonActionConfirmation(readableContent)
is MessageContent.Location -> packLocation(readableContent, expectsReadConfirmation, legalHoldStatus)
}
}
Expand Down Expand Up @@ -165,6 +166,16 @@ class ProtoContentMapperImpl(
)
)

private fun packButtonActionConfirmation(
readableContent: MessageContent.ButtonActionConfirmation
): GenericMessage.Content.ButtonActionConfirmation =
GenericMessage.Content.ButtonActionConfirmation(
ButtonActionConfirmation(
buttonId = readableContent.buttonId,
referenceMessageId = readableContent.referencedMessageId
)
)

private fun packComposite(
readableContent: MessageContent.Composite,
expectsReadConfirmation: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.wire.kalium.logic.feature.client

import com.wire.kalium.logger.obfuscateId
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.client.ClientRepository
import com.wire.kalium.logic.data.conversation.ClientId
Expand All @@ -28,6 +29,7 @@ import com.wire.kalium.logic.feature.featureConfig.SyncFeatureConfigsUseCase
import com.wire.kalium.logic.feature.session.UpgradeCurrentSessionUseCase
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.nullableFold
import com.wire.kalium.logic.kaliumLogger

/**
* This use case is responsible for getting the client.
Expand Down Expand Up @@ -74,6 +76,7 @@ internal class GetOrRegisterClientUseCaseImpl(

when (result) {
is RegisterClientResult.E2EICertificateRequired -> {
kaliumLogger.i("Client registration blocked because E2EI certificate required")
clientRepository.setClientRegistrationBlockedByE2EI()
upgradeCurrentSessionAndPersistClient(result.client.id)
}
Expand All @@ -86,7 +89,9 @@ internal class GetOrRegisterClientUseCaseImpl(
}

private suspend fun upgradeCurrentSessionAndPersistClient(clientId: ClientId) {
kaliumLogger.i("Upgrade current session for client ${clientId.value.obfuscateId()}")
upgradeCurrentSessionUseCase(clientId).flatMap {
kaliumLogger.i("Persist client ${clientId.value.obfuscateId()}")
clientRepository.persistClientId(clientId)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ import com.wire.kalium.logic.feature.asset.UpdateAssetMessageDownloadStatusUseCa
import com.wire.kalium.logic.feature.asset.UpdateAssetMessageDownloadStatusUseCaseImpl
import com.wire.kalium.logic.feature.asset.UpdateAssetMessageUploadStatusUseCase
import com.wire.kalium.logic.feature.asset.UpdateAssetMessageUploadStatusUseCaseImpl
import com.wire.kalium.logic.feature.message.composite.SendButtonActionConfirmationMessageUseCase
import com.wire.kalium.logic.feature.message.composite.SendButtonActionMessageUseCase
import com.wire.kalium.logic.feature.message.composite.SendButtonMessageUseCase
import com.wire.kalium.logic.feature.message.ephemeral.DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl
import com.wire.kalium.logic.feature.message.ephemeral.DeleteEphemeralMessageForSelfUserAsSenderUseCaseImpl
import com.wire.kalium.logic.feature.message.ephemeral.DeleteEphemeralMessagesAfterEndDateUseCase
Expand Down Expand Up @@ -353,6 +355,14 @@ class MessageScope internal constructor(
val resetSession: ResetSessionUseCase
get() = ResetSessionUseCaseImpl(proteusClientProvider, sessionResetSender, messageRepository)

val sendButtonActionConfirmationMessage: SendButtonActionConfirmationMessageUseCase
get() = SendButtonActionConfirmationMessageUseCase(
syncManager = syncManager,
messageSender = messageSender,
selfUserId = selfUserId,
currentClientIdProvider = currentClientIdProvider
)

val sendButtonActionMessage: SendButtonActionMessageUseCase
get() = SendButtonActionMessageUseCase(
syncManager = syncManager,
Expand All @@ -361,6 +371,19 @@ class MessageScope internal constructor(
currentClientIdProvider = currentClientIdProvider,
messageMetadataRepository = messageMetadataRepository
)

val sendButtonMessage: SendButtonMessageUseCase
get() = SendButtonMessageUseCase(
persistMessage = persistMessage,
selfUserId = selfUserId,
provideClientId = currentClientIdProvider,
slowSyncRepository = slowSyncRepository,
messageSender = messageSender,
messageSendFailureHandler = messageSendFailureHandler,
userPropertyRepository = userPropertyRepository,
scope = scope
)

private val deleteEphemeralMessageForSelfUserAsReceiver: DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl
get() = DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl(
messageRepository = messageRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.feature.message.composite

import com.benasher44.uuid.uuid4
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.data.message.MessageTarget
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.sync.SyncManager
import com.wire.kalium.util.DateTimeUtil

/**
* Use case for sending a button action message.
* @param conversationId The conversation id.
* @param messageId The id of the message that contains the button.
* @param buttonId The id of the button.
*
* the action message is sent only to the message original sender.
*/
class SendButtonActionConfirmationMessageUseCase internal constructor(
private val messageSender: MessageSender,
private val syncManager: SyncManager,
private val currentClientIdProvider: CurrentClientIdProvider,
private val selfUserId: UserId
) {
suspend operator fun invoke(
conversationId: ConversationId,
messageId: String,
buttonId: String,
userIds: List<UserId>
): Result = syncManager.waitUntilLiveOrFailure().flatMap {
currentClientIdProvider().flatMap { currentClientId ->
val regularMessage = Message.Signaling(
id = uuid4().toString(),
content = MessageContent.ButtonActionConfirmation(
referencedMessageId = messageId,
buttonId = buttonId
),
conversationId = conversationId,
date = DateTimeUtil.currentIsoDateTimeString(),
senderUserId = selfUserId,
senderClientId = currentClientId,
status = Message.Status.Pending,
isSelfMessage = true,
expirationData = null
)
messageSender.sendMessage(regularMessage, messageTarget = MessageTarget.Users(userIds))
}
}.fold(Result::Failure, { Result.Success })

sealed interface Result {
data object Success : Result
data class Failure(
val error: CoreFailure
) : Result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.feature.message.composite

import com.benasher44.uuid.uuid4
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.message.PersistMessageUseCase
import com.wire.kalium.logic.data.message.mention.MessageMention
import com.wire.kalium.logic.data.properties.UserPropertyRepository
import com.wire.kalium.logic.data.sync.SlowSyncRepository
import com.wire.kalium.logic.data.sync.SlowSyncStatus
import com.wire.kalium.logic.feature.message.MessageSendFailureHandler
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.onFailure
import com.wire.kalium.util.DateTimeUtil
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.first

@Suppress("LongParameterList")
/**
* @sample samples.logic.MessageUseCases.sendingBasicTextMessage
* @sample samples.logic.MessageUseCases.sendingTextMessageWithMentions
*/
class SendButtonMessageUseCase internal constructor(
private val persistMessage: PersistMessageUseCase,
private val selfUserId: QualifiedID,
private val provideClientId: CurrentClientIdProvider,
private val slowSyncRepository: SlowSyncRepository,
private val messageSender: MessageSender,
private val messageSendFailureHandler: MessageSendFailureHandler,
private val userPropertyRepository: UserPropertyRepository,
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl,
private val scope: CoroutineScope
) {

suspend operator fun invoke(
conversationId: ConversationId,
text: String,
mentions: List<MessageMention> = emptyList(),
quotedMessageId: String? = null,
buttons: List<String> = listOf()
): Either<CoreFailure, Unit> = scope.async(dispatchers.io) {
slowSyncRepository.slowSyncStatus.first {
it is SlowSyncStatus.Complete
}

val generatedMessageUuid = uuid4().toString()
val expectsReadConfirmation = userPropertyRepository.getReadReceiptsStatus()

provideClientId().flatMap { clientId ->
val textContent = MessageContent.Text(
value = text,
mentions = mentions,
quotedMessageReference = quotedMessageId?.let { quotedMessageId ->
MessageContent.QuoteReference(
quotedMessageId = quotedMessageId,
quotedMessageSha256 = null,
isVerified = true
)
}
)

val transform: (String) -> MessageContent.Composite.Button = { MessageContent.Composite.Button(it, it, false) }
val buttonContent = buttons.map(transform)
val content = MessageContent.Composite(textContent, buttonContent)

val message = Message.Regular(
id = generatedMessageUuid,
content = content,
expectsReadConfirmation = expectsReadConfirmation,
conversationId = conversationId,
date = DateTimeUtil.currentIsoDateTimeString(),
senderUserId = selfUserId,
senderClientId = clientId,
status = Message.Status.Pending,
editStatus = Message.EditStatus.NotEdited,
// According to proto Ephemeral it is not possible to send a Composite message with timer
expirationData = null,
isSelfMessage = true
)
persistMessage(message).flatMap {
messageSender.sendMessage(message)
}
}.onFailure {
messageSendFailureHandler.handleFailureAndUpdateMessageStatus(
failure = it,
conversationId = conversationId,
messageId = generatedMessageUuid,
messageType = TYPE
)
}
}.await()

companion object {
const val TYPE = "Text"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.wire.kalium.logic.data.properties.UserPropertyRepository
import com.wire.kalium.logic.data.sync.SlowSyncRepository
import com.wire.kalium.logic.data.sync.SlowSyncStatus
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.feature.selfDeletingMessages.ObserveSelfDeletionTimerSettingsForConversationUseCase
import com.wire.kalium.logic.data.message.SelfDeletionTimer
import com.wire.kalium.logic.framework.TestClient
Expand All @@ -38,6 +39,7 @@ import io.mockative.any
import io.mockative.classOf
import io.mockative.configure
import io.mockative.given
import io.mockative.matching
import io.mockative.mock
import io.mockative.once
import io.mockative.verify
Expand Down Expand Up @@ -73,11 +75,14 @@ class SendTextMessageCaseTest {
.wasInvoked(once)
verify(arrangement.persistMessage)
.suspendFunction(arrangement.persistMessage::invoke)
.with(any())
.with(matching { message -> message.content is MessageContent.Text })
.wasInvoked(once)
verify(arrangement.messageSender)
.suspendFunction(arrangement.messageSender::sendMessage)
.with(any(), any())
.with(
matching { message -> message.content is MessageContent.Text },
any()
)
.wasInvoked(once)
verify(arrangement.messageSendFailureHandler)
.suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus)
Expand Down Expand Up @@ -149,39 +154,44 @@ class SendTextMessageCaseTest {
.whenInvokedWith(any(), any())
.thenReturn(Either.Right(Unit))
}

fun withSendMessageFailure() = apply {
given(messageSender)
.suspendFunction(messageSender::sendMessage)
.whenInvokedWith(any(), any())
.thenReturn(Either.Left(NetworkFailure.NoNetworkConnection(null)))
}

fun withCurrentClientProviderSuccess(clientId: ClientId = TestClient.CLIENT_ID) = apply {
given(currentClientIdProvider)
.suspendFunction(currentClientIdProvider::invoke)
.whenInvoked()
.thenReturn(Either.Right(clientId))
}

fun withPersistMessageSuccess() = apply {
given(persistMessage)
.suspendFunction(persistMessage::invoke)
.whenInvokedWith(any())
.thenReturn(Either.Right(Unit))
}

fun withSlowSyncStatusComplete() = apply {
val stateFlow = MutableStateFlow<SlowSyncStatus>(SlowSyncStatus.Complete).asStateFlow()
given(slowSyncRepository)
.getter(slowSyncRepository::slowSyncStatus)
.whenInvoked()
.thenReturn(stateFlow)
}

fun withToggleReadReceiptsStatus(enabled: Boolean = false) = apply {
given(userPropertyRepository)
.suspendFunction(userPropertyRepository::getReadReceiptsStatus)
.whenInvoked()
.thenReturn(enabled)
}

fun withMessageTimer(result: SelfDeletionTimer) = apply {
fun withMessageTimer(result: SelfDeletionTimer) = apply {
given(observeSelfDeletionTimerSettingsForConversation)
.suspendFunction(observeSelfDeletionTimerSettingsForConversation::invoke)
.whenInvokedWith(any())
Expand Down
Loading

0 comments on commit 75db67d

Please sign in to comment.