-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create observer for legal hold state (WPB-5442) (#2251)
* feat: Create observer for legal hold state * chore: rename use case
- Loading branch information
1 parent
aba5f75
commit 21fa1eb
Showing
7 changed files
with
371 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...Main/kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldForSelfUserUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.legalhold | ||
|
||
import com.wire.kalium.logic.data.user.UserId | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Use case that allows to observe the legal hold state for the self user. | ||
*/ | ||
interface ObserveLegalHoldForSelfUserUseCase { | ||
suspend operator fun invoke(): Flow<LegalHoldState> | ||
} | ||
|
||
internal class ObserveLegalHoldForSelfUserUseCaseImpl internal constructor( | ||
private val selfUserId: UserId, | ||
private val observeLegalHoldStateForUser: ObserveLegalHoldStateForUserUseCase | ||
) : ObserveLegalHoldForSelfUserUseCase { | ||
override suspend fun invoke(): Flow<LegalHoldState> = observeLegalHoldStateForUser( | ||
userId = selfUserId | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ain/kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldStateForUserUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.legalhold | ||
|
||
import com.wire.kalium.logic.data.client.ClientRepository | ||
import com.wire.kalium.logic.data.client.DeviceType | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.functional.fold | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
/** | ||
* Use case that allows to observe the legal hold state for a given user. | ||
*/ | ||
interface ObserveLegalHoldStateForUserUseCase { | ||
suspend operator fun invoke(userId: UserId): Flow<LegalHoldState> | ||
} | ||
|
||
internal class ObserveLegalHoldStateForUserUseCaseImpl internal constructor( | ||
private val clientRepository: ClientRepository | ||
) : ObserveLegalHoldStateForUserUseCase { | ||
override suspend fun invoke(userId: UserId): Flow<LegalHoldState> = | ||
clientRepository.observeClientsByUserId(userId).map { | ||
it.fold( | ||
{ | ||
LegalHoldState.Disabled | ||
}, | ||
{ clients -> | ||
val isLegalHoldEnabled = clients.any { otherUserClient -> | ||
otherUserClient.deviceType == DeviceType.LegalHold | ||
} | ||
if (isLegalHoldEnabled) { | ||
LegalHoldState.Enabled | ||
} else { | ||
LegalHoldState.Disabled | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
sealed class LegalHoldState { | ||
data object Enabled : LegalHoldState() | ||
data object Disabled : LegalHoldState() | ||
} |
98 changes: 98 additions & 0 deletions
98
.../kotlin/com/wire/kalium/logic/feature/legalhold/ObserveLegalHoldForSelfUserUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.legalhold | ||
|
||
import com.wire.kalium.logic.framework.TestUser | ||
import io.mockative.Mock | ||
import io.mockative.eq | ||
import io.mockative.given | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ObserveLegalHoldForSelfUserUseCaseTest { | ||
|
||
@Test | ||
fun givenLegalHoldObserverForUserReturnsEnabled_whenStartingObservingForSelfUser_thenEmitEnabled() = | ||
runTest { | ||
val (arrangement, useCase) = Arrangement() | ||
.withLegalHoldEnabledState() | ||
.arrange() | ||
|
||
val result = useCase() | ||
|
||
assertEquals(LegalHoldState.Enabled, result.first()) | ||
verify(arrangement.observeLegalHoldStateForUser) | ||
.suspendFunction(arrangement.observeLegalHoldStateForUser::invoke) | ||
.with(eq(TestUser.SELF.id)) | ||
.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenLegalHoldObserverForUserReturnsDisabled_whenStartingObservingForSelfUser_thenEmitDisabled() = | ||
runTest { | ||
val (arrangement, useCase) = Arrangement() | ||
.withLegalHoldDisabledState() | ||
.arrange() | ||
|
||
val result = useCase() | ||
|
||
assertEquals(LegalHoldState.Disabled, result.first()) | ||
verify(arrangement.observeLegalHoldStateForUser) | ||
.suspendFunction(arrangement.observeLegalHoldStateForUser::invoke) | ||
.with(eq(TestUser.SELF.id)) | ||
.wasInvoked(once) | ||
} | ||
|
||
private class Arrangement { | ||
|
||
@Mock | ||
val observeLegalHoldStateForUser = mock(ObserveLegalHoldStateForUserUseCase::class) | ||
|
||
val observeLegalHoldForSelfUser: ObserveLegalHoldForSelfUserUseCase = | ||
ObserveLegalHoldForSelfUserUseCaseImpl( | ||
selfUserId = TestUser.SELF.id, | ||
observeLegalHoldStateForUser = observeLegalHoldStateForUser | ||
) | ||
|
||
fun arrange() = this to observeLegalHoldForSelfUser | ||
|
||
fun withLegalHoldEnabledState() = apply { | ||
given(observeLegalHoldStateForUser) | ||
.suspendFunction(observeLegalHoldStateForUser::invoke) | ||
.whenInvokedWith(eq(TestUser.SELF.id)) | ||
.then { | ||
flowOf(LegalHoldState.Enabled) | ||
} | ||
} | ||
|
||
fun withLegalHoldDisabledState() = apply { | ||
given(observeLegalHoldStateForUser) | ||
.suspendFunction(observeLegalHoldStateForUser::invoke) | ||
.whenInvokedWith(eq(TestUser.SELF.id)) | ||
.then { | ||
flowOf(LegalHoldState.Disabled) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.