Introduce SessionWellknownRetriever and implementation that uses a MatrixClient.
This commit is contained in:
parent
dbe5bb767b
commit
76849c4374
5 changed files with 89 additions and 14 deletions
|
|
@ -0,0 +1,13 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.wellknown.api
|
||||||
|
|
||||||
|
interface SessionWellknownRetriever {
|
||||||
|
suspend fun getWellKnown(): WellKnown?
|
||||||
|
suspend fun getElementWellKnown(): ElementWellKnown?
|
||||||
|
}
|
||||||
|
|
@ -27,6 +27,7 @@ dependencies {
|
||||||
implementation(libs.serialization.json)
|
implementation(libs.serialization.json)
|
||||||
implementation(projects.libraries.architecture)
|
implementation(projects.libraries.architecture)
|
||||||
implementation(projects.libraries.core)
|
implementation(projects.libraries.core)
|
||||||
|
implementation(projects.libraries.matrix.api)
|
||||||
implementation(projects.libraries.network)
|
implementation(projects.libraries.network)
|
||||||
implementation(projects.libraries.wellknown.api)
|
implementation(projects.libraries.wellknown.api)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.wellknown.impl
|
||||||
|
|
||||||
|
import com.squareup.anvil.annotations.ContributesBinding
|
||||||
|
import io.element.android.libraries.core.extensions.mapCatchingExceptions
|
||||||
|
import io.element.android.libraries.di.SessionScope
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
|
import io.element.android.libraries.wellknown.api.ElementWellKnown
|
||||||
|
import io.element.android.libraries.wellknown.api.SessionWellknownRetriever
|
||||||
|
import io.element.android.libraries.wellknown.api.WellKnown
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import timber.log.Timber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@ContributesBinding(SessionScope::class)
|
||||||
|
class DefaultSessionWellknownRetriever @Inject constructor(
|
||||||
|
private val matrixClient: MatrixClient,
|
||||||
|
) : SessionWellknownRetriever {
|
||||||
|
private val parser by lazy { Json { ignoreUnknownKeys = true } }
|
||||||
|
private val domain by lazy { matrixClient.userIdServerName() }
|
||||||
|
|
||||||
|
override suspend fun getWellKnown(): WellKnown? {
|
||||||
|
val url = "https://$domain/.well-known/matrix/client"
|
||||||
|
return matrixClient
|
||||||
|
.getUrl(url)
|
||||||
|
.mapCatchingExceptions { String(it) }
|
||||||
|
.mapCatchingExceptions { parser.decodeFromString(InternalWellKnown.serializer(), it) }
|
||||||
|
.onFailure { Timber.e(it, "Failed to retrieve .well-known from $domain") }
|
||||||
|
.map { it.map() }
|
||||||
|
.getOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getElementWellKnown(): ElementWellKnown? {
|
||||||
|
val url = "https://$domain/.well-known/element/element.json"
|
||||||
|
return matrixClient
|
||||||
|
.getUrl(url)
|
||||||
|
.mapCatchingExceptions { String(it) }
|
||||||
|
.mapCatchingExceptions { parser.decodeFromString(InternalElementWellKnown.serializer(), it) }
|
||||||
|
.onFailure { Timber.e(it, "Failed to retrieve Element .well-known from $domain") }
|
||||||
|
.map { it.map() }
|
||||||
|
.getOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -53,17 +53,3 @@ class DefaultWellknownRetriever @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun InternalElementWellKnown.map() = ElementWellKnown(
|
|
||||||
registrationHelperUrl = registrationHelperUrl,
|
|
||||||
enforceElementPro = enforceElementPro,
|
|
||||||
)
|
|
||||||
|
|
||||||
private fun InternalWellKnown.map() = WellKnown(
|
|
||||||
homeServer = homeServer?.map(),
|
|
||||||
identityServer = identityServer?.map(),
|
|
||||||
)
|
|
||||||
|
|
||||||
private fun InternalWellKnownBaseConfig.map() = WellKnownBaseConfig(
|
|
||||||
baseURL = baseURL,
|
|
||||||
)
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.wellknown.impl
|
||||||
|
|
||||||
|
import io.element.android.libraries.wellknown.api.ElementWellKnown
|
||||||
|
import io.element.android.libraries.wellknown.api.WellKnown
|
||||||
|
import io.element.android.libraries.wellknown.api.WellKnownBaseConfig
|
||||||
|
|
||||||
|
internal fun InternalElementWellKnown.map() = ElementWellKnown(
|
||||||
|
registrationHelperUrl = registrationHelperUrl,
|
||||||
|
enforceElementPro = enforceElementPro,
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun InternalWellKnown.map() = WellKnown(
|
||||||
|
homeServer = homeServer?.map(),
|
||||||
|
identityServer = identityServer?.map(),
|
||||||
|
)
|
||||||
|
|
||||||
|
internal fun InternalWellKnownBaseConfig.map() = WellKnownBaseConfig(
|
||||||
|
baseURL = baseURL,
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue