Create a loggerTag val.

This commit is contained in:
Benoit Marty 2024-12-11 12:14:03 +01:00 committed by Benoit Marty
parent f32495ee58
commit a55a493060

View file

@ -10,6 +10,7 @@ package io.element.android.libraries.pushproviders.unifiedpush
import com.squareup.anvil.annotations.ContributesBinding import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.data.tryOrNull import io.element.android.libraries.core.data.tryOrNull
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.di.AppScope import io.element.android.libraries.di.AppScope
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
@ -29,6 +30,8 @@ interface UnifiedPushGatewayResolver {
suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult
} }
private val loggerTag = LoggerTag("DefaultUnifiedPushGatewayResolver")
@ContributesBinding(AppScope::class) @ContributesBinding(AppScope::class)
class DefaultUnifiedPushGatewayResolver @Inject constructor( class DefaultUnifiedPushGatewayResolver @Inject constructor(
private val unifiedPushApiFactory: UnifiedPushApiFactory, private val unifiedPushApiFactory: UnifiedPushApiFactory,
@ -36,36 +39,36 @@ class DefaultUnifiedPushGatewayResolver @Inject constructor(
) : UnifiedPushGatewayResolver { ) : UnifiedPushGatewayResolver {
override suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult { override suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult {
val url = tryOrNull( val url = tryOrNull(
onException = { Timber.tag("DefaultUnifiedPushGatewayResolver").d(it, "Cannot parse endpoint as an URL") } onException = { Timber.tag(loggerTag.value).d(it, "Cannot parse endpoint as an URL") }
) { ) {
URL(endpoint) URL(endpoint)
} }
return if (url == null) { return if (url == null) {
Timber.tag("DefaultUnifiedPushGatewayResolver").d("ErrorInvalidUrl") Timber.tag(loggerTag.value).d("ErrorInvalidUrl")
UnifiedPushGatewayResolverResult.ErrorInvalidUrl UnifiedPushGatewayResolverResult.ErrorInvalidUrl
} else { } else {
val port = if (url.port != -1) ":${url.port}" else "" val port = if (url.port != -1) ":${url.port}" else ""
val customBase = "${url.protocol}://${url.host}$port" val customBase = "${url.protocol}://${url.host}$port"
val customUrl = "$customBase/_matrix/push/v1/notify" val customUrl = "$customBase/_matrix/push/v1/notify"
Timber.tag("DefaultUnifiedPushGatewayResolver").i("Testing $customUrl") Timber.tag(loggerTag.value).i("Testing $customUrl")
return withContext(coroutineDispatchers.io) { return withContext(coroutineDispatchers.io) {
val api = unifiedPushApiFactory.create(customBase) val api = unifiedPushApiFactory.create(customBase)
try { try {
val discoveryResponse = api.discover() val discoveryResponse = api.discover()
if (discoveryResponse.unifiedpush.gateway == "matrix") { if (discoveryResponse.unifiedpush.gateway == "matrix") {
Timber.tag("DefaultUnifiedPushGatewayResolver").d("The endpoint seems to be a valid UnifiedPush gateway") Timber.tag(loggerTag.value).d("The endpoint seems to be a valid UnifiedPush gateway")
UnifiedPushGatewayResolverResult.Success(customUrl) UnifiedPushGatewayResolverResult.Success(customUrl)
} else { } else {
// The endpoint returned a 200 OK but didn't promote an actual matrix gateway, which means it doesn't have any // The endpoint returned a 200 OK but didn't promote an actual matrix gateway, which means it doesn't have any
Timber.tag("DefaultUnifiedPushGatewayResolver").w("The endpoint does not seem to be a valid UnifiedPush gateway, using fallback") Timber.tag(loggerTag.value).w("The endpoint does not seem to be a valid UnifiedPush gateway, using fallback")
UnifiedPushGatewayResolverResult.NoMatrixGateway UnifiedPushGatewayResolverResult.NoMatrixGateway
} }
} catch (throwable: Throwable) { } catch (throwable: Throwable) {
if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) { if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) {
Timber.tag("DefaultUnifiedPushGatewayResolver").i("Checking for UnifiedPush endpoint yielded 404, using fallback") Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded 404, using fallback")
UnifiedPushGatewayResolverResult.NoMatrixGateway UnifiedPushGatewayResolverResult.NoMatrixGateway
} else { } else {
Timber.tag("DefaultUnifiedPushGatewayResolver").e(throwable, "Error checking for UnifiedPush endpoint") Timber.tag(loggerTag.value).e(throwable, "Error checking for UnifiedPush endpoint")
UnifiedPushGatewayResolverResult.Error(customUrl) UnifiedPushGatewayResolverResult.Error(customUrl)
} }
} }