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: Ask User for call quality feedback [WPB-9452] #2966

Merged
merged 5 commits into from
Aug 26, 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 @@ -144,6 +144,8 @@ interface UserConfigRepository {
suspend fun setPreviousTrackingIdentifier(identifier: String)
suspend fun getPreviousTrackingIdentifier(): String?
suspend fun deletePreviousTrackingIdentifier()
suspend fun updateNextTimeForCallFeedback(valueMs: Long)
suspend fun getNextTimeForCallFeedback(): Either<StorageFailure, Long>
}

@Suppress("TooManyFunctions")
Expand Down Expand Up @@ -538,4 +540,11 @@ internal class UserConfigDataSource internal constructor(
userConfigDAO.deletePreviousTrackingIdentifier()
}
}

override suspend fun updateNextTimeForCallFeedback(valueMs: Long) {
userConfigDAO.setNextTimeForCallFeedback(valueMs)
}

override suspend fun getNextTimeForCallFeedback() = wrapStorageRequest { userConfigDAO.getNextTimeForCallFeedback() }

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.wire.kalium.logic.feature.call.usecase.IsLastCallClosedUseCase
import com.wire.kalium.logic.feature.call.usecase.IsLastCallClosedUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.MuteCallUseCase
import com.wire.kalium.logic.feature.call.usecase.MuteCallUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveAskCallFeedbackUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEndCallDueToConversationDegradationUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEndCallDueToConversationDegradationUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
Expand All @@ -74,6 +75,8 @@ import com.wire.kalium.logic.feature.call.usecase.UpdateConversationClientsForCu
import com.wire.kalium.logic.feature.call.usecase.UpdateConversationClientsForCurrentCallUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.video.SetVideoSendStateUseCase
import com.wire.kalium.logic.feature.call.usecase.video.UpdateVideoStateUseCase
import com.wire.kalium.logic.feature.user.ShouldAskCallFeedbackUseCase
import com.wire.kalium.logic.feature.user.UpdateNextTimeCallFeedbackUseCase
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.sync.SyncManager
import com.wire.kalium.util.KaliumDispatcher
Expand Down Expand Up @@ -145,7 +148,13 @@ class CallsScope internal constructor(
kaliumConfigs
)

val endCall: EndCallUseCase get() = EndCallUseCaseImpl(callManager, callRepository, KaliumDispatcherImpl)
val endCall: EndCallUseCase
get() = EndCallUseCaseImpl(
callManager = callManager,
callRepository = callRepository,
endCallListener = EndCallResultListenerImpl,
shouldAskCallFeedback = shouldAskCallFeedback
)

val endCallOnConversationChange: EndCallOnConversationChangeUseCase
get() = EndCallOnConversationChangeUseCaseImpl(
Expand Down Expand Up @@ -198,6 +207,16 @@ class CallsScope internal constructor(

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

val observeEndCallDialog: ObserveEndCallDueToConversationDegradationUseCase
val observeEndCallDueToDegradationDialog: ObserveEndCallDueToConversationDegradationUseCase
get() = ObserveEndCallDueToConversationDegradationUseCaseImpl(EndCallResultListenerImpl)
val observeAskCallFeedbackUseCase: ObserveAskCallFeedbackUseCase
get() = ObserveAskCallFeedbackUseCase(EndCallResultListenerImpl)

private val shouldAskCallFeedback: ShouldAskCallFeedbackUseCase by lazy {
ShouldAskCallFeedbackUseCase(userConfigRepository)
}

val updateNextTimeCallFeedback: UpdateNextTimeCallFeedbackUseCase by lazy {
UpdateNextTimeCallFeedbackUseCase(userConfigRepository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal class EndCallOnConversationChangeUseCaseImpl(
}
.filter { it.shouldFinishCall() }
.map {
endCallListener.onCallEndedBecauseOfVerificationDegraded(conversationId)
endCallListener.onCallEndedBecauseOfVerificationDegraded()
conversationId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,34 @@
*/
package com.wire.kalium.logic.feature.call.usecase

import com.wire.kalium.logic.data.id.ConversationId
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow

interface EndCallResultListener {
suspend fun observeCallEndedBecauseOfVerificationDegraded(): Flow<ConversationId>
suspend fun onCallEndedBecauseOfVerificationDegraded(conversationId: ConversationId)
suspend fun observeCallEndedResult(): Flow<EndCallResult>
suspend fun onCallEndedBecauseOfVerificationDegraded()
suspend fun onCallEndedAskForFeedback(shouldAsk: Boolean)
}

/**
* This singleton allow us to queue event to show dialog informing user that call was ended because of verification degradation.
*/
object EndCallResultListenerImpl : EndCallResultListener {

private val conversationCallEnded = MutableSharedFlow<ConversationId>()
private val conversationCallEnded = MutableSharedFlow<EndCallResult>()

override suspend fun observeCallEndedBecauseOfVerificationDegraded(): Flow<ConversationId> = conversationCallEnded
override suspend fun observeCallEndedResult(): Flow<EndCallResult> = conversationCallEnded

override suspend fun onCallEndedBecauseOfVerificationDegraded(conversationId: ConversationId) {
conversationCallEnded.emit(conversationId)
override suspend fun onCallEndedBecauseOfVerificationDegraded() {
conversationCallEnded.emit(EndCallResult.VerificationDegraded)
}

override suspend fun onCallEndedAskForFeedback(shouldAsk: Boolean) {
conversationCallEnded.emit(EndCallResult.AskForFeedback(shouldAsk))
}
}

sealed class EndCallResult {
data object VerificationDegraded : EndCallResult()
data class AskForFeedback(val shouldAsk: Boolean) : EndCallResult()
Comment on lines +47 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.feature.call.CallManager
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.feature.user.ShouldAskCallFeedbackUseCase
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.flow.first
Expand All @@ -46,6 +47,8 @@ interface EndCallUseCase {
internal class EndCallUseCaseImpl(
private val callManager: Lazy<CallManager>,
private val callRepository: CallRepository,
private val endCallListener: EndCallResultListener,
private val shouldAskCallFeedback: ShouldAskCallFeedbackUseCase,
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl
) : EndCallUseCase {

Expand Down Expand Up @@ -73,5 +76,6 @@ internal class EndCallUseCaseImpl(

callManager.value.endCall(conversationId)
callRepository.updateIsCameraOnById(conversationId, false)
endCallListener.onCallEndedAskForFeedback(shouldAskCallFeedback())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map

/**
* The useCase for observing when the ongoing call was ended because of degradation of conversation verification status (Proteus or MLS)
*/
interface ObserveAskCallFeedbackUseCase {
/**
* @return [Flow] that emits only when the call was ended because of degradation of conversation verification status (Proteus or MLS)
*/
suspend operator fun invoke(): Flow<Boolean>
}

@Suppress("FunctionNaming")
internal fun ObserveAskCallFeedbackUseCase(
endCallListener: EndCallResultListener
) = object : ObserveAskCallFeedbackUseCase {
override suspend fun invoke(): Flow<Boolean> =
endCallListener.observeCallEndedResult()
.filterIsInstance(EndCallResult.AskForFeedback::class)
.map { it.shouldAsk }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.wire.kalium.logic.feature.call.usecase

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map

/**
Expand All @@ -34,5 +35,7 @@ internal class ObserveEndCallDueToConversationDegradationUseCaseImpl(
private val endCallListener: EndCallResultListener
) : ObserveEndCallDueToConversationDegradationUseCase {
override suspend fun invoke(): Flow<Unit> =
endCallListener.observeCallEndedBecauseOfVerificationDegraded().map { Unit }
endCallListener.observeCallEndedResult()
.filterIsInstance(EndCallResult.VerificationDegraded::class)
.map { Unit }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.user

import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.functional.getOrElse
import com.wire.kalium.logic.functional.map
import com.wire.kalium.util.DateTimeUtil
import kotlinx.datetime.Instant

/**
* Use case that returns [Boolean] if user should be asked for a feedback about call quality or not.
*/
interface ShouldAskCallFeedbackUseCase {
/**
* @return [Boolean] if user should be asked for a feedback about call quality or not.
*/
suspend operator fun invoke(): Boolean
}

@Suppress("FunctionNaming")
internal fun ShouldAskCallFeedbackUseCase(
userConfigRepository: UserConfigRepository
) = object : ShouldAskCallFeedbackUseCase {

override suspend fun invoke(): Boolean =
userConfigRepository.getNextTimeForCallFeedback().map {
it > 0L && DateTimeUtil.currentInstant() > Instant.fromEpochMilliseconds(it)
}.getOrElse(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the .getOrElse(true) will mean that the first call of a fresh logged in user will display the feedback right?


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.user

import com.wire.kalium.logic.configuration.UserConfigRepository
import com.wire.kalium.logic.feature.user.UpdateNextTimeCallFeedbackUseCase.Companion.askingForFeedbackPeriod
import com.wire.kalium.util.DateTimeUtil
import kotlin.time.Duration.Companion.days

/**
* Use case that updates next time when user should be asked for a feedback about call quality.
*/
interface UpdateNextTimeCallFeedbackUseCase {
/**
* Update next time when user should be asked for a feedback about call quality.
* @param neverAskAgain [Boolean] if user checked "never ask me again"
*/
suspend operator fun invoke(neverAskAgain: Boolean)

companion object {
val askingForFeedbackPeriod = 3.days
}
}

@Suppress("FunctionNaming")
internal fun UpdateNextTimeCallFeedbackUseCase(
userConfigRepository: UserConfigRepository
) = object : UpdateNextTimeCallFeedbackUseCase {

override suspend fun invoke(neverAskAgain: Boolean) {
val nextTimestamp = if (neverAskAgain) -1L
else DateTimeUtil.currentInstant().plus(askingForFeedbackPeriod).toEpochMilliseconds()

userConfigRepository.updateNextTimeForCallFeedback(nextTimestamp)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class EndCallOnConversationChangeUseCaseTest {
endCall.invoke(eq(conversationId))
}.returns(Unit)
coEvery {
endCallDialogManager.onCallEndedBecauseOfVerificationDegraded(eq(conversationId))
endCallDialogManager.onCallEndedBecauseOfVerificationDegraded()
}.returns(Unit)

withEstablishedCallsFlow(listOf(call))
Expand Down
Loading
Loading