Skip to content

Commit

Permalink
Include screen sharing metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
m-zagorski committed Dec 5, 2024
1 parent 75cc576 commit 6ab6e3d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data class RecentlyEndedCallMetadata(
) {
data class CallDetails(
val isCallScreenShare: Boolean,
val screenShareDurationInSeconds: Long,
val callScreenShareUniques: Int,
val isOutgoingCall: Boolean,
val callDurationInSeconds: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
package com.wire.kalium.logic.feature.call.usecase

import com.wire.kalium.logic.data.call.Call
import com.wire.kalium.logic.data.call.CallMetadata
import com.wire.kalium.logic.data.call.CallRepository
import com.wire.kalium.logic.data.call.CallScreenSharingMetadata
import com.wire.kalium.logic.data.call.RecentlyEndedCallMetadata
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.SelfTeamIdProvider
Expand All @@ -42,19 +43,20 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl internal constructor(
private val selfTeamIdProvider: SelfTeamIdProvider,
) : CreateAndPersistRecentlyEndedCallMetadataUseCase {
override suspend fun invoke(conversationId: ConversationId, callEndedReason: Int) {
val call = callRepository.observeCurrentCall(conversationId).first()
call?.createMetadata(callEndedReason = callEndedReason)?.let { metadata ->
callRepository.getCallMetadataProfile()[conversationId]?.createMetadata(
conversationId = conversationId,
callEndedReason = callEndedReason
)?.let { metadata ->
callRepository.updateRecentlyEndedCallMetadata(metadata)
}
}

private suspend fun Call.createMetadata(callEndedReason: Int): RecentlyEndedCallMetadata {
val selfCallUser = participants.firstOrNull { participant -> participant.userType == UserType.OWNER }
private suspend fun CallMetadata.createMetadata(conversationId: ConversationId, callEndedReason: Int): RecentlyEndedCallMetadata {
val selfCallUser = getFullParticipants().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 = participants.count { participant -> participant.isSharingScreen }
val isOutgoingCall = callerId.value == selfCallUser?.id?.value
val callDurationInSeconds = establishedTime?.let {
DateTimeUtil.calculateMillisDifference(it, DateTimeUtil.currentIsoDateTimeString()) / MILLIS_IN_SECOND
Expand All @@ -64,7 +66,8 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl internal constructor(
callEndReason = callEndedReason,
callDetails = RecentlyEndedCallMetadata.CallDetails(
isCallScreenShare = selfCallUser?.isSharingScreen ?: false,
callScreenShareUniques = uniqueScreenShares,
screenShareDurationInSeconds = screenShareMetadata.totalDurationInSeconds(),
callScreenShareUniques = screenShareMetadata.uniqueSharingUsers.size,
isOutgoingCall = isOutgoingCall,
callDurationInSeconds = callDurationInSeconds,
callParticipantsCount = participants.size,
Expand All @@ -82,6 +85,14 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseImpl internal constructor(
)
}

private fun CallScreenSharingMetadata.totalDurationInSeconds(): Long {
val now = DateTimeUtil.currentInstant()
val activeScreenSharesDurationInSeconds =
activeScreenShares.values.sumOf { startTime -> DateTimeUtil.calculateMillisDifference(startTime, now) }

return (activeScreenSharesDurationInSeconds + completedScreenShareDurationInMillis) / MILLIS_IN_SECOND
}

private companion object {
const val MILLIS_IN_SECOND = 1_000L
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@
*/
package com.wire.kalium.logic.feature.call.usecase

import com.wire.kalium.logic.data.call.Call
import com.wire.kalium.logic.data.call.CallMetadata
import com.wire.kalium.logic.data.call.CallMetadataProfile
import com.wire.kalium.logic.data.call.CallRepository
import com.wire.kalium.logic.data.call.Participant
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.call.ParticipantMinimized
import com.wire.kalium.logic.data.call.RecentlyEndedCallMetadata
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.conversation.MemberDetails
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.SelfTeamIdProvider
import com.wire.kalium.logic.data.user.type.UserType
import com.wire.kalium.logic.feature.conversation.ObserveConversationMembersUseCase
import com.wire.kalium.logic.framework.TestCall
import com.wire.kalium.logic.framework.TestCall.CALLER_ID
import com.wire.kalium.logic.framework.TestUser
import com.wire.kalium.logic.framework.TestUser.OTHER_MINIMIZED
import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.any
Expand All @@ -55,7 +57,7 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {

// when
useCase(
conversationId = ConversationId(value = "value", domain = "domain"),
conversationId = CONVERSATION_ID,
callEndedReason = 2
)

Expand All @@ -76,7 +78,7 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {

// when
useCase(
conversationId = ConversationId(value = "value", domain = "domain"),
conversationId = CONVERSATION_ID,
callEndedReason = 2
)

Expand All @@ -103,7 +105,7 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {

// when
useCase(
conversationId = ConversationId(value = "value", domain = "domain"),
conversationId = CONVERSATION_ID,
callEndedReason = 2
)

Expand Down Expand Up @@ -131,7 +133,7 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {

// when
useCase(
conversationId = ConversationId(value = "value", domain = "domain"),
conversationId = CONVERSATION_ID,
callEndedReason = 2
)

Expand All @@ -157,14 +159,14 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {
@Mock
val callRepository = mock(CallRepository::class)

suspend fun withOutgoingCall() = apply {
coEvery { callRepository.observeCurrentCall(any()) }
.returns(flowOf(callWithOwner()))
fun withOutgoingCall() = apply {
every { callRepository.getCallMetadataProfile() }
.returns(CallMetadataProfile(mapOf(CONVERSATION_ID to callMetadata())))
}

suspend fun withIncomingCall() = apply {
coEvery { callRepository.observeCurrentCall(any()) }
.returns(flowOf(callWithOwner().copy(callerId = CALLER_ID.copy(value = "external"))))
fun withIncomingCall() = apply {
every { callRepository.getCallMetadataProfile() }
.returns(CallMetadataProfile(mapOf(CONVERSATION_ID to callMetadata().copy(callerId = CALLER_ID.copy(value = "external")))))
}

suspend fun withConversationMembers() = apply {
Expand Down Expand Up @@ -211,35 +213,55 @@ class CreateAndPersistRecentlyEndedCallMetadataUseCaseTest {
selfTeamIdProvider = selfTeamIdProvider
)

private fun callWithOwner(): Call {
return TestCall.oneOnOneEstablishedCall()
.copy(
callerId = CALLER_ID.copy(value = "ownerId"),
participants = TestCall.oneOnOneEstablishedCall().participants.plus(
Participant(
id = CALLER_ID.copy(value = "ownerId"),
clientId = "abcd",
isMuted = true,
isCameraOn = false,
isSharingScreen = false,
hasEstablishedAudio = true,
name = "User Name",
avatarAssetId = null,
userType = UserType.OWNER,
isSpeaking = false,
accentId = 0
)
private fun callMetadata(): CallMetadata {
return CallMetadata(
callerId = CALLER_ID.copy(value = "ownerId"),
isMuted = true,
isCameraOn = false,
isCbrEnabled = false,
conversationName = null,
users = listOf(
OTHER_MINIMIZED.copy(id = CALLER_ID.copy(value = "ownerId"), userType = UserType.OWNER),
OTHER_MINIMIZED
),
participants = listOf(
ParticipantMinimized(
id = CALLER_ID.copy(value = "ownerId"),
userId = CALLER_ID.copy(value = "ownerId"),
clientId = "abcd",
isMuted = true,
isCameraOn = false,
isSharingScreen = false,
hasEstablishedAudio = true
),
ParticipantMinimized(
id = CALLER_ID,
userId = CALLER_ID,
clientId = "abcd",
isMuted = true,
isCameraOn = false,
isSharingScreen = false,
hasEstablishedAudio = true
)
)
),
conversationType = Conversation.Type.ONE_ON_ONE,
callerName = "User Name",
callerTeamName = null,
callStatus = CallStatus.ESTABLISHED,
protocol = Conversation.ProtocolInfo.Proteus,
activeSpeakers = mapOf()
)
}
}

private companion object {
val CONVERSATION_ID = ConversationId(value = "value", domain = "domain")
val DEFAULT_ENDED_CALL_METADATA = RecentlyEndedCallMetadata(
callEndReason = 2,
isTeamMember = true,
callDetails = RecentlyEndedCallMetadata.CallDetails(
isCallScreenShare = false,
screenShareDurationInSeconds = 0L,
callScreenShareUniques = 0,
isOutgoingCall = true,
callDurationInSeconds = 0L,
Expand Down

0 comments on commit 6ab6e3d

Please sign in to comment.