Store the first provider even if no distributor is available, else error in troubleshoot test will not be accurate.

Also when registering for the first time, pick the fist available provider with at least one distributor.
This commit is contained in:
Benoit Marty
2024-06-17 17:36:39 +02:00
parent f72fc36de8
commit b4b407a69e
4 changed files with 33 additions and 2 deletions
@@ -113,14 +113,21 @@ class LoggedInPresenter @Inject constructor(
Timber.tag(pusherTag.value).d("Ensure pusher is registered") Timber.tag(pusherTag.value).d("Ensure pusher is registered")
val currentPushProvider = pushService.getCurrentPushProvider() val currentPushProvider = pushService.getCurrentPushProvider()
val result = if (currentPushProvider == null) { val result = if (currentPushProvider == null) {
Timber.tag(pusherTag.value).d("Register with the first available push provider") Timber.tag(pusherTag.value).d("Register with the first available push provider with at least one distributor")
val pushProvider = pushService.getAvailablePushProviders().firstOrNull() val pushProvider = pushService.getAvailablePushProviders()
.firstOrNull { it.getDistributors().isNotEmpty() }
// Else fallback to the first available push provider (the list should never be empty)
?: pushService.getAvailablePushProviders().firstOrNull()
?: return Unit ?: return Unit
.also { Timber.tag(pusherTag.value).w("No push providers available") } .also { Timber.tag(pusherTag.value).w("No push providers available") }
.also { pusherRegistrationState.value = AsyncData.Failure(PusherRegistrationFailure.NoProvidersAvailable()) } .also { pusherRegistrationState.value = AsyncData.Failure(PusherRegistrationFailure.NoProvidersAvailable()) }
val distributor = pushProvider.getDistributors().firstOrNull() val distributor = pushProvider.getDistributors().firstOrNull()
?: return Unit ?: return Unit
.also { Timber.tag(pusherTag.value).w("No distributors available") } .also { Timber.tag(pusherTag.value).w("No distributors available") }
.also {
// In this case, consider the push provider is chosen.
pushService.selectPushProvider(matrixClient, pushProvider)
}
.also { pusherRegistrationState.value = AsyncData.Failure(PusherRegistrationFailure.NoDistributorsAvailable()) } .also { pusherRegistrationState.value = AsyncData.Failure(PusherRegistrationFailure.NoDistributorsAvailable()) }
pushService.registerWith(matrixClient, pushProvider, distributor) pushService.registerWith(matrixClient, pushProvider, distributor)
} else { } else {
@@ -44,6 +44,15 @@ interface PushService {
distributor: Distributor, distributor: Distributor,
): Result<Unit> ): Result<Unit>
/**
* Store the given push provider as the current one, but do not register.
* To be used when there is no distributor available.
*/
suspend fun selectPushProvider(
matrixClient: MatrixClient,
pushProvider: PushProvider,
)
fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean> fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean>
suspend fun setIgnoreRegistrationError(sessionId: SessionId, ignore: Boolean) suspend fun setIgnoreRegistrationError(sessionId: SessionId, ignore: Boolean)
@@ -73,6 +73,15 @@ class DefaultPushService @Inject constructor(
return pushProvider.registerWith(matrixClient, distributor) return pushProvider.registerWith(matrixClient, distributor)
} }
override suspend fun selectPushProvider(
matrixClient: MatrixClient,
pushProvider: PushProvider,
) {
Timber.d("Select ${pushProvider.name}")
val userPushStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
userPushStore.setPushProviderName(pushProvider.name)
}
override fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean> { override fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean> {
return userPushStoreFactory.getOrCreate(sessionId).ignoreRegistrationError() return userPushStoreFactory.getOrCreate(sessionId).ignoreRegistrationError()
} }
@@ -21,6 +21,7 @@ import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.push.api.PushService import io.element.android.libraries.push.api.PushService
import io.element.android.libraries.pushproviders.api.Distributor import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.api.PushProvider import io.element.android.libraries.pushproviders.api.PushProvider
import io.element.android.tests.testutils.lambda.lambdaError
import io.element.android.tests.testutils.simulateLongTask import io.element.android.tests.testutils.simulateLongTask
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -32,6 +33,7 @@ class FakePushService(
Result.success(Unit) Result.success(Unit)
}, },
private val currentPushProvider: () -> PushProvider? = { availablePushProviders.firstOrNull() }, private val currentPushProvider: () -> PushProvider? = { availablePushProviders.firstOrNull() },
private val selectPushProviderLambda: suspend (MatrixClient, PushProvider) -> Unit = { _, _ -> lambdaError() }
) : PushService { ) : PushService {
override suspend fun getCurrentPushProvider(): PushProvider? { override suspend fun getCurrentPushProvider(): PushProvider? {
return registeredPushProvider ?: currentPushProvider() return registeredPushProvider ?: currentPushProvider()
@@ -56,6 +58,10 @@ class FakePushService(
} }
} }
override suspend fun selectPushProvider(matrixClient: MatrixClient, pushProvider: PushProvider) {
selectPushProviderLambda(matrixClient, pushProvider)
}
private val ignoreRegistrationError = MutableStateFlow(false) private val ignoreRegistrationError = MutableStateFlow(false)
override fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean> { override fun ignoreRegistrationError(sessionId: SessionId): Flow<Boolean> {