diff --git a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt index 26b900eb87..442eea6206 100644 --- a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt +++ b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt @@ -12,12 +12,13 @@ import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.core.data.tryOrNull import io.element.android.libraries.di.AppScope import kotlinx.coroutines.withContext +import retrofit2.HttpException import timber.log.Timber import java.net.URL import javax.inject.Inject interface UnifiedPushGatewayResolver { - suspend fun getGateway(endpoint: String): String + suspend fun getGateway(endpoint: String, previousGateway: String?): String } @ContributesBinding(AppScope::class) @@ -27,7 +28,7 @@ class DefaultUnifiedPushGatewayResolver @Inject constructor( ) : UnifiedPushGatewayResolver { private val logger = Timber.tag("DefaultUnifiedPushGatewayResolver") - override suspend fun getGateway(endpoint: String): String { + override suspend fun getGateway(endpoint: String, previousGateway: String?): String { val url = tryOrNull( onError = { logger.d(it, "Cannot parse endpoint as an URL") } ) { @@ -47,14 +48,23 @@ class DefaultUnifiedPushGatewayResolver @Inject constructor( val discoveryResponse = api.discover() if (discoveryResponse.unifiedpush.gateway == "matrix") { logger.d("The endpoint seems to be a valid UnifiedPush gateway") + customUrl } else { - logger.e("The endpoint does not seem to be a valid UnifiedPush gateway") + logger.w("The endpoint does not seem to be a valid UnifiedPush gateway, using fallback") + UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL + } + } catch (exception: HttpException) { + if (exception.code() == 404) { + logger.i("Checking for UnifiedPush endpoint yielded 404, using fallback") + UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL + } else { + logger.e(exception, "Error checking for UnifiedPush endpoint") + previousGateway ?: customUrl } } catch (throwable: Throwable) { logger.e(throwable, "Error checking for UnifiedPush endpoint") + previousGateway ?: customUrl } - // Always return the custom url. - customUrl } } } diff --git a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/VectorUnifiedPushMessagingReceiver.kt b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/VectorUnifiedPushMessagingReceiver.kt index 51e6729fda..98e2f9d9e7 100644 --- a/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/VectorUnifiedPushMessagingReceiver.kt +++ b/libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/VectorUnifiedPushMessagingReceiver.kt @@ -63,7 +63,7 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() { override fun onNewEndpoint(context: Context, endpoint: String, instance: String) { Timber.tag(loggerTag.value).i("onNewEndpoint: $endpoint") coroutineScope.launch { - val gateway = unifiedPushGatewayResolver.getGateway(endpoint) + val gateway = unifiedPushGatewayResolver.getGateway(endpoint, unifiedPushStore.getPushGateway(instance)) unifiedPushStore.storePushGateway(instance, gateway) val result = newGatewayHandler.handle(endpoint, gateway, instance) .onFailure {