diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ClientRepository.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ClientRepository.kt index 8cca1a5da58..eb84c333743 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ClientRepository.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/client/ClientRepository.kt @@ -76,15 +76,29 @@ interface ClientRepository { suspend fun isClientRegistrationBlockedByE2EI(): Either suspend fun deleteClient(param: DeleteClientParam): Either suspend fun selfListOfClients(): Either> - suspend fun observeClientsByUserIdAndClientId(userId: UserId, clientId: ClientId): Flow> + suspend fun observeClientsByUserIdAndClientId( + userId: UserId, + clientId: ClientId + ): Flow> + suspend fun storeUserClientListAndRemoveRedundantClients(clients: List): Either - suspend fun storeUserClientIdList(userId: UserId, clients: List): Either + suspend fun storeUserClientIdList( + userId: UserId, + clients: List + ): Either + suspend fun storeMapOfUserToClientId(userToClientMap: Map>): Either suspend fun removeClientsAndReturnUsersWithNoClients( redundantClientsOfUsers: Map> ): Either> - suspend fun registerToken(body: PushTokenBody): Either + suspend fun registerToken( + senderId: String, + client: String, + token: String, + transport: String + ): Either + suspend fun deregisterToken(token: String): Either suspend fun getClientsByUserId(userId: UserId): Either> suspend fun observeClientsByUserId(userId: UserId): Flow>> @@ -205,7 +219,10 @@ class ClientDataSource( } } - override suspend fun observeClientsByUserIdAndClientId(userId: UserId, clientId: ClientId): Flow> = + override suspend fun observeClientsByUserIdAndClientId( + userId: UserId, + clientId: ClientId + ): Flow> = clientDAO.observeClient(userId.toDao(), clientId.value) .map { it?.let { clientMapper.fromClientEntity(it) } } .wrapStorageRequest() @@ -250,7 +267,11 @@ class ClientDataSource( redundantClientsOfUsers.mapKeys { it.key.toDao() } .mapValues { it.value.map { clientId -> clientId.value } } .let { redundantClientsOfUsersDao -> - wrapStorageRequest { clientDAO.removeClientsAndReturnUsersWithNoClients(redundantClientsOfUsersDao) } + wrapStorageRequest { + clientDAO.removeClientsAndReturnUsersWithNoClients( + redundantClientsOfUsersDao + ) + } .map { it.map { userId -> userId.toModel() } } @@ -258,10 +279,20 @@ class ClientDataSource( override suspend fun storeUserClientListAndRemoveRedundantClients( clients: List - ): Either = wrapStorageRequest { clientDAO.insertClientsAndRemoveRedundant(clients) } - - override suspend fun registerToken(body: PushTokenBody): Either = clientRemoteRepository.registerToken(body) - override suspend fun deregisterToken(token: String): Either = clientRemoteRepository.deregisterToken(token) + ): Either = + wrapStorageRequest { clientDAO.insertClientsAndRemoveRedundant(clients) } + + override suspend fun registerToken( + senderId: String, + client: String, + token: String, + transport: String + ): Either = clientRemoteRepository.registerToken( + PushTokenBody(senderId, client, token, transport) + ) + + override suspend fun deregisterToken(token: String): Either = + clientRemoteRepository.deregisterToken(token) override suspend fun getClientsByUserId(userId: UserId): Either> = wrapStorageRequest { diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdater.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdater.kt index b1da606fafc..5c7cc3f8253 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdater.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdater.kt @@ -15,7 +15,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ -@file:Suppress("konsist.useCasesShouldNotAccessNetworkLayerDirectly") package com.wire.kalium.logic.feature.notificationToken @@ -27,7 +26,6 @@ import com.wire.kalium.logic.functional.flatMap import com.wire.kalium.logic.functional.isRight import com.wire.kalium.logic.functional.onFailure import com.wire.kalium.logic.kaliumLogger -import com.wire.kalium.network.api.model.PushTokenBody import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter @@ -51,18 +49,18 @@ internal class PushTokenUpdater( notificationTokenRepository.getNotificationToken() .flatMap { notificationToken -> clientRepository.registerToken( - body = PushTokenBody( - senderId = notificationToken.applicationId, - client = clientId.value, - token = notificationToken.token, - transport = notificationToken.transport - ) + senderId = notificationToken.applicationId, + client = clientId.value, + token = notificationToken.token, + transport = notificationToken.transport ) } .onFailure { kaliumLogger.i( "$TAG Error while registering Firebase token " + - "for the client: ${clientId.toString().obfuscateId()} error: $it" + "for the client: ${ + clientId.toString().obfuscateId() + } error: $it" ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdaterTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdaterTest.kt index eb7b15f107d..08e9d595adf 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdaterTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/notificationToken/PushTokenUpdaterTest.kt @@ -59,7 +59,7 @@ class PushTokenUpdaterTest { }.wasNotInvoked() coVerify { - arrangement.clientRepository.registerToken(any()) + arrangement.clientRepository.registerToken(any(), any(), any(), any()) }.wasNotInvoked() coVerify { @@ -81,7 +81,7 @@ class PushTokenUpdaterTest { }.wasNotInvoked() coVerify { - arrangement.clientRepository.registerToken(any()) + arrangement.clientRepository.registerToken(any(), any(), any(), any()) }.wasNotInvoked() coVerify { @@ -94,14 +94,27 @@ class PushTokenUpdaterTest { val (arrangement, pushTokenUpdater) = Arrangement() .withUpdateFirebaseTokenFlag(true) .withCurrentClientId(ClientId(MOCK_CLIENT_ID)) - .withNotificationToken(Either.Right(NotificationToken(MOCK_TOKEN, MOCK_TRANSPORT, MOCK_APP_ID))) + .withNotificationToken( + Either.Right( + NotificationToken( + MOCK_TOKEN, + MOCK_TRANSPORT, + MOCK_APP_ID + ) + ) + ) .withRegisterTokenResult(Either.Right(Unit)) .arrange() pushTokenUpdater.monitorTokenChanges() coVerify { - arrangement.clientRepository.registerToken(eq(pushTokenRequestBody)) + arrangement.clientRepository.registerToken( + eq(pushTokenRequestBody.senderId), + eq(pushTokenRequestBody.client), + eq(pushTokenRequestBody.token), + eq(pushTokenRequestBody.transport), + ) }.wasInvoked(once) coVerify { @@ -129,7 +142,8 @@ class PushTokenUpdaterTest { val clientRepository: ClientRepository = mock(ClientRepository::class) @Mock - val notificationTokenRepository: NotificationTokenRepository = mock(NotificationTokenRepository::class) + val notificationTokenRepository: NotificationTokenRepository = + mock(NotificationTokenRepository::class) @Mock val pushTokenRepository: PushTokenRepository = mock(PushTokenRepository::class) @@ -148,7 +162,7 @@ class PushTokenUpdaterTest { suspend fun withRegisterTokenResult(result: Either) = apply { coEvery { - clientRepository.registerToken(any()) + clientRepository.registerToken(any(), any(), any(), any()) }.returns(result) }