Skip to content

Commit

Permalink
fix: execute job sending message inside UserSessionScope (#1919)
Browse files Browse the repository at this point in the history
* works fine

* fix

* fix

* fix

* fix scope
  • Loading branch information
trOnk12 authored Jul 25, 2023
1 parent d42a5fd commit 17ec885
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ class MessageScope internal constructor(

val sendTextMessage: SendTextMessageUseCase
get() = SendTextMessageUseCase(
persistMessage,
selfUserId,
currentClientIdProvider,
slowSyncRepository,
messageSender,
messageSendFailureHandler,
userPropertyRepository,
observeSelfDeletingMessages
persistMessage = persistMessage,
selfUserId = selfUserId,
provideClientId = currentClientIdProvider,
slowSyncRepository = slowSyncRepository,
messageSender = messageSender,
messageSendFailureHandler = messageSendFailureHandler,
userPropertyRepository = userPropertyRepository,
selfDeleteTimer = observeSelfDeletingMessages,
scope = scope
)

val sendEditTextMessage: SendEditTextMessageUseCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ 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
import kotlinx.coroutines.withContext
import kotlin.time.Duration

@Suppress("LongParameterList")
Expand All @@ -55,15 +56,16 @@ class SendTextMessageUseCase internal constructor(
private val messageSendFailureHandler: MessageSendFailureHandler,
private val userPropertyRepository: UserPropertyRepository,
private val selfDeleteTimer: ObserveSelfDeletionTimerSettingsForConversationUseCase,
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl,
private val scope: CoroutineScope
) {

suspend operator fun invoke(
conversationId: ConversationId,
text: String,
mentions: List<MessageMention> = emptyList(),
quotedMessageId: String? = null
): Either<CoreFailure, Unit> = withContext(dispatchers.io) {
): Either<CoreFailure, Unit> = scope.async(dispatchers.io) {
slowSyncRepository.slowSyncStatus.first {
it is SlowSyncStatus.Complete
}
Expand Down Expand Up @@ -98,9 +100,18 @@ class SendTextMessageUseCase internal constructor(
expirationData = messageTimer?.let { Message.ExpirationData(it) },
isSelfMessage = true
)
persistMessage(message).flatMap { messageSender.sendMessage(message) }
}.onFailure { messageSendFailureHandler.handleFailureAndUpdateMessageStatus(it, conversationId, generatedMessageUuid, TYPE) }
}
persistMessage(message).flatMap {
messageSender.sendMessage(message)
}
}.onFailure {
messageSendFailureHandler.handleFailureAndUpdateMessageStatus(
failure = it,
conversationId = conversationId,
messageId = generatedMessageUuid,
messageType = TYPE
)
}
}.await()

companion object {
const val TYPE = "Text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import io.mockative.given
import io.mockative.mock
import io.mockative.once
import io.mockative.verify
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -53,7 +54,7 @@ class SendTextMessageCaseTest {
@Test
fun givenAValidMessage_whenSendingSomeText_thenShouldReturnASuccessResult() = runTest {
// Given
val (arrangement, sendTextMessage) = Arrangement()
val (arrangement, sendTextMessage) = Arrangement(this)
.withToggleReadReceiptsStatus()
.withCurrentClientProviderSuccess()
.withPersistMessageSuccess()
Expand Down Expand Up @@ -88,7 +89,7 @@ class SendTextMessageCaseTest {
@Test
fun givenNoNetwork_whenSendingSomeText_thenShouldReturnAFailure() = runTest {
// Given
val (arrangement, sendTextMessage) = Arrangement()
val (arrangement, sendTextMessage) = Arrangement(this)
.withToggleReadReceiptsStatus()
.withCurrentClientProviderSuccess()
.withPersistMessageSuccess()
Expand Down Expand Up @@ -120,7 +121,7 @@ class SendTextMessageCaseTest {
.wasInvoked(once)
}

private class Arrangement {
private class Arrangement(private val coroutineScope: CoroutineScope) {

@Mock
val persistMessage = mock(classOf<PersistMessageUseCase>())
Expand Down Expand Up @@ -196,7 +197,8 @@ class SendTextMessageCaseTest {
messageSender,
messageSendFailureHandler,
userPropertyRepository,
observeSelfDeletionTimerSettingsForConversation
observeSelfDeletionTimerSettingsForConversation,
scope = coroutineScope
)
}

Expand Down

0 comments on commit 17ec885

Please sign in to comment.