-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use case to send button action confirmations (WPB-2633) (#2393)
* feat: use case to send button action confirmations (WPB-2633) * Fixed issue with domain of button sender
- Loading branch information
1 parent
cd403f1
commit ee72866
Showing
8 changed files
with
353 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...wire/kalium/logic/feature/message/composite/SendButtonActionConfirmationMessageUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...om/wire/kalium/logic/feature/message/composite/SendButtonActionConfirmationMessageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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.wire.kalium.logic.data.conversation.ClientId | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.message.MessageTarget | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.util.arrangement.MessageSenderArrangement | ||
import com.wire.kalium.logic.util.arrangement.MessageSenderArrangementImpl | ||
import com.wire.kalium.logic.util.arrangement.SyncManagerArrangement | ||
import com.wire.kalium.logic.util.arrangement.SyncManagerArrangementImpl | ||
import com.wire.kalium.logic.util.arrangement.provider.CurrentClientIdProviderArrangement | ||
import com.wire.kalium.logic.util.arrangement.provider.CurrentClientIdProviderArrangementImpl | ||
import io.mockative.any | ||
import io.mockative.matching | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
|
||
class SendButtonActionConfirmationMessageTest { | ||
|
||
@Test | ||
fun givenMessageSendingSuccess_thenMessageIsSentOnlyToOriginalSenderOfTheButtonAction() = runTest { | ||
val convId = ConversationId("conversation-id", "conversation-domain") | ||
val buttonActionSender = UserId("action-sender-id", "action-sender-domain") | ||
val (arrangement, useCase) = Arrangement() | ||
.arrange { | ||
withWaitUntilLiveOrFailure(Either.Right(Unit)) | ||
withCurrentClientIdSuccess(ClientId("client-id")) | ||
withSendMessageSucceed() | ||
} | ||
|
||
val result = useCase( | ||
conversationId = convId, | ||
messageId = "message-id", | ||
buttonId = "button-id", | ||
userIds = listOf(buttonActionSender) | ||
) | ||
|
||
assertIs<SendButtonActionConfirmationMessageUseCase.Result.Success>(result) | ||
|
||
verify(arrangement.messageSender) | ||
.suspendFunction(arrangement.messageSender::sendMessage) | ||
.with(any(), matching { | ||
it is MessageTarget.Users && it.userId == listOf(buttonActionSender) | ||
}) | ||
.wasInvoked(exactly = once) | ||
|
||
verify(arrangement.currentClientIdProvider) | ||
.suspendFunction(arrangement.currentClientIdProvider::invoke) | ||
.wasInvoked(exactly = once) | ||
|
||
verify(arrangement.syncManager) | ||
.suspendFunction(arrangement.syncManager::waitUntilLiveOrFailure) | ||
.wasInvoked(exactly = once) | ||
} | ||
|
||
private companion object { | ||
val SELF_USER_ID: UserId = UserId("self-user-id", "self-user-domain") | ||
} | ||
|
||
private class Arrangement : | ||
MessageSenderArrangement by MessageSenderArrangementImpl(), | ||
SyncManagerArrangement by SyncManagerArrangementImpl(), | ||
CurrentClientIdProviderArrangement by CurrentClientIdProviderArrangementImpl() { | ||
|
||
private lateinit var useCase: SendButtonActionConfirmationMessageUseCase | ||
|
||
fun arrange(block: Arrangement.() -> Unit): Pair<Arrangement, SendButtonActionConfirmationMessageUseCase> { | ||
apply(block) | ||
useCase = SendButtonActionConfirmationMessageUseCase( | ||
messageSender = messageSender, | ||
syncManager = syncManager, | ||
currentClientIdProvider = currentClientIdProvider, | ||
selfUserId = SELF_USER_ID, | ||
) | ||
|
||
return this to useCase | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.