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

chore: observe only current call [WPB-3959] #3011

Merged
merged 6 commits into from
Sep 16, 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 @@ -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
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[versions]
kotlin = "1.9.23"
ksp = "1.9.23-1.0.20"
activity-compose = "1.9.0"
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.13"
compose-ui = "1.6.6"
compose-material = "1.6.6"
cryptobox4j = "1.4.0"
Expand Down Expand Up @@ -79,7 +80,7 @@ kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
ksp = { id = "com.google.devtools.ksp", version = "1.9.23-1.0.20" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
carthage = { id = "com.wire.carthage-gradle-plugin", version.ref = "carthage" }
sqldelight = { id = "app.cash.sqldelight", version.ref = "sqldelight" }
protobuf = { id = "com.google.protobuf", version.ref = "protobufCodegen" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
*/
package com.wire.kalium.logic.feature.call.scenario

import com.wire.kalium.calling.types.Uint32_t
import com.wire.kalium.calling.ConversationTypeCalling
import com.wire.kalium.calling.types.Uint32_t
import com.wire.kalium.logic.data.call.CallRepository
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.call.ConversationTypeForCall
import com.wire.kalium.logic.data.call.mapper.CallMapperImpl
import com.wire.kalium.logic.data.id.QualifiedIdMapperImpl
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.framework.TestClient
import com.wire.kalium.logic.framework.TestConversation
Expand Down Expand Up @@ -66,7 +66,7 @@ class OnIncomingCallTest {
eq(TestConversation.CONVERSATION.id),
eq(ConversationTypeForCall.Conference),
eq(CallStatus.INCOMING),
eq(TestUser.USER_ID.toString()),
eq(TestUser.USER_ID),
eq(true),
eq(false),
eq(false)
Expand Down Expand Up @@ -96,7 +96,7 @@ class OnIncomingCallTest {
eq(TestConversation.CONVERSATION.id),
eq(ConversationTypeForCall.Conference),
eq(CallStatus.STILL_ONGOING),
eq(TestUser.USER_ID.toString()),
eq(TestUser.USER_ID),
eq(true),
eq(false),
eq(false)
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
Loading
Loading