Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create use case for calling activated status #WPB-1826 #3087

Merged
merged 8 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ interface UserConfigRepository {
suspend fun getSupportedProtocols(): Either<StorageFailure, Set<SupportedProtocol>>
fun setConferenceCallingEnabled(enabled: Boolean): Either<StorageFailure, Unit>
fun isConferenceCallingEnabled(): Either<StorageFailure, Boolean>
fun observeConferenceCallingEnabled(): Flow<Either<StorageFailure, Boolean>>
fun setUseSFTForOneOnOneCalls(shouldUse: Boolean): Either<StorageFailure, Unit>
fun shouldUseSFTForOneOnOneCalls(): Either<StorageFailure, Boolean>
fun setSecondFactorPasswordChallengeStatus(isRequired: Boolean): Either<StorageFailure, Unit>
Expand Down Expand Up @@ -302,6 +303,9 @@ internal class UserConfigDataSource internal constructor(
userConfigStorage.isConferenceCallingEnabled()
}

override fun observeConferenceCallingEnabled(): Flow<Either<StorageFailure, Boolean>> =
userConfigStorage.isConferenceCallingEnabledFlow().wrapStorageRequest()

override fun setUseSFTForOneOnOneCalls(shouldUse: Boolean): Either<StorageFailure, Unit> = wrapStorageRequest {
userConfigStorage.persistUseSftForOneOnOneCalls(shouldUse)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import com.wire.kalium.logic.feature.call.usecase.GetAllCallsWithSortedParticipa
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.GetIncomingCallsUseCase
import com.wire.kalium.logic.feature.call.usecase.GetIncomingCallsUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveConferenceCallingEnabledUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveConferenceCallingEnabledUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.IsCallRunningUseCase
import com.wire.kalium.logic.feature.call.usecase.IsEligibleToStartCallUseCase
import com.wire.kalium.logic.feature.call.usecase.IsEligibleToStartCallUseCaseImpl
Expand Down Expand Up @@ -215,6 +217,9 @@ class CallsScope internal constructor(

val isEligibleToStartCall: IsEligibleToStartCallUseCase get() = IsEligibleToStartCallUseCaseImpl(userConfigRepository, callRepository)

val observeConferenceCallingEnabled: ObserveConferenceCallingEnabledUseCase
get() = ObserveConferenceCallingEnabledUseCaseImpl(userConfigRepository)

val observeEndCallDueToDegradationDialog: ObserveEndCallDueToConversationDegradationUseCase
get() = ObserveEndCallDueToConversationDegradationUseCaseImpl(EndCallResultListenerImpl)
val observeAskCallFeedbackUseCase: ObserveAskCallFeedbackUseCase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.call.usecase

import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.functional.fold
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.zip

/**
* Returns [Unit] only when our conference calling setting changes from false to true, meaning our conference calling
* capability has been enabled. Internally we rely on getting event [Event.FeatureConfig.ConferenceCallingUpdated].
* This can be used to inform user about the change, for example displaying a dialog about upgrading to enterprise edition.
*/
interface ObserveConferenceCallingEnabledUseCase {
suspend operator fun invoke(): Flow<Unit>
}

internal class ObserveConferenceCallingEnabledUseCaseImpl(
private val userConfigRepository: UserConfigRepository,
) : ObserveConferenceCallingEnabledUseCase {
override suspend fun invoke(): Flow<Unit> {
val enabledFlow = userConfigRepository.observeConferenceCallingEnabled()
.map { isEnabled -> isEnabled.fold({ false }, { it }) }
return enabledFlow
.zip(enabledFlow.drop(1)) { old, new -> old to new }
m-zagorski marked this conversation as resolved.
Show resolved Hide resolved
.filter { (old, new) -> !old && new }
.map { Unit }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.call.usecase

import app.cash.turbine.test
import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.every
import io.mockative.mock
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.test.runTest
import kotlin.test.Test

class ObserveConferenceCallingEnabledUseCaseTest {

@Test
fun givenOnlyDefaultConferenceCallingValue_whenNewValueIsNotPresent_thenDoNotReturnAnything() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(false))
.arrange()

// when then
useCase().test {
awaitComplete()
}
}

@Test
fun givenDefaultConferenceCallingValueIsTrue_whenNewValueIsAlsoTrue_thenDoNotReturnAnything() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(true, true))
.arrange()

// when then
useCase().test {
awaitComplete()
}
}

@Test
fun givenDefaultConferenceCallingValueIsTrue_whenNewValueIsFalse_thenDoNotReturnAnything() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(true, false))
.arrange()

// when then
useCase().test {
awaitComplete()
}
}

