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

refactor: remove the fetch of self user when only the ID is used [WPB-3726] #3196

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
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 @@ -34,7 +34,6 @@ import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
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.CreateAndPersistRecentlyEndedCallMetadataUseCase
import com.wire.kalium.logic.feature.call.usecase.GetCallConversationTypeProvider
Expand All @@ -59,11 +58,10 @@ class CallManagerTest {
@Mock
private val calling = mock(Calling::class)

@Mock
private val callRepository = mock(CallRepository::class)
private val selfUserId = UserId(value = "selfUserId", domain = "selfDomain")

@Mock
private val userRepository = mock(UserRepository::class)
private val callRepository = mock(CallRepository::class)

@Mock
private val messageSender = mock(MessageSender::class)
Expand Down Expand Up @@ -120,7 +118,6 @@ class CallManagerTest {
callManagerImpl = CallManagerImpl(
calling = calling,
callRepository = callRepository,
userRepository = userRepository,
currentClientIdProvider = currentClientIdProvider,
selfConversationIdProvider = selfConversationIdProvider,
conversationRepository = conversationRepository,
Expand All @@ -137,7 +134,8 @@ class CallManagerTest {
kaliumConfigs = kaliumConfigs,
mediaManagerService = mediaManagerService,
flowManagerService = flowManagerService,
createAndPersistRecentlyEndedCallMetadata = createAndPersistRecentlyEndedCallMetadata
createAndPersistRecentlyEndedCallMetadata = createAndPersistRecentlyEndedCallMetadata,
selfUserId = selfUserId
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.wire.kalium.logic.data.id.FederatedIdMapper
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
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
Expand All @@ -44,7 +43,6 @@ actual class GlobalCallManager {
internal actual fun getCallManagerForClient(
userId: QualifiedID,
callRepository: CallRepository,
userRepository: UserRepository,
currentClientIdProvider: CurrentClientIdProvider,
selfConversationIdProvider: SelfConversationIdProvider,
conversationRepository: ConversationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.data.message.Message
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.call.scenario.CallingMessageSender
import com.wire.kalium.logic.feature.call.scenario.OnActiveSpeakers
import com.wire.kalium.logic.feature.call.scenario.OnAnsweredCall
Expand Down Expand Up @@ -104,7 +103,6 @@ import java.util.Collections
class CallManagerImpl internal constructor(
private val calling: Calling,
private val callRepository: CallRepository,
private val userRepository: UserRepository,
private val currentClientIdProvider: CurrentClientIdProvider,
selfConversationIdProvider: SelfConversationIdProvider,
private val conversationRepository: ConversationRepository,
Expand All @@ -121,6 +119,7 @@ class CallManagerImpl internal constructor(
private val mediaManagerService: MediaManagerService,
private val flowManagerService: FlowManagerService,
private val createAndPersistRecentlyEndedCallMetadata: CreateAndPersistRecentlyEndedCallMetadataUseCase,
private val selfUserId: UserId,
private val json: Json = Json { ignoreUnknownKeys = true },
private val shouldRemoteMuteChecker: ShouldRemoteMuteChecker = ShouldRemoteMuteCheckerImpl(),
private val serverTimeHandler: ServerTimeHandler = ServerTimeHandlerImpl(),
Expand Down Expand Up @@ -153,11 +152,6 @@ class CallManagerImpl internal constructor(
it
})
}
private val userId: Deferred<UserId> = scope.async(start = CoroutineStart.LAZY) {
userRepository.observeSelfUser().first().id.also {
callingLogger.d("$TAG - userId ${it.toLogString()}")
}
}

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
private val metricsHandler = MetricsHandler { conversationId: String, metricsJson: String, arg: Pointer? ->
Expand Down Expand Up @@ -186,7 +180,7 @@ class CallManagerImpl internal constructor(
}
joinAll(flowManagerStartJob, mediaManagerStartJob)
callingLogger.i("$TAG: Creating Handle")
val selfUserId = federatedIdMapper.parseToFederatedId(userId.await())
val selfUserId = federatedIdMapper.parseToFederatedId(selfUserId)
val selfClientId = clientId.await().value

val waitInitializationJob = Job()
Expand Down Expand Up @@ -254,7 +248,7 @@ class CallManagerImpl internal constructor(
val conversationMembers = conversationRepository.observeConversationMembers(message.conversationId).first()
val shouldRemoteMute = shouldRemoteMuteChecker.check(
senderUserId = message.senderUserId,
selfUserId = userId.await(),
selfUserId = selfUserId,
selfClientId = clientId.await().value,
targets = callingValue.targets,
conversationMembers = conversationMembers
Expand Down Expand Up @@ -306,7 +300,7 @@ class CallManagerImpl internal constructor(
isMuted = false,
isCameraOn = isCameraOn,
isCbrEnabled = isAudioCbr,
callerId = userId.await()
callerId = selfUserId
)

withCalling {
Expand Down Expand Up @@ -401,7 +395,7 @@ class CallManagerImpl internal constructor(
}
callingLogger.d("$TAG -> set test video to $logString")

val selfUserId = federatedIdMapper.parseToFederatedId(userId.await())
val selfUserId = federatedIdMapper.parseToFederatedId(selfUserId)
val selfClientId = clientId.await().value
val handle = deferredHandle.await()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.wire.kalium.logic.data.id.FederatedIdMapper
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
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
Expand Down Expand Up @@ -82,7 +81,6 @@ actual class GlobalCallManager(
internal actual fun getCallManagerForClient(
userId: QualifiedID,
callRepository: CallRepository,
userRepository: UserRepository,
currentClientIdProvider: CurrentClientIdProvider,
selfConversationIdProvider: SelfConversationIdProvider,
conversationRepository: ConversationRepository,
Expand All @@ -103,7 +101,7 @@ actual class GlobalCallManager(
CallManagerImpl(
calling = calling,
callRepository = callRepository,
userRepository = userRepository,
selfUserId = userId,
currentClientIdProvider = currentClientIdProvider,
selfConversationIdProvider = selfConversationIdProvider,
callMapper = callMapper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@

package com.wire.kalium.logic.data.call

import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.functional.fold

internal interface CallingParticipantsOrder {
suspend fun reorderItems(participants: List<Participant>): List<Participant>
}

internal class CallingParticipantsOrderImpl(
private val userRepository: UserRepository,
private val currentClientIdProvider: CurrentClientIdProvider,
private val participantsFilter: ParticipantsFilter,
private val participantsOrderByName: ParticipantsOrderByName,
private val selfUserId: UserId
) : CallingParticipantsOrder {

override suspend fun reorderItems(participants: List<Participant>): List<Participant> {
return if (participants.isNotEmpty()) {
currentClientIdProvider().fold({
participants
}, { selfClientId ->
val selfUserId = userRepository.getSelfUser()?.id!!

val selfParticipant = participantsFilter.selfParticipant(participants, selfUserId, selfClientId.value)
val otherParticipants = participantsFilter.otherParticipants(participants, selfClientId.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ internal class UserDataSource internal constructor(
private suspend fun updateSelfUserProviderAccountInfo(userDTO: SelfUserDTO): Either<StorageFailure, Unit> =
sessionRepository.updateSsoIdAndScimInfo(userDTO.id.toModel(), idMapper.toSsoId(userDTO.ssoID), userDTO.managedByDTO)

// TODO: race condition, if we request the same user (can happen for self) multiple times, we will fetch it multiple times
override suspend fun getKnownUser(userId: UserId): Flow<OtherUser?> =
userDAO.observeUserDetailsByQualifiedID(qualifiedID = userId.toDao())
.map { userEntity ->
Expand Down Expand Up @@ -414,6 +415,7 @@ internal class UserDataSource internal constructor(
else fetchUsersByIds(missingIds.map { it.toModel() }.toSet()).map { }
}

// TODO: this can cause many issues since it will
@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun observeSelfUser(): Flow<SelfUser> {
return metadataDAO.valueByKeyFlow(SELF_USER_ID_KEY).onEach {
Expand Down Expand Up @@ -467,8 +469,9 @@ internal class UserDataSource internal constructor(
}

// TODO: replace the flow with selfUser and cache it
override suspend fun getSelfUser(): SelfUser? =
observeSelfUser().firstOrNull()
override suspend fun getSelfUser(): SelfUser? {
return observeSelfUser().firstOrNull()
}

override suspend fun observeAllKnownUsers(): Flow<Either<StorageFailure, List<OtherUser>>> {
val selfUserId = selfUserId.toDao()
Expand Down Expand Up @@ -656,7 +659,6 @@ internal class UserDataSource internal constructor(
CreateUserTeam(dto.teamId, dto.teamName)
}
.onSuccess {
kaliumLogger.d("Migrated user to team")
fetchSelfUser()
}
.onFailure { failure ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,6 @@ class UserSessionScope internal constructor(
globalCallManager.getCallManagerForClient(
userId = userId,
callRepository = callRepository,
userRepository = userRepository,
currentClientIdProvider = clientIdProvider,
conversationRepository = conversationRepository,
userConfigRepository = userConfigRepository,
Expand Down Expand Up @@ -2060,7 +2059,6 @@ class UserSessionScope internal constructor(
callManager = callManager,
callRepository = callRepository,
conversationRepository = conversationRepository,
userRepository = userRepository,
flowManagerService = flowManagerService,
mediaManagerService = mediaManagerService,
syncManager = syncManager,
Expand All @@ -2071,6 +2069,8 @@ class UserSessionScope internal constructor(
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
kaliumConfigs = kaliumConfigs,
inCallReactionsRepository = inCallReactionsRepository,
selfUserId = userId,
userRepository = userRepository
)

val connection: ConnectionScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.wire.kalium.logic.data.call.ParticipantsOrderByNameImpl
import com.wire.kalium.logic.data.conversation.ConversationRepository
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.feature.call.usecase.AnswerCallUseCase
import com.wire.kalium.logic.feature.call.usecase.AnswerCallUseCaseImpl
Expand Down Expand Up @@ -107,6 +108,7 @@ class CallsScope internal constructor(
private val getCallConversationType: GetCallConversationTypeProvider,
private val kaliumConfigs: KaliumConfigs,
private val inCallReactionsRepository: InCallReactionsRepository,
private val selfUserId: UserId,
internal val dispatcher: KaliumDispatcher = KaliumDispatcherImpl
) {

Expand All @@ -122,7 +124,7 @@ class CallsScope internal constructor(
get() = GetIncomingCallsUseCaseImpl(
callRepository = callRepository,
conversationRepository = conversationRepository,
userRepository = userRepository
userRepository = userRepository,
)
val observeOutgoingCall: ObserveOutgoingCallUseCase
get() = ObserveOutgoingCallUseCaseImpl(
Expand Down Expand Up @@ -211,10 +213,10 @@ class CallsScope internal constructor(

private val callingParticipantsOrder: CallingParticipantsOrder
get() = CallingParticipantsOrderImpl(
userRepository = userRepository,
currentClientIdProvider = currentClientIdProvider,
participantsFilter = ParticipantsFilterImpl(qualifiedIdMapper),
participantsOrderByName = ParticipantsOrderByNameImpl()
participantsOrderByName = ParticipantsOrderByNameImpl(),
selfUserId = selfUserId
)

val isLastCallClosed: IsLastCallClosedUseCase get() = IsLastCallClosedUseCaseImpl(callRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.wire.kalium.logic.data.id.FederatedIdMapper
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.id.QualifiedIdMapper
import com.wire.kalium.logic.data.user.UserId
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
Expand All @@ -45,7 +44,6 @@ expect class GlobalCallManager {
internal fun getCallManagerForClient(
userId: QualifiedID,
callRepository: CallRepository,
userRepository: UserRepository,
currentClientIdProvider: CurrentClientIdProvider,
selfConversationIdProvider: SelfConversationIdProvider,
conversationRepository: ConversationRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ internal class GetIncomingCallsUseCaseImpl internal constructor(

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun observeIncomingCallsIfUserStatusAllows(): Flow<List<Call>> =
// TODO: do not refresh self form remote in this case and just get status from local
userRepository.observeSelfUser()
.flatMapLatest {

// if user is AWAY we don't show any IncomingCalls
if (it.availabilityStatus == UserAvailabilityStatus.AWAY) {
logger.d("$TAG; Ignoring possible calls based user's status")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ConversationScope internal constructor(
get() = ObserveConversationMembersUseCaseImpl(conversationRepository, userRepository)

val getMembersToMention: MembersToMentionUseCase
get() = MembersToMentionUseCase(observeConversationMembers, userRepository)
get() = MembersToMentionUseCase(observeConversationMembers = observeConversationMembers, selfUserId = selfUserId)

val observeUserListById: ObserveUserListByIdUseCase
get() = ObserveUserListByIdUseCase(userRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.wire.kalium.logic.feature.conversation

import com.wire.kalium.logic.data.conversation.MemberDetails
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.flow.first
Expand All @@ -42,7 +42,7 @@ import kotlinx.coroutines.withContext
@Suppress("ReturnCount")
class MembersToMentionUseCase internal constructor(
private val observeConversationMembers: ObserveConversationMembersUseCase,
private val userRepository: UserRepository,
private val selfUserId: UserId,
private val dispatcher: KaliumDispatcher = KaliumDispatcherImpl
) {
/**
Expand All @@ -57,7 +57,7 @@ class MembersToMentionUseCase internal constructor(
// TODO apply normalization techniques that are used for other searches to the name (e.g. ö -> oe)

val usersToSearch = conversationMembers.filter {
it.user.id != userRepository.getSelfUser()?.id
it.user.id != selfUserId
}
if (searchQuery.isEmpty())
return@withContext usersToSearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ class DebugScope internal constructor(

val sendConfirmation: SendConfirmationUseCase
get() = SendConfirmationUseCase(
userRepository,
currentClientIdProvider,
slowSyncRepository,
messageSender
currentClientIdProvider = currentClientIdProvider,
slowSyncRepository = slowSyncRepository,
messageSender = messageSender,
selfUserId = userId,
)

val disableEventProcessing: DisableEventProcessingUseCase
Expand Down
Loading
Loading