diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt index b753babfcb2..68a353dc754 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt @@ -1976,7 +1976,12 @@ class UserSessionScope internal constructor( appLockConfigHandler ) - val team: TeamScope get() = TeamScope(teamRepository, conversationRepository, selfTeamId) + val team: TeamScope get() = TeamScope( + teamRepository = teamRepository, + conversationRepository = conversationRepository, + slowSyncRepository = slowSyncRepository, + selfTeamIdProvider = selfTeamId + ) val service: ServiceScope get() = ServiceScope( diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/team/TeamScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/team/TeamScope.kt index a9a184e561b..bc31e9ae436 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/team/TeamScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/team/TeamScope.kt @@ -20,6 +20,7 @@ package com.wire.kalium.logic.feature.team import com.wire.kalium.logic.data.conversation.ConversationRepository import com.wire.kalium.logic.data.id.SelfTeamIdProvider +import com.wire.kalium.logic.data.sync.SlowSyncRepository import com.wire.kalium.logic.data.team.TeamRepository import com.wire.kalium.logic.feature.user.IsSelfATeamMemberUseCase import com.wire.kalium.logic.feature.user.IsSelfATeamMemberUseCaseImpl @@ -27,6 +28,7 @@ import com.wire.kalium.logic.feature.user.IsSelfATeamMemberUseCaseImpl class TeamScope internal constructor( private val teamRepository: TeamRepository, private val conversationRepository: ConversationRepository, + private val slowSyncRepository: SlowSyncRepository, private val selfTeamIdProvider: SelfTeamIdProvider ) { val getUpdatedSelfTeamUseCase: GetUpdatedSelfTeamUseCase @@ -42,5 +44,8 @@ class TeamScope internal constructor( conversationRepository = conversationRepository, ) - val isSelfATeamMember: IsSelfATeamMemberUseCase get() = IsSelfATeamMemberUseCaseImpl(selfTeamIdProvider) + val isSelfATeamMember: IsSelfATeamMemberUseCase get() = IsSelfATeamMemberUseCaseImpl( + selfTeamIdProvider = selfTeamIdProvider, + slowSyncRepository = slowSyncRepository + ) } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCase.kt index 9592703baec..23268f9679a 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCase.kt @@ -19,20 +19,38 @@ package com.wire.kalium.logic.feature.user import com.wire.kalium.logic.data.id.SelfTeamIdProvider +import com.wire.kalium.logic.data.sync.SlowSyncRepository import com.wire.kalium.logic.functional.fold +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map /** * Checks if the self user is a team member or not. * @return true if the self user is a team member, false otherwise. */ -fun interface IsSelfATeamMemberUseCase { +interface IsSelfATeamMemberUseCase { + /** + * Flow that emits the current value, _i.e._ whether self user is a team member or not. + * It will _not_ emit while SlowSync isn't done, as it needs to assure that SelfUser has been properly initialised. + */ + suspend fun observe(): Flow suspend operator fun invoke(): Boolean } -class IsSelfATeamMemberUseCaseImpl internal constructor( - private val selfTeamIdProvider: SelfTeamIdProvider +internal class IsSelfATeamMemberUseCaseImpl internal constructor( + private val selfTeamIdProvider: SelfTeamIdProvider, + private val slowSyncRepository: SlowSyncRepository ) : IsSelfATeamMemberUseCase { - override suspend operator fun invoke(): Boolean = selfTeamIdProvider().fold({ false }, { - it != null - }) + + override suspend fun observe(): Flow = slowSyncRepository + .observeLastSlowSyncCompletionInstant() + .filterNotNull() + .map { + selfTeamIdProvider() + .fold({ false }, { it != null }) + } + + override suspend operator fun invoke(): Boolean = observe().first() } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCaseTest.kt index 7c05f631e76..57a974e66cc 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/user/IsSelfATeamMemberUseCaseTest.kt @@ -21,13 +21,16 @@ package com.wire.kalium.logic.feature.user import com.wire.kalium.logic.CoreFailure import com.wire.kalium.logic.data.id.TeamId import com.wire.kalium.logic.data.id.SelfTeamIdProvider +import com.wire.kalium.logic.data.sync.SlowSyncRepository import com.wire.kalium.logic.functional.Either import io.mockative.Mock import io.mockative.coEvery import io.mockative.coVerify import io.mockative.mock import io.mockative.once +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.test.runTest +import kotlinx.datetime.Instant import kotlin.test.Test import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -37,6 +40,7 @@ class IsSelfATeamMemberUseCaseTest { @Test fun givenSelfIsTeamMember_thenReturnTrue() = runTest { val (arrangement, isSelfTeamMember) = Arrangement() + .withLastSlowSyncCompletionInstant(Instant.DISTANT_PAST) .withSelfTeamId(Either.Right(TeamId("gg"))) .arrange() @@ -52,6 +56,7 @@ class IsSelfATeamMemberUseCaseTest { @Test fun givenSelfIsNotTeamMember_thenReturnTrue() = runTest { val (arrangement, isSelfTeamMember) = Arrangement() + .withLastSlowSyncCompletionInstant(Instant.DISTANT_PAST) .withSelfTeamId(Either.Right(null)) .arrange() @@ -68,7 +73,18 @@ class IsSelfATeamMemberUseCaseTest { @Mock val selfTeamIdProvider: SelfTeamIdProvider = mock(SelfTeamIdProvider::class) - private val isSelfATeamMember: IsSelfATeamMemberUseCaseImpl = IsSelfATeamMemberUseCaseImpl(selfTeamIdProvider) + @Mock + val slowSyncRepository: SlowSyncRepository = mock(SlowSyncRepository::class) + + private val isSelfATeamMember: IsSelfATeamMemberUseCaseImpl = IsSelfATeamMemberUseCaseImpl( + selfTeamIdProvider = selfTeamIdProvider, + slowSyncRepository = slowSyncRepository + ) + + suspend fun withLastSlowSyncCompletionInstant(result: Instant?) = apply { + coEvery { slowSyncRepository.observeLastSlowSyncCompletionInstant() }.returns(flowOf(result)) + } + suspend fun withSelfTeamId(result: Either) = apply { coEvery { selfTeamIdProvider.invoke()