Skip to content

Commit

Permalink
chore: Improve isSelfATeamMember UseCase (WPB-8978) (#2924)
Browse files Browse the repository at this point in the history
* chore: add slow sync repository to observe for last sync completion before returning default false value for isSelfATeamMember

* chore: add parameter for SlowSyncRepository for new usage

* tests: adjust existing tests for the usecase
  • Loading branch information
alexandreferris authored Aug 1, 2024
1 parent 21725e7 commit 9dbf5f5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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

class TeamScope internal constructor(
private val teamRepository: TeamRepository,
private val conversationRepository: ConversationRepository,
private val slowSyncRepository: SlowSyncRepository,
private val selfTeamIdProvider: SelfTeamIdProvider
) {
val getUpdatedSelfTeamUseCase: GetUpdatedSelfTeamUseCase
Expand All @@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>
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<Boolean> = slowSyncRepository
.observeLastSlowSyncCompletionInstant()
.filterNotNull()
.map {
selfTeamIdProvider()
.fold({ false }, { it != null })
}

override suspend operator fun invoke(): Boolean = observe().first()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -52,6 +56,7 @@ class IsSelfATeamMemberUseCaseTest {
@Test
fun givenSelfIsNotTeamMember_thenReturnTrue() = runTest {
val (arrangement, isSelfTeamMember) = Arrangement()
.withLastSlowSyncCompletionInstant(Instant.DISTANT_PAST)
.withSelfTeamId(Either.Right(null))
.arrange()

Expand All @@ -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<CoreFailure, TeamId?>) = apply {
coEvery {
selfTeamIdProvider.invoke()
Expand Down

0 comments on commit 9dbf5f5

Please sign in to comment.