Use org.matrix.rustcomponents.sdk.matrixToUserPermalink to build permalinks for user.
This commit is contained in:
parent
294f1f2d96
commit
6b301ac37f
2 changed files with 6 additions and 76 deletions
|
|
@ -17,45 +17,22 @@
|
|||
package io.element.android.libraries.matrix.impl.permalink
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.appconfig.MatrixConfiguration
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.matrix.api.core.MatrixPatterns
|
||||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkBuilder
|
||||
import io.element.android.libraries.matrix.api.permalink.PermalinkBuilderError
|
||||
import org.matrix.rustcomponents.sdk.matrixToUserPermalink
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultPermalinkBuilder @Inject constructor() : PermalinkBuilder {
|
||||
private val permalinkBaseUrl
|
||||
get() = (MatrixConfiguration.clientPermalinkBaseUrl ?: MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL).also {
|
||||
var baseUrl = it
|
||||
if (!baseUrl.endsWith("/")) {
|
||||
baseUrl += "/"
|
||||
}
|
||||
if (!baseUrl.endsWith("/#/")) {
|
||||
baseUrl += "/#/"
|
||||
}
|
||||
}
|
||||
|
||||
override fun permalinkForUser(userId: UserId): Result<String> {
|
||||
return if (MatrixPatterns.isUserId(userId.value)) {
|
||||
val url = buildString {
|
||||
append(permalinkBaseUrl)
|
||||
if (!isMatrixTo()) {
|
||||
append(USER_PATH)
|
||||
}
|
||||
append(userId.value)
|
||||
}
|
||||
Result.success(url)
|
||||
} else {
|
||||
Result.failure(PermalinkBuilderError.InvalidUserId)
|
||||
if (!MatrixPatterns.isUserId(userId.value)) {
|
||||
return Result.failure(PermalinkBuilderError.InvalidUserId)
|
||||
}
|
||||
return runCatching {
|
||||
matrixToUserPermalink(userId.value)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isMatrixTo(): Boolean = permalinkBaseUrl.startsWith(MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL)
|
||||
|
||||
companion object {
|
||||
private const val USER_PATH = "user/"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl.permalink
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.androidutils.metadata.withReleaseBehavior
|
||||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import io.element.android.tests.testutils.assertThrowsInDebug
|
||||
import org.junit.Test
|
||||
|
||||
class DefaultPermalinkBuilderTest {
|
||||
@Test
|
||||
fun `building a permalink for an invalid user id throws when verifying the id`() {
|
||||
assertThrowsInDebug {
|
||||
val userId = UserId("some invalid user id")
|
||||
DefaultPermalinkBuilder().permalinkForUser(userId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for an invalid user id returns failure when not verifying the id`() {
|
||||
withReleaseBehavior {
|
||||
val userId = UserId("some invalid user id")
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForUser(userId).isFailure).isTrue()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for a valid user id returns a matrix-to url`() {
|
||||
val userId = UserId("@user:matrix.org")
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForUser(userId).getOrNull()).isEqualTo("https://matrix.to/#/@user:matrix.org")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue