Skip to content

Commit d292a52

Browse files
committed
Introduce SessionWellknownRetriever and implementation that uses a MatrixClient.
1 parent db64ce3 commit d292a52

File tree

5 files changed

+89
-14
lines changed

5 files changed

+89
-14
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.wellknown.api
9+
10+
interface SessionWellknownRetriever {
11+
suspend fun getWellKnown(): WellKnown?
12+
suspend fun getElementWellKnown(): ElementWellKnown?
13+
}

libraries/wellknown/impl/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
implementation(libs.serialization.json)
2828
implementation(projects.libraries.architecture)
2929
implementation(projects.libraries.core)
30+
implementation(projects.libraries.matrix.api)
3031
implementation(projects.libraries.network)
3132
implementation(projects.libraries.wellknown.api)
3233
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.wellknown.impl
9+
10+
import com.squareup.anvil.annotations.ContributesBinding
11+
import io.element.android.libraries.core.extensions.mapCatchingExceptions
12+
import io.element.android.libraries.di.SessionScope
13+
import io.element.android.libraries.matrix.api.MatrixClient
14+
import io.element.android.libraries.wellknown.api.ElementWellKnown
15+
import io.element.android.libraries.wellknown.api.SessionWellknownRetriever
16+
import io.element.android.libraries.wellknown.api.WellKnown
17+
import kotlinx.serialization.json.Json
18+
import timber.log.Timber
19+
import javax.inject.Inject
20+
21+
@ContributesBinding(SessionScope::class)
22+
class DefaultSessionWellknownRetriever @Inject constructor(
23+
private val matrixClient: MatrixClient,
24+
) : SessionWellknownRetriever {
25+
private val parser by lazy { Json { ignoreUnknownKeys = true } }
26+
private val domain by lazy { matrixClient.userIdServerName() }
27+
28+
override suspend fun getWellKnown(): WellKnown? {
29+
val url = "https://$domain/.well-known/matrix/client"
30+
return matrixClient
31+
.getUrl(url)
32+
.mapCatchingExceptions { String(it) }
33+
.mapCatchingExceptions { parser.decodeFromString(InternalWellKnown.serializer(), it) }
34+
.onFailure { Timber.e(it, "Failed to retrieve .well-known from $domain") }
35+
.map { it.map() }
36+
.getOrNull()
37+
}
38+
39+
override suspend fun getElementWellKnown(): ElementWellKnown? {
40+
val url = "https://$domain/.well-known/element/element.json"
41+
return matrixClient
42+
.getUrl(url)
43+
.mapCatchingExceptions { String(it) }
44+
.mapCatchingExceptions { parser.decodeFromString(InternalElementWellKnown.serializer(), it) }
45+
.onFailure { Timber.e(it, "Failed to retrieve Element .well-known from $domain") }
46+
.map { it.map() }
47+
.getOrNull()
48+
}
49+
}

libraries/wellknown/impl/src/main/kotlin/io/element/android/libraries/wellknown/impl/DefaultWellknownRetriever.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,3 @@ class DefaultWellknownRetriever @Inject constructor(
5353
}
5454
}
5555
}
56-
57-
private fun InternalElementWellKnown.map() = ElementWellKnown(
58-
registrationHelperUrl = registrationHelperUrl,
59-
enforceElementPro = enforceElementPro,
60-
)
61-
62-
private fun InternalWellKnown.map() = WellKnown(
63-
homeServer = homeServer?.map(),
64-
identityServer = identityServer?.map(),
65-
)
66-
67-
private fun InternalWellKnownBaseConfig.map() = WellKnownBaseConfig(
68-
baseURL = baseURL,
69-
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.libraries.wellknown.impl
9+
10+
import io.element.android.libraries.wellknown.api.ElementWellKnown
11+
import io.element.android.libraries.wellknown.api.WellKnown
12+
import io.element.android.libraries.wellknown.api.WellKnownBaseConfig
13+
14+
internal fun InternalElementWellKnown.map() = ElementWellKnown(
15+
registrationHelperUrl = registrationHelperUrl,
16+
enforceElementPro = enforceElementPro,
17+
)
18+
19+
internal fun InternalWellKnown.map() = WellKnown(
20+
homeServer = homeServer?.map(),
21+
identityServer = identityServer?.map(),
22+
)
23+
24+
internal fun InternalWellKnownBaseConfig.map() = WellKnownBaseConfig(
25+
baseURL = baseURL,
26+
)

0 commit comments

Comments
 (0)