From 330cb384cf3a77277c757ad84cc55d8ce82e2bc9 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Sat, 14 Dec 2024 09:55:58 +0100 Subject: [PATCH] Properly handle UnifiedPush distributors without matrix gateway again Fixes https://github.com/SchildiChat/schildichat-android-next/issues/38 Co-authored-by: sim Change-Id: I2b349911f6ddffd4a2dda89fb6e4057df8759a60 --- .../unifiedpush/UnifiedPushGatewayResolver.kt | 20 ++++++++++++++----- .../VectorUnifiedPushMessagingReceiver.kt | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) 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 {