@Test
fun givenDefaultConferenceCallingValueIsFalse_whenNewValueIsTrue_thenReturnResult() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(false, true))
.arrange()

// when then
useCase().test {
awaitItem()
awaitComplete()
}
}

@Test
fun givenDefaultConferenceCallingValueIsFalse_whenTwoNewValuesOfTrue_thenReturnOnlyOneResult() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(false, true, true))
.arrange()

// when then
useCase().test {
awaitItem()
awaitComplete()
}
}

@Test
fun givenDefaultConferenceCallingValueIsFalse_whenThreeNewValuesAreTrueFalseTrue_thenReturnTwoResults() = runTest {
// given
val (_, useCase) = Arrangement()
.withDefaultValue(listOf(false, true, false, true))
.arrange()

// when then
useCase().test {
awaitItem()
awaitItem()
awaitComplete()
}
}

private class Arrangement {
@Mock
val userConfigRepository = mock(UserConfigRepository::class)

fun withDefaultValue(values: List<Boolean>) = apply {
every {
userConfigRepository.observeConferenceCallingEnabled()
}.returns(values.map { Either.Right(it) }.asFlow())
}

fun arrange(): Pair<Arrangement, ObserveConferenceCallingEnabledUseCase> =
this to ObserveConferenceCallingEnabledUseCaseImpl(userConfigRepository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ interface UserConfigStorage {
*/
fun isConferenceCallingEnabled(): Boolean

/**
* Get a flow of saved flag to know if conference calling is enabled or not
*/
fun isConferenceCallingEnabledFlow(): Flow<Boolean>

fun persistUseSftForOneOnOneCalls(shouldUse: Boolean)

fun shouldUseSftForOneOnOneCalls(): Boolean
Expand Down Expand Up @@ -325,6 +330,12 @@ class UserConfigStorageImpl(
onBufferOverflow = BufferOverflow.DROP_OLDEST
)

private val conferenceCallingEnabledFlow =
MutableSharedFlow<Unit>(
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)

private val legalHoldRequestFlow =
MutableSharedFlow<Unit>(
extraBufferCapacity = 1,
Expand Down Expand Up @@ -497,6 +508,7 @@ class UserConfigStorageImpl(

override fun persistConferenceCalling(enabled: Boolean) {
kaliumPreferences.putBoolean(ENABLE_CONFERENCE_CALLING, enabled)
conferenceCallingEnabledFlow.tryEmit(Unit)
}

override fun isConferenceCallingEnabled(): Boolean =
Expand All @@ -505,6 +517,10 @@ class UserConfigStorageImpl(
DEFAULT_CONFERENCE_CALLING_ENABLED_VALUE
)

override fun isConferenceCallingEnabledFlow(): Flow<Boolean> = conferenceCallingEnabledFlow
.map { isConferenceCallingEnabled() }
.onStart { emit(isConferenceCallingEnabled()) }

override fun persistUseSftForOneOnOneCalls(shouldUse: Boolean) {
kaliumPreferences.putBoolean(USE_SFT_FOR_ONE_ON_ONE_CALLS, shouldUse)
}
Expand Down
Loading