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: Recently ended call metadata compact [WPB-14256] #3113

Merged
Show file tree
Hide file tree
Changes from 4 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.network.NetworkStateObserver
Expand All @@ -56,7 +57,8 @@ actual class GlobalCallManager {
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
getCallConversationType: GetCallConversationTypeProvider,
networkStateObserver: NetworkStateObserver,
kaliumConfigs: KaliumConfigs
kaliumConfigs: KaliumConfigs,
createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase
): CallManager {
return CallManagerImpl()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import com.wire.kalium.logic.feature.call.scenario.OnSFTRequest
import com.wire.kalium.logic.feature.call.scenario.OnSendOTR
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.functional.fold
Expand Down Expand Up @@ -119,6 +120,7 @@ class CallManagerImpl internal constructor(
private val kaliumConfigs: KaliumConfigs,
private val mediaManagerService: MediaManagerService,
private val flowManagerService: FlowManagerService,
private val createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase,
private val json: Json = Json { ignoreUnknownKeys = true },
private val shouldRemoteMuteChecker: ShouldRemoteMuteChecker = ShouldRemoteMuteCheckerImpl(),
private val serverTimeHandler: ServerTimeHandler = ServerTimeHandlerImpl(),
Expand Down Expand Up @@ -219,7 +221,8 @@ class CallManagerImpl internal constructor(
callRepository = callRepository,
networkStateObserver = networkStateObserver,
scope = scope,
qualifiedIdMapper = qualifiedIdMapper
qualifiedIdMapper = qualifiedIdMapper,
createAndPersistRecentlyEndedCallMetadata = createAndPersistRecentlyEndedCallMetadata
).keepingStrongReference(),
metricsHandler = metricsHandler,
callConfigRequestHandler = OnConfigRequest(calling, callRepository, scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.util.CurrentPlatform
Expand Down Expand Up @@ -94,7 +95,8 @@ actual class GlobalCallManager(
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
getCallConversationType: GetCallConversationTypeProvider,
networkStateObserver: NetworkStateObserver,
kaliumConfigs: KaliumConfigs
kaliumConfigs: KaliumConfigs,
createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase
): CallManager {
if (kaliumConfigs.enableCalling) {
return callManagerHolder.computeIfAbsent(userId) {
Expand All @@ -116,7 +118,8 @@ actual class GlobalCallManager(
mediaManagerService = mediaManager,
flowManagerService = flowManager,
userConfigRepository = userConfigRepository,
kaliumConfigs = kaliumConfigs
kaliumConfigs = kaliumConfigs,
createAndPersistRecentlyEndedCallMetadata = createAndPersistRecentlyEndedCallMetadata
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ 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.id.QualifiedIdMapper
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.network.NetworkState
import com.wire.kalium.network.NetworkStateObserver
import kotlinx.coroutines.CoroutineScope
Expand All @@ -41,7 +42,8 @@ class OnCloseCall(
private val callRepository: CallRepository,
private val scope: CoroutineScope,
private val qualifiedIdMapper: QualifiedIdMapper,
private val networkStateObserver: NetworkStateObserver
private val networkStateObserver: NetworkStateObserver,
private val createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase
) : CloseCallHandler {
override fun onClosedCall(
reason: Int,
Expand Down Expand Up @@ -79,19 +81,15 @@ class OnCloseCall(
status = callStatus
)

callMetadata?.let {
callRepository.updateRecentlyEndedCall(
conversationId = conversationIdWithDomain,
callMetadata = callMetadata,
callEndReason = reason
)
}

if (callMetadata?.protocol is Conversation.ProtocolInfo.MLS) {
callRepository.leaveMlsConference(conversationIdWithDomain)
}
callingLogger.i("[OnCloseCall] -> ConversationId: ${conversationId.obfuscateId()} | callStatus: $callStatus")
}

scope.launch {
createAndPersistRecentlyEndedCallMetadata(conversationIdWithDomain, reason)
}
}

private fun shouldPersistMissedCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ interface CallRepository {
fun currentCallProtocol(conversationId: ConversationId): Conversation.ProtocolInfo?
suspend fun observeCurrentCall(conversationId: ConversationId): Flow<Call?>

suspend fun updateRecentlyEndedCall(conversationId: ConversationId, callMetadata: CallMetadata, callEndReason: Int)
suspend fun observeRecentlyEndedCall(): Flow<RecentlyEndedCall>
suspend fun updateRecentlyEndedCallMetadata(recentlyEndedCallMetadata: RecentlyEndedCallMetadata)
suspend fun observeRecentlyEndedCallMetadata(): Flow<RecentlyEndedCallMetadata>
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -167,25 +167,16 @@ internal class CallDataSource(
private val scope = CoroutineScope(job + kaliumDispatchers.io)
private val callJobs = ConcurrentMutableMap<ConversationId, Job>()
private val staleParticipantJobs = ConcurrentMutableMap<QualifiedClientID, Job>()
private val _recentlyEndedCallFlow = MutableSharedFlow<RecentlyEndedCall>(
private val _recentlyEndedCallFlow = MutableSharedFlow<RecentlyEndedCallMetadata>(
extraBufferCapacity = 1
)

override suspend fun observeCurrentCall(conversationId: ConversationId): Flow<Call?> = _callMetadataProfile.map {
it[conversationId]?.mapCallMetadataToCall(conversationId)
}

override suspend fun updateRecentlyEndedCall(
conversationId: ConversationId,
callMetadata: CallMetadata,
callEndReason: Int
) {
_recentlyEndedCallFlow.tryEmit(
RecentlyEndedCall(
call = callMetadata.mapCallMetadataToCall(conversationId),
callEndedCause = callEndReason
)
)
override suspend fun updateRecentlyEndedCallMetadata(recentlyEndedCallMetadata: RecentlyEndedCallMetadata) {
_recentlyEndedCallFlow.tryEmit(recentlyEndedCallMetadata)
}

private fun CallMetadata.mapCallMetadataToCall(conversationId: ConversationId): Call {
Expand All @@ -206,7 +197,7 @@ internal class CallDataSource(
)
}

override suspend fun observeRecentlyEndedCall(): Flow<RecentlyEndedCall> {
override suspend fun observeRecentlyEndedCallMetadata(): Flow<RecentlyEndedCallMetadata> {
return _recentlyEndedCallFlow
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdat
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdaterImpl
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProviderImpl
import com.wire.kalium.logic.feature.call.usecase.GetRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.call.usecase.GetRecentlyEndedCallMetadataUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.UpdateConversationClientsForCurrentCallUseCase
import com.wire.kalium.logic.feature.call.usecase.UpdateConversationClientsForCurrentCallUseCaseImpl
import com.wire.kalium.logic.feature.client.ClientScope
Expand Down Expand Up @@ -1265,7 +1265,8 @@ class UserSessionScope internal constructor(
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
getCallConversationType = getCallConversationType,
networkStateObserver = networkStateObserver,
kaliumConfigs = kaliumConfigs
kaliumConfigs = kaliumConfigs,
createAndPersistRecentlyEndedCallMetadata = createAndPersistRecentlyEndedCallMetadata
)
}

Expand Down Expand Up @@ -2093,8 +2094,9 @@ class UserSessionScope internal constructor(
userScopedLogger
)

val getRecentlyEndedCallMetadata: GetRecentlyEndedCallMetadataUseCase
get() = GetRecentlyEndedCallMetadataUseCaseImpl(
private val createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase
get() = CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl(
callRepository = callRepository,
observeConversationMembers = conversations.observeConversationMembers,
getSelf = users.getSelfUser
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ import com.wire.kalium.logic.feature.call.usecase.ObserveOngoingCallsUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveOngoingCallsUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveOutgoingCallUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveOutgoingCallUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveRecentlyEndedCallUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveRecentlyEndedCallUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveRecentlyEndedCallMetadataUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveSpeakerUseCase
import com.wire.kalium.logic.feature.call.usecase.RejectCallUseCase
import com.wire.kalium.logic.feature.call.usecase.RequestVideoStreamsUseCase
Expand Down Expand Up @@ -230,8 +230,8 @@ class CallsScope internal constructor(
UpdateNextTimeCallFeedbackUseCase(userConfigRepository)
}

val observeRecentlyEndedCall: ObserveRecentlyEndedCallUseCase
get() = ObserveRecentlyEndedCallUseCaseImpl(
val observeRecentlyEndedCallMetadata: ObserveRecentlyEndedCallMetadataUseCase
get() = ObserveRecentlyEndedCallMetadataUseCaseImpl(
callRepository = callRepository
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.feature.call.usecase.ConversationClientsInCallUpdater
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
import com.wire.kalium.logic.feature.call.usecase.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.message.MessageSender
import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.network.NetworkStateObserver
Expand All @@ -57,7 +58,8 @@ expect class GlobalCallManager {
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
getCallConversationType: GetCallConversationTypeProvider,
networkStateObserver: NetworkStateObserver,
kaliumConfigs: KaliumConfigs
kaliumConfigs: KaliumConfigs,
createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase
): CallManager

suspend fun removeInMemoryCallingManagerForUser(userId: UserId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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.RecentlyEndedCallMetadata
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.feature.conversation.ObserveConversationMembersUseCase
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
Expand All @@ -30,24 +32,32 @@ import kotlinx.coroutines.flow.firstOrNull
* Given a call and raw call end reason create metadata containing all information regarding
* a call.
*/
interface GetRecentlyEndedCallMetadataUseCase {
suspend operator fun invoke(call: Call, callEndedReason: Int): RecentlyEndedCallMetadata
interface CreateAndPersistRecentlyEndedCallMetadataUseCase {
suspend operator fun invoke(conversationId: ConversationId, callEndedReason: Int)
}

class GetRecentlyEndedCallMetadataUseCaseImpl(
class CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl internal constructor(
private val callRepository: CallRepository,
private val observeConversationMembers: ObserveConversationMembersUseCase,
private val getSelf: GetSelfUserUseCase,
) : GetRecentlyEndedCallMetadataUseCase {
override suspend fun invoke(call: Call, callEndedReason: Int): RecentlyEndedCallMetadata {
) : CreateAndPersistRecentlyEndedCallMetadataUseCase {
override suspend fun invoke(conversationId: ConversationId, callEndedReason: Int) {
val call = callRepository.observeCurrentCall(conversationId).first()
call?.createMetadata(callEndedReason = callEndedReason)?.let { metadata ->
callRepository.updateRecentlyEndedCallMetadata(metadata)
}
}

private suspend fun Call.createMetadata(callEndedReason: Int): RecentlyEndedCallMetadata {
val selfUser = getSelf().firstOrNull()
val selfCallUser = call.participants.firstOrNull { participant -> participant.userType == UserType.OWNER }
val conversationMembers = observeConversationMembers(call.conversationId).first()
val selfCallUser = participants.firstOrNull { participant -> participant.userType == UserType.OWNER }
val conversationMembers = observeConversationMembers(conversationId).first()
val conversationServicesCount = conversationMembers.count { member -> member.user.userType == UserType.SERVICE }
val guestsCount = conversationMembers.count { member -> member.user.userType == UserType.GUEST }
val guestsProCount = conversationMembers.count { member -> member.user.userType == UserType.GUEST && member.user.teamId != null }
val uniqueScreenShares = call.participants.count { participant -> participant.isSharingScreen }
val isOutgoingCall = call.callerId.value == selfCallUser?.id?.value
val callDurationInSeconds = call.establishedTime?.let {
val uniqueScreenShares = participants.count { participant -> participant.isSharingScreen }
val isOutgoingCall = callerId.value == selfCallUser?.id?.value
val callDurationInSeconds = establishedTime?.let {
DateTimeUtil.calculateMillisDifference(it, DateTimeUtil.currentIsoDateTimeString()) / MILLIS_IN_SECOND
} ?: 0L

Expand All @@ -58,13 +68,13 @@ class GetRecentlyEndedCallMetadataUseCaseImpl(
callScreenShareUniques = uniqueScreenShares,
isOutgoingCall = isOutgoingCall,
callDurationInSeconds = callDurationInSeconds,
callParticipantsCount = call.participants.size,
callParticipantsCount = participants.size,
conversationServices = conversationServicesCount,
callAVSwitchToggle = selfCallUser?.isCameraOn ?: false,
callVideoEnabled = call.isCameraOn
callVideoEnabled = isCameraOn
),
conversationDetails = RecentlyEndedCallMetadata.ConversationDetails(
conversationType = call.conversationType,
conversationType = conversationType,
conversationSize = conversationMembers.size,
conversationGuests = guestsCount,
conversationGuestsPro = guestsProCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
package com.wire.kalium.logic.feature.call.usecase

import com.wire.kalium.logic.data.call.CallRepository
import com.wire.kalium.logic.data.call.RecentlyEndedCall
import com.wire.kalium.logic.data.call.RecentlyEndedCallMetadata
import kotlinx.coroutines.flow.Flow
import com.wire.kalium.logic.data.id.ConversationId

/**
* Use case to observe recently ended call. This gives us the call that has been ended and also
* raw cause of the end.
* Use case to observe recently ended call metadata. This gives us all metadata assigned to a call.
* Used mainly for analytics.
*/
interface ObserveRecentlyEndedCallUseCase {
suspend operator fun invoke(conversationId: ConversationId): Flow<RecentlyEndedCall>
interface ObserveRecentlyEndedCallMetadataUseCase {
suspend operator fun invoke(conversationId: ConversationId): Flow<RecentlyEndedCallMetadata>
}

class ObserveRecentlyEndedCallUseCaseImpl internal constructor(
class ObserveRecentlyEndedCallMetadataUseCaseImpl internal constructor(
private val callRepository: CallRepository,
) : ObserveRecentlyEndedCallUseCase {
override suspend fun invoke(conversationId: ConversationId): Flow<RecentlyEndedCall> {
return callRepository.observeRecentlyEndedCall()
) : ObserveRecentlyEndedCallMetadataUseCase {
override suspend fun invoke(conversationId: ConversationId): Flow<RecentlyEndedCallMetadata> {
return callRepository.observeRecentlyEndedCallMetadata()
}
}
Loading