From 3cbdc97cdd9a4bf962c95ba7261863d782a49d09 Mon Sep 17 00:00:00 2001 From: S1m <31284753+p1gp1g@users.noreply.github.com> Date: Mon, 17 Nov 2025 12:34:35 +0000 Subject: [PATCH] Fix push gateway with some push provider (Sunup/autopush) (#5741) * Add more HTTP response code returning NoMatrixGateway Fix Push notifications with Mozilla's autopush that returns 406 * Update gateway resolver tests to match new known errors --- .../unifiedpush/UnifiedPushGatewayResolver.kt | 15 +++++++-- .../DefaultUnifiedPushGatewayResolverTest.kt | 32 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 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 4f217c485d..8aa67b1ee9 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 @@ -64,8 +64,9 @@ class DefaultUnifiedPushGatewayResolver( UnifiedPushGatewayResolverResult.NoMatrixGateway } } catch (throwable: Throwable) { - if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) { - Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded 404, using fallback") + val code = (throwable as? HttpException)?.code() + if (code in NoMatrixGatewayResp) { + Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded $code, using fallback") UnifiedPushGatewayResolverResult.NoMatrixGateway } else { Timber.tag(loggerTag.value).e(throwable, "Error checking for UnifiedPush endpoint") @@ -75,4 +76,14 @@ class DefaultUnifiedPushGatewayResolver( } } } + + companion object { + private val NoMatrixGatewayResp = listOf( + HttpURLConnection.HTTP_UNAUTHORIZED, + HttpURLConnection.HTTP_FORBIDDEN, + HttpURLConnection.HTTP_NOT_FOUND, + HttpURLConnection.HTTP_BAD_METHOD, + HttpURLConnection.HTTP_NOT_ACCEPTABLE + ) + } } diff --git a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt index 8b2fc259f7..642f14438f 100644 --- a/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt +++ b/libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt @@ -119,7 +119,7 @@ class DefaultUnifiedPushGatewayResolverTest { } @Test - fun `when a custom url is forbidden (403), Error is returned`() = runTest { + fun `when a custom url is forbidden (403), NoMatrixGateway is returned`() = runTest { val unifiedPushApiFactory = FakeUnifiedPushApiFactory( discoveryResponse = { throw HttpException(Response.error(HttpURLConnection.HTTP_FORBIDDEN, "".toResponseBody())) @@ -130,6 +130,36 @@ class DefaultUnifiedPushGatewayResolverTest { ) val result = sut.getGateway("http://custom.url") assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url") + assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway) + } + + @Test + fun `when a custom url is not acceptable (406), NoMatrixGateway is returned`() = runTest { + val unifiedPushApiFactory = FakeUnifiedPushApiFactory( + discoveryResponse = { + throw HttpException(Response.error(HttpURLConnection.HTTP_NOT_ACCEPTABLE, "".toResponseBody())) + } + ) + val sut = createDefaultUnifiedPushGatewayResolver( + unifiedPushApiFactory = unifiedPushApiFactory + ) + val result = sut.getGateway("http://custom.url") + assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url") + assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway) + } + + @Test + fun `when a custom url is internal error (500), Error is returned`() = runTest { + val unifiedPushApiFactory = FakeUnifiedPushApiFactory( + discoveryResponse = { + throw HttpException(Response.error(HttpURLConnection.HTTP_INTERNAL_ERROR, "".toResponseBody())) + } + ) + val sut = createDefaultUnifiedPushGatewayResolver( + unifiedPushApiFactory = unifiedPushApiFactory + ) + val result = sut.getGateway("http://custom.url") + assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url") assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.Error("http://custom.url/_matrix/push/v1/notify")) }