Skip to content

Commit

Permalink
refactor: remove network from registerToken #WPB-9476 (#3051)
Browse files Browse the repository at this point in the history
* refactor: remove network from registerToken #WPB-9476

* cheeky commit singing test

* Revert "cheeky commit singing test"

This reverts commit 44cd4fc.
  • Loading branch information
damian-kaczmarek authored and MohamadJaara committed Oct 18, 2024
1 parent 1cdc126 commit 089eca5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,29 @@ interface ClientRepository {
suspend fun isClientRegistrationBlockedByE2EI(): Either<CoreFailure, Boolean>
suspend fun deleteClient(param: DeleteClientParam): Either<NetworkFailure, Unit>
suspend fun selfListOfClients(): Either<NetworkFailure, List<Client>>
suspend fun observeClientsByUserIdAndClientId(userId: UserId, clientId: ClientId): Flow<Either<StorageFailure, Client>>
suspend fun observeClientsByUserIdAndClientId(
userId: UserId,
clientId: ClientId
): Flow<Either<StorageFailure, Client>>

suspend fun storeUserClientListAndRemoveRedundantClients(clients: List<InsertClientParam>): Either<StorageFailure, Unit>
suspend fun storeUserClientIdList(userId: UserId, clients: List<ClientId>): Either<StorageFailure, Unit>
suspend fun storeUserClientIdList(
userId: UserId,
clients: List<ClientId>
): Either<StorageFailure, Unit>

suspend fun storeMapOfUserToClientId(userToClientMap: Map<UserId, List<ClientId>>): Either<StorageFailure, Unit>
suspend fun removeClientsAndReturnUsersWithNoClients(
redundantClientsOfUsers: Map<UserId, List<ClientId>>
): Either<StorageFailure, List<UserId>>

suspend fun registerToken(body: PushTokenBody): Either<NetworkFailure, Unit>
suspend fun registerToken(
senderId: String,
client: String,
token: String,
transport: String
): Either<NetworkFailure, Unit>

suspend fun deregisterToken(token: String): Either<NetworkFailure, Unit>
suspend fun getClientsByUserId(userId: UserId): Either<StorageFailure, List<OtherUserClient>>
suspend fun observeClientsByUserId(userId: UserId): Flow<Either<StorageFailure, List<Client>>>
Expand Down Expand Up @@ -205,7 +219,10 @@ class ClientDataSource(
}
}

override suspend fun observeClientsByUserIdAndClientId(userId: UserId, clientId: ClientId): Flow<Either<StorageFailure, Client>> =
override suspend fun observeClientsByUserIdAndClientId(
userId: UserId,
clientId: ClientId
): Flow<Either<StorageFailure, Client>> =
clientDAO.observeClient(userId.toDao(), clientId.value)
.map { it?.let { clientMapper.fromClientEntity(it) } }
.wrapStorageRequest()
Expand Down Expand Up @@ -250,18 +267,32 @@ 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() }
}
}

override suspend fun storeUserClientListAndRemoveRedundantClients(
clients: List<InsertClientParam>
): Either<StorageFailure, Unit> = wrapStorageRequest { clientDAO.insertClientsAndRemoveRedundant(clients) }

override suspend fun registerToken(body: PushTokenBody): Either<NetworkFailure, Unit> = clientRemoteRepository.registerToken(body)
override suspend fun deregisterToken(token: String): Either<NetworkFailure, Unit> = clientRemoteRepository.deregisterToken(token)
): Either<StorageFailure, Unit> =
wrapStorageRequest { clientDAO.insertClientsAndRemoveRedundant(clients) }

override suspend fun registerToken(
senderId: String,
client: String,
token: String,
transport: String
): Either<NetworkFailure, Unit> = clientRemoteRepository.registerToken(
PushTokenBody(senderId, client, token, transport)
)

override suspend fun deregisterToken(token: String): Either<NetworkFailure, Unit> =
clientRemoteRepository.deregisterToken(token)

override suspend fun getClientsByUserId(userId: UserId): Either<StorageFailure, List<OtherUserClient>> =
wrapStorageRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PushTokenUpdaterTest {
}.wasNotInvoked()

coVerify {
arrangement.clientRepository.registerToken(any())
arrangement.clientRepository.registerToken(any(), any(), any(), any())
}.wasNotInvoked()

coVerify {
Expand All @@ -81,7 +81,7 @@ class PushTokenUpdaterTest {
}.wasNotInvoked()

coVerify {
arrangement.clientRepository.registerToken(any())
arrangement.clientRepository.registerToken(any(), any(), any(), any())
}.wasNotInvoked()

coVerify {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -148,7 +162,7 @@ class PushTokenUpdaterTest {

suspend fun withRegisterTokenResult(result: Either<NetworkFailure, Unit>) = apply {
coEvery {
clientRepository.registerToken(any())
clientRepository.registerToken(any(), any(), any(), any())
}.returns(result)
}

Expand Down

0 comments on commit 089eca5

Please sign in to comment.