Use SDK API to build room and event permalinks.
This commit is contained in:
parent
7b14a0e4c8
commit
294f1f2d96
9 changed files with 37 additions and 109 deletions
|
|
@ -20,7 +20,6 @@ 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.RoomId
|
||||
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
|
||||
|
|
@ -54,39 +53,9 @@ class DefaultPermalinkBuilder @Inject constructor() : PermalinkBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
override fun permalinkForRoomAlias(roomAlias: String): Result<String> {
|
||||
return if (MatrixPatterns.isRoomAlias(roomAlias)) {
|
||||
Result.success(permalinkForRoomAliasOrId(roomAlias))
|
||||
} else {
|
||||
Result.failure(PermalinkBuilderError.InvalidRoomAlias)
|
||||
}
|
||||
}
|
||||
|
||||
override fun permalinkForRoomId(roomId: RoomId): Result<String> {
|
||||
return if (MatrixPatterns.isRoomId(roomId.value)) {
|
||||
Result.success(permalinkForRoomAliasOrId(roomId.value))
|
||||
} else {
|
||||
Result.failure(PermalinkBuilderError.InvalidRoomId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun permalinkForRoomAliasOrId(value: String): String {
|
||||
val id = escapeId(value)
|
||||
return buildString {
|
||||
append(permalinkBaseUrl)
|
||||
if (!isMatrixTo()) {
|
||||
append(ROOM_PATH)
|
||||
}
|
||||
append(id)
|
||||
}
|
||||
}
|
||||
|
||||
private fun escapeId(value: String) = value.replace("/", "%2F")
|
||||
|
||||
private fun isMatrixTo(): Boolean = permalinkBaseUrl.startsWith(MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL)
|
||||
|
||||
companion object {
|
||||
private const val ROOM_PATH = "room/"
|
||||
private const val USER_PATH = "user/"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package io.element.android.libraries.matrix.impl.room
|
||||
|
||||
import io.element.android.appconfig.MatrixConfiguration
|
||||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.core.coroutine.childScope
|
||||
import io.element.android.libraries.matrix.api.core.EventId
|
||||
|
|
@ -731,17 +730,12 @@ class RustMatrixRoom(
|
|||
)
|
||||
}
|
||||
|
||||
override suspend fun getPermalinkFor(eventId: EventId): Result<String> {
|
||||
// FIXME Use the SDK API once https://github.com/matrix-org/matrix-rust-sdk/issues/3259 has been done
|
||||
// Now use a simple builder
|
||||
return runCatching {
|
||||
buildString {
|
||||
append(MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL)
|
||||
append(roomId.value)
|
||||
append("/")
|
||||
append(eventId.value)
|
||||
}
|
||||
}
|
||||
override suspend fun getPermalink(): Result<String> = runCatching {
|
||||
innerRoom.matrixToPermalink()
|
||||
}
|
||||
|
||||
override suspend fun getPermalinkFor(eventId: EventId): Result<String> = runCatching {
|
||||
innerRoom.matrixToEventPermalink(eventId.value)
|
||||
}
|
||||
|
||||
private fun sendAttachment(files: List<File>, handle: () -> SendAttachmentJoinHandle): Result<MediaUploadHandler> {
|
||||
|
|
|
|||
|
|
@ -18,12 +18,12 @@ 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.RoomId
|
||||
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")
|
||||
|
|
@ -31,13 +31,6 @@ class DefaultPermalinkBuilderTest {
|
|||
}
|
||||
}
|
||||
|
||||
fun `building a permalink for an invalid room id throws when verifying the id`() {
|
||||
assertThrowsInDebug {
|
||||
val roomId = RoomId("some invalid room id")
|
||||
DefaultPermalinkBuilder().permalinkForRoomId(roomId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for an invalid user id returns failure when not verifying the id`() {
|
||||
withReleaseBehavior {
|
||||
|
|
@ -46,35 +39,9 @@ class DefaultPermalinkBuilderTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for an invalid room id returns failure when not verifying the id`() {
|
||||
withReleaseBehavior {
|
||||
val roomId = RoomId("some invalid room id")
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForRoomId(roomId).isFailure).isTrue()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for an invalid room alias returns failure`() {
|
||||
val roomAlias = "an invalid room alias"
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForRoomAlias(roomAlias).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")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for a valid room id returns a matrix-to url`() {
|
||||
val roomId = RoomId("!aBCdEFG1234:matrix.org")
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForRoomId(roomId).getOrNull()).isEqualTo("https://matrix.to/#/!aBCdEFG1234:matrix.org")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `building a permalink for a valid room alias returns a matrix-to url`() {
|
||||
val roomAlias = "#room:matrix.org"
|
||||
assertThat(DefaultPermalinkBuilder().permalinkForRoomAlias(roomAlias).getOrNull()).isEqualTo("https://matrix.to/#/#room:matrix.org")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue