Skip to content

Commit

Permalink
chore: observe only current call
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Sep 13, 2024
1 parent 27532f3 commit cd47592
Show file tree
Hide file tree
Showing 26 changed files with 171 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.wire.kalium.logic.data.call

import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedID

enum class CallStatus {
STARTED,
Expand All @@ -39,7 +40,7 @@ data class Call(
val isMuted: Boolean,
val isCameraOn: Boolean,
val isCbrEnabled: Boolean,
val callerId: String,
val callerId: QualifiedID,
val conversationName: String?,
val conversationType: Conversation.Type,
val callerName: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.wire.kalium.logic.data.call

import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.user.OtherUserMinimized
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.type.UserType
Expand All @@ -31,6 +32,7 @@ data class CallMetadataProfile(
}

data class CallMetadata(
val callerId: QualifiedID,
val isMuted: Boolean,
val isCameraOn: Boolean,
val isCbrEnabled: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ app-compat = "1.6.1"
android-paging3 = "3.2.1"
cli-kt = "3.5.0"
coroutines = "1.8.0"
compose-compiler = "1.5.11"
compose-compiler = "1.5.14"
compose-ui = "1.6.6"
compose-material = "1.6.6"
cryptobox4j = "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class CallManagerImpl internal constructor(
isMuted = false,
isCameraOn = isCameraOn,
isCbrEnabled = isAudioCbr,
callerId = userId.await().toString()
callerId = userId.await()
)

withCalling {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class OnIncomingCall(
callRepository.createCall(
conversationId = qualifiedConversationId,
status = status,
callerId = qualifiedIdMapper.fromStringToQualifiedID(userId).toString(),
callerId = qualifiedIdMapper.fromStringToQualifiedID(userId),
isMuted = isMuted,
isCameraOn = false,
type = mappedConversationType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flattenConcat
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
Expand Down Expand Up @@ -108,7 +109,7 @@ interface CallRepository {
conversationId: ConversationId,
type: ConversationTypeForCall,
status: CallStatus,
callerId: String,
callerId: UserId,
isMuted: Boolean,
isCameraOn: Boolean,
isCbrEnabled: Boolean
Expand All @@ -133,6 +134,7 @@ interface CallRepository {
suspend fun observeEpochInfo(conversationId: ConversationId): Either<CoreFailure, Flow<EpochInfo>>
suspend fun advanceEpoch(conversationId: ConversationId)
fun currentCallProtocol(conversationId: ConversationId): Conversation.ProtocolInfo?
suspend fun observeCurrentCall(conversationId: ConversationId): Flow<Call?>
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -162,6 +164,26 @@ internal class CallDataSource(
private val callJobs = ConcurrentMutableMap<ConversationId, Job>()
private val staleParticipantJobs = ConcurrentMutableMap<QualifiedClientID, Job>()

override suspend fun observeCurrentCall(conversationId: ConversationId): Flow<Call?> = _callMetadataProfile.map {
it[conversationId]?.let { currentCall ->
Call(
conversationId = conversationId,
status = currentCall.callStatus,
isMuted = currentCall.isMuted,
isCameraOn = currentCall.isCameraOn,
isCbrEnabled = currentCall.isCbrEnabled,
callerId = currentCall.callerId,
conversationName = currentCall.conversationName,
conversationType = currentCall.conversationType,
callerName = currentCall.callerName,
callerTeamName = currentCall.callerTeamName,
establishedTime = currentCall.establishedTime,
participants = currentCall.getFullParticipants(),
maxParticipants = currentCall.maxParticipants
)
}
}

override suspend fun getCallConfigResponse(limit: Int?): Either<CoreFailure, String> = wrapApiRequest {
callApi.getCallConfig(limit = limit)
}
Expand Down Expand Up @@ -191,18 +213,15 @@ internal class CallDataSource(
conversationId: ConversationId,
type: ConversationTypeForCall,
status: CallStatus,
callerId: String,
callerId: UserId,
isMuted: Boolean,
isCameraOn: Boolean,
isCbrEnabled: Boolean
) {
val conversation: ConversationDetails =
conversationRepository.observeConversationDetailsById(conversationId).onlyRight().first()

// in OnIncomingCall we get callerId without a domain,
// to cover that case and have a valid UserId we have that workaround
val callerIdWithDomain = qualifiedIdMapper.fromStringToQualifiedID(callerId)
val caller = userRepository.getKnownUser(callerIdWithDomain).first()
val caller = userRepository.getKnownUser(callerId).first()
val team = caller?.teamId?.let { teamId -> teamRepository.getTeam(teamId).first() }

val callEntity = callMapper.toCallEntity(
Expand All @@ -211,10 +230,11 @@ internal class CallDataSource(
type = type,
status = status,
conversationType = conversation.conversation.type,
callerId = callerIdWithDomain
callerId = callerId
)

val metadata = CallMetadata(
callerId = callerId,
conversationName = conversation.conversation.name,
conversationType = conversation.conversation.type,
callerName = caller?.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class CallMapperImpl(
isMuted = metadata?.isMuted ?: true,
isCameraOn = metadata?.isCameraOn ?: false,
isCbrEnabled = metadata?.isCbrEnabled ?: false,
callerId = callEntity.callerId,
callerId = metadata?.callerId ?: qualifiedIdMapper.fromStringToQualifiedID(callEntity.callerId),
conversationName = metadata?.conversationName,
conversationType = toConversationType(conversationType = callEntity.conversationType),
callerName = metadata?.callerName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ 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.ObserveEstablishedCallWithSortedParticipantsUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallWithSortedParticipantsUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveOngoingCallsUseCase
Expand Down Expand Up @@ -129,6 +131,12 @@ class CallsScope internal constructor(
callRepository = callRepository,
)

val observeEstablishedCallWithSortedParticipants: ObserveEstablishedCallWithSortedParticipantsUseCase
get() = ObserveEstablishedCallWithSortedParticipantsUseCaseImpl(
callRepository = callRepository,
callingParticipantsOrder = callingParticipantsOrder
)

val startCall: StartCallUseCase
get() = StartCallUseCase(
callManager = callManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.data.call.Call
import com.wire.kalium.logic.data.call.CallRepository
import com.wire.kalium.logic.data.call.CallingParticipantsOrder
import com.wire.kalium.logic.data.id.ConversationId
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map

/**
* Use case to observe established call with the participants sorted according to the [CallingParticipantsOrder]
*/
interface ObserveEstablishedCallWithSortedParticipantsUseCase {
suspend operator fun invoke(conversationId: ConversationId): Flow<Call?>
}

class ObserveEstablishedCallWithSortedParticipantsUseCaseImpl internal constructor(
private val callRepository: CallRepository,
private val callingParticipantsOrder: CallingParticipantsOrder
) : ObserveEstablishedCallWithSortedParticipantsUseCase {

override suspend operator fun invoke(conversationId: ConversationId): Flow<Call?> {
return callRepository.observeCurrentCall(conversationId)
.distinctUntilChanged()
.map { call ->
call?.let {
val sortedParticipants = callingParticipantsOrder.reorderItems(call.participants)
call.copy(participants = sortedParticipants)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.STARTED,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -242,7 +242,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.STARTED,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -288,7 +288,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -343,7 +343,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -392,7 +392,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -427,7 +427,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.STARTED,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -466,7 +466,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.STARTED,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -504,7 +504,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -550,7 +550,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -587,7 +587,7 @@ class CallRepositoryTest {
callRepository.createCall(
conversationId = Arrangement.conversationId,
status = CallStatus.INCOMING,
callerId = callerId.value,
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down Expand Up @@ -1477,7 +1477,7 @@ class CallRepositoryTest {
private fun provideCall(id: ConversationId, status: CallStatus) = Call(
conversationId = id,
status = status,
callerId = "callerId@domain",
callerId = UserId("callerId", "domain"),
participants = listOf(),
isMuted = false,
isCameraOn = false,
Expand All @@ -1502,6 +1502,7 @@ class CallRepositoryTest {
)

private fun createCallMetadata() = CallMetadata(
callerId = callerId,
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
Expand Down
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.featureFlags.KaliumConfigs
import com.wire.kalium.logic.framework.TestCall
import com.wire.kalium.logic.test_util.TestKaliumDispatcher
import com.wire.kalium.logic.test_util.testKaliumDispatcher
import io.mockative.Mock
Expand Down Expand Up @@ -170,7 +171,7 @@ class AnswerCallUseCaseTest {
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
callerId = "id",
callerId = TestCall.CALLER_ID,
conversationName = "caller-name",
conversationType = Conversation.Type.GROUP,
callerName = "Name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class EndCallOnConversationChangeUseCaseTest {
private val call = Call(
conversationId = conversationId,
status = CallStatus.ESTABLISHED,
callerId = "called-id",
callerId = UserId("called-id", "domain"),
isMuted = false,
isCameraOn = false,
isCbrEnabled = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.wire.kalium.logic.data.call.Call
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.user.ShouldAskCallFeedbackUseCase
import com.wire.kalium.logic.test_util.TestKaliumDispatcher
import com.wire.kalium.logic.util.arrangement.repository.CallManagerArrangement
Expand Down Expand Up @@ -233,7 +234,7 @@ class EndCallUseCaseTest {
private val call = Call(
conversationId = conversationId,
status = CallStatus.ESTABLISHED,
callerId = "called-id",
callerId = UserId("called-id", "domain"),
isMuted = false,
isCameraOn = false,
isCbrEnabled = false,
Expand Down
Loading

0 comments on commit cd47592

Please sign in to comment.