-
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.
- Loading branch information
Showing
6 changed files
with
324 additions
and
147 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
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
94 changes: 94 additions & 0 deletions
94
...ain/kotlin/com/wire/kalium/logic/feature/message/confirmation/SendDeliverSignalUseCase.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,94 @@ | ||
/* | ||
* 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.confirmation | ||
|
||
import com.benasher44.uuid.uuid4 | ||
import com.wire.kalium.logger.KaliumLogLevel | ||
import com.wire.kalium.logger.KaliumLogger | ||
import com.wire.kalium.logger.obfuscateId | ||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.conversation.Conversation | ||
import com.wire.kalium.logic.data.id.CurrentClientIdProvider | ||
import com.wire.kalium.logic.data.id.MessageId | ||
import com.wire.kalium.logic.data.message.Message | ||
import com.wire.kalium.logic.data.message.MessageContent | ||
import com.wire.kalium.logic.data.message.receipt.ReceiptType | ||
import com.wire.kalium.logic.data.user.UserId | ||
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.logic.functional.onSuccess | ||
import com.wire.kalium.logic.logStructuredJson | ||
import kotlinx.datetime.Clock | ||
|
||
/** | ||
* Use case for sending a delivery confirmation signal for a list of messages in a conversation. | ||
*/ | ||
interface SendDeliverSignalUseCase { | ||
suspend operator fun invoke(conversation: Conversation, messages: List<MessageId>): Either<CoreFailure, Unit> | ||
} | ||
|
||
internal class SendDeliverSignalUseCaseImpl( | ||
private val selfUserId: UserId, | ||
private val messageSender: MessageSender, | ||
private val currentClientIdProvider: CurrentClientIdProvider, | ||
private val kaliumLogger: KaliumLogger | ||
) : SendDeliverSignalUseCase { | ||
override suspend fun invoke( | ||
conversation: Conversation, | ||
messages: List<MessageId> | ||
): Either<CoreFailure, Unit> = currentClientIdProvider() | ||
.flatMap { currentClientId -> | ||
val message = Message.Signaling( | ||
id = uuid4().toString(), | ||
content = MessageContent.Receipt(ReceiptType.DELIVERED, messages), | ||
conversationId = conversation.id, | ||
date = Clock.System.now(), | ||
senderUserId = selfUserId, | ||
senderClientId = currentClientId, | ||
status = Message.Status.Pending, | ||
isSelfMessage = true, | ||
expirationData = null | ||
) | ||
messageSender.sendMessage(message) | ||
.onFailure { error -> | ||
kaliumLogger.logStructuredJson( | ||
level = KaliumLogLevel.ERROR, | ||
leadingMessage = "Error while sending delivery confirmation for ${conversation.id.toLogString()}", | ||
jsonStringKeyValues = mapOf( | ||
"conversationId" to conversation.id.toLogString(), | ||
"messages" to messages.joinToString { it.obfuscateId() }, | ||
"error" to error.toString() | ||
) | ||
) | ||
} | ||
.onSuccess { | ||
kaliumLogger.logStructuredJson( | ||
level = KaliumLogLevel.DEBUG, | ||
leadingMessage = "Delivery confirmation sent for ${conversation.id.toLogString()}" + | ||
" and message count: ${messages.size}", | ||
jsonStringKeyValues = mapOf( | ||
"conversationId" to conversation.id.toLogString(), | ||
"messages" to messages.joinToString { it.obfuscateId() }, | ||
"messageCount" to messages.size | ||
) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.