Skip to content

Commit

Permalink
Properly handle UnifiedPush distributors without matrix gateway again
Browse files Browse the repository at this point in the history
Fixes #38

Co-authored-by: sim <git@sgougeon.fr>
Change-Id: I2b349911f6ddffd4a2dda89fb6e4057df8759a60
  • Loading branch information
SpiritCroc and p1gp1g committed Dec 14, 2024
1 parent 3b20a71 commit 330cb38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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") }
) {
Expand All @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 330cb38

Please sign in to comment.