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

chore: Improve isSelfATeamMember UseCase (WPB-8978) #2924

Merged
merged 3 commits into from
Aug 1, 2024
Merged
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 @@ -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
Loading