|
17 | 17 | package io.element.android.libraries.matrix.impl.permalink |
18 | 18 |
|
19 | 19 | import com.squareup.anvil.annotations.ContributesBinding |
20 | | -import io.element.android.appconfig.MatrixConfiguration |
21 | 20 | import io.element.android.libraries.di.AppScope |
22 | 21 | import io.element.android.libraries.matrix.api.core.MatrixPatterns |
23 | 22 | import io.element.android.libraries.matrix.api.core.UserId |
24 | 23 | import io.element.android.libraries.matrix.api.permalink.PermalinkBuilder |
25 | 24 | import io.element.android.libraries.matrix.api.permalink.PermalinkBuilderError |
| 25 | +import org.matrix.rustcomponents.sdk.matrixToUserPermalink |
26 | 26 | import javax.inject.Inject |
27 | 27 |
|
28 | 28 | @ContributesBinding(AppScope::class) |
29 | 29 | class DefaultPermalinkBuilder @Inject constructor() : PermalinkBuilder { |
30 | | - private val permalinkBaseUrl |
31 | | - get() = (MatrixConfiguration.clientPermalinkBaseUrl ?: MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL).also { |
32 | | - var baseUrl = it |
33 | | - if (!baseUrl.endsWith("/")) { |
34 | | - baseUrl += "/" |
35 | | - } |
36 | | - if (!baseUrl.endsWith("/#/")) { |
37 | | - baseUrl += "/#/" |
38 | | - } |
39 | | - } |
40 | | - |
41 | 30 | override fun permalinkForUser(userId: UserId): Result<String> { |
42 | | - return if (MatrixPatterns.isUserId(userId.value)) { |
43 | | - val url = buildString { |
44 | | - append(permalinkBaseUrl) |
45 | | - if (!isMatrixTo()) { |
46 | | - append(USER_PATH) |
47 | | - } |
48 | | - append(userId.value) |
49 | | - } |
50 | | - Result.success(url) |
51 | | - } else { |
52 | | - Result.failure(PermalinkBuilderError.InvalidUserId) |
| 31 | + if (!MatrixPatterns.isUserId(userId.value)) { |
| 32 | + return Result.failure(PermalinkBuilderError.InvalidUserId) |
| 33 | + } |
| 34 | + return runCatching { |
| 35 | + matrixToUserPermalink(userId.value) |
53 | 36 | } |
54 | | - } |
55 | | - |
56 | | - private fun isMatrixTo(): Boolean = permalinkBaseUrl.startsWith(MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL) |
57 | | - |
58 | | - companion object { |
59 | | - private const val USER_PATH = "user/" |
60 | 37 | } |
61 | 38 | } |
0 commit comments