mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-17 19:58:57 +03:00
Permalink: use client base url if any
This commit is contained in:
parent
0d344fde03
commit
a73f0a9fa8
4 changed files with 99 additions and 31 deletions
|
@ -19,7 +19,7 @@ package org.matrix.android.sdk.api.session.permalinks
|
|||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
|
||||
/**
|
||||
* Useful methods to create Matrix permalink (matrix.to links).
|
||||
* Useful methods to create permalink (like matrix.to links or client permalinks).
|
||||
*/
|
||||
interface PermalinkService {
|
||||
|
||||
|
@ -32,10 +32,11 @@ interface PermalinkService {
|
|||
* Ex: "https://matrix.to/#/!nbzmcXAqpxBXjAdgoX:matrix.org/$1531497316352799BevdV:matrix.org"
|
||||
*
|
||||
* @param event the event
|
||||
* @param forceMatrixTo whether we should force using matrix.to base URL
|
||||
*
|
||||
* @return the permalink, or null in case of error
|
||||
*/
|
||||
fun createPermalink(event: Event): String?
|
||||
fun createPermalink(event: Event, forceMatrixTo: Boolean = false): String?
|
||||
|
||||
/**
|
||||
* Creates a permalink for an id (can be a user Id, etc.).
|
||||
|
@ -43,18 +44,21 @@ interface PermalinkService {
|
|||
* Ex: "https://matrix.to/#/@benoit:matrix.org"
|
||||
*
|
||||
* @param id the id
|
||||
* @param forceMatrixTo whether we should force using matrix.to base URL
|
||||
*
|
||||
* @return the permalink, or null in case of error
|
||||
*/
|
||||
fun createPermalink(id: String): String?
|
||||
fun createPermalink(id: String, forceMatrixTo: Boolean = false): String?
|
||||
|
||||
/**
|
||||
* Creates a permalink for a roomId, including the via parameters
|
||||
*
|
||||
* @param roomId the room id
|
||||
* @param forceMatrixTo whether we should force using matrix.to base URL
|
||||
*
|
||||
* @return the permalink, or null in case of error
|
||||
*/
|
||||
fun createRoomPermalink(roomId: String, viaServers: List<String>? = null): String?
|
||||
fun createRoomPermalink(roomId: String, viaServers: List<String>? = null, forceMatrixTo: Boolean = false): String?
|
||||
|
||||
/**
|
||||
* Creates a permalink for an event. If you have an event you can use [createPermalink]
|
||||
|
@ -62,10 +66,11 @@ interface PermalinkService {
|
|||
*
|
||||
* @param roomId the id of the room
|
||||
* @param eventId the id of the event
|
||||
* @param forceMatrixTo whether we should force using matrix.to base URL
|
||||
*
|
||||
* @return the permalink
|
||||
*/
|
||||
fun createPermalink(roomId: String, eventId: String): String
|
||||
fun createPermalink(roomId: String, eventId: String, forceMatrixTo: Boolean = false): String
|
||||
|
||||
/**
|
||||
* Extract the linked id from the universal link
|
||||
|
|
|
@ -24,20 +24,20 @@ internal class DefaultPermalinkService @Inject constructor(
|
|||
private val permalinkFactory: PermalinkFactory
|
||||
) : PermalinkService {
|
||||
|
||||
override fun createPermalink(event: Event): String? {
|
||||
return permalinkFactory.createPermalink(event)
|
||||
override fun createPermalink(event: Event, forceMatrixTo: Boolean): String? {
|
||||
return permalinkFactory.createPermalink(event, forceMatrixTo)
|
||||
}
|
||||
|
||||
override fun createPermalink(id: String): String? {
|
||||
return permalinkFactory.createPermalink(id)
|
||||
override fun createPermalink(id: String, forceMatrixTo: Boolean): String? {
|
||||
return permalinkFactory.createPermalink(id, forceMatrixTo)
|
||||
}
|
||||
|
||||
override fun createRoomPermalink(roomId: String, viaServers: List<String>?): String? {
|
||||
return permalinkFactory.createRoomPermalink(roomId, viaServers)
|
||||
override fun createRoomPermalink(roomId: String, viaServers: List<String>?, forceMatrixTo: Boolean): String? {
|
||||
return permalinkFactory.createRoomPermalink(roomId, viaServers, forceMatrixTo)
|
||||
}
|
||||
|
||||
override fun createPermalink(roomId: String, eventId: String): String {
|
||||
return permalinkFactory.createPermalink(roomId, eventId)
|
||||
override fun createPermalink(roomId: String, eventId: String, forceMatrixTo: Boolean): String {
|
||||
return permalinkFactory.createPermalink(roomId, eventId, forceMatrixTo)
|
||||
}
|
||||
|
||||
override fun getLinkedId(url: String): String? {
|
||||
|
|
|
@ -16,7 +16,11 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.session.permalinks
|
||||
|
||||
import org.matrix.android.sdk.api.MatrixConfiguration
|
||||
import org.matrix.android.sdk.api.MatrixPatterns
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkData
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkParser
|
||||
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.Companion.MATRIX_TO_URL_BASE
|
||||
import org.matrix.android.sdk.internal.di.UserId
|
||||
import javax.inject.Inject
|
||||
|
@ -24,28 +28,44 @@ import javax.inject.Inject
|
|||
internal class PermalinkFactory @Inject constructor(
|
||||
@UserId
|
||||
private val userId: String,
|
||||
private val viaParameterFinder: ViaParameterFinder
|
||||
private val viaParameterFinder: ViaParameterFinder,
|
||||
private val matrixConfiguration: MatrixConfiguration
|
||||
) {
|
||||
|
||||
fun createPermalink(event: Event): String? {
|
||||
fun createPermalink(event: Event, forceMatrixTo: Boolean): String? {
|
||||
if (event.roomId.isNullOrEmpty() || event.eventId.isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
return createPermalink(event.roomId, event.eventId)
|
||||
return createPermalink(event.roomId, event.eventId, forceMatrixTo)
|
||||
}
|
||||
|
||||
fun createPermalink(id: String): String? {
|
||||
return if (id.isEmpty()) {
|
||||
null
|
||||
} else MATRIX_TO_URL_BASE + escape(id)
|
||||
fun createPermalink(id: String, forceMatrixTo: Boolean): String? {
|
||||
return when {
|
||||
id.isEmpty() -> null
|
||||
!useClientFormat(forceMatrixTo) -> MATRIX_TO_URL_BASE + escape(id)
|
||||
else -> {
|
||||
buildString {
|
||||
append(matrixConfiguration.clientPermalinkBaseUrl)
|
||||
when {
|
||||
MatrixPatterns.isRoomId(id) || MatrixPatterns.isRoomAlias(id) -> append(ROOM_PATH)
|
||||
MatrixPatterns.isUserId(id) -> append(USER_PATH)
|
||||
MatrixPatterns.isGroupId(id) -> append(GROUP_PATH)
|
||||
}
|
||||
append(escape(id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createRoomPermalink(roomId: String, via: List<String>? = null): String? {
|
||||
fun createRoomPermalink(roomId: String, via: List<String>? = null, forceMatrixTo: Boolean): String? {
|
||||
return if (roomId.isEmpty()) {
|
||||
null
|
||||
} else {
|
||||
buildString {
|
||||
append(MATRIX_TO_URL_BASE)
|
||||
append(baseUrl(forceMatrixTo))
|
||||
if (useClientFormat(forceMatrixTo)) {
|
||||
append(ROOM_PATH)
|
||||
}
|
||||
append(escape(roomId))
|
||||
append(
|
||||
via?.takeIf { it.isNotEmpty() }?.let { viaParameterFinder.asUrlViaParameters(it) }
|
||||
|
@ -55,14 +75,33 @@ internal class PermalinkFactory @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun createPermalink(roomId: String, eventId: String): String {
|
||||
return MATRIX_TO_URL_BASE + escape(roomId) + "/" + escape(eventId) + viaParameterFinder.computeViaParams(userId, roomId)
|
||||
fun createPermalink(roomId: String, eventId: String, forceMatrixTo: Boolean): String {
|
||||
return buildString {
|
||||
append(baseUrl(forceMatrixTo))
|
||||
if (useClientFormat(forceMatrixTo)) {
|
||||
append(ROOM_PATH)
|
||||
}
|
||||
append(escape(roomId))
|
||||
append("/")
|
||||
append(escape(eventId))
|
||||
append(viaParameterFinder.computeViaParams(userId, roomId))
|
||||
}
|
||||
}
|
||||
|
||||
fun getLinkedId(url: String): String? {
|
||||
return url
|
||||
.takeIf { it.startsWith(MATRIX_TO_URL_BASE) }
|
||||
?.substring(MATRIX_TO_URL_BASE.length)
|
||||
val clientBaseUrl = matrixConfiguration.clientPermalinkBaseUrl
|
||||
return when {
|
||||
url.startsWith(MATRIX_TO_URL_BASE) -> url.substring(MATRIX_TO_URL_BASE.length)
|
||||
clientBaseUrl != null && url.startsWith(clientBaseUrl) -> {
|
||||
when (PermalinkParser.parse(url)) {
|
||||
is PermalinkData.GroupLink -> url.substring(clientBaseUrl.length + GROUP_PATH.length)
|
||||
is PermalinkData.RoomLink -> url.substring(clientBaseUrl.length + ROOM_PATH.length)
|
||||
is PermalinkData.UserLink -> url.substring(clientBaseUrl.length + USER_PATH.length)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
?.substringBeforeLast("?")
|
||||
}
|
||||
|
||||
|
@ -85,4 +124,28 @@ internal class PermalinkFactory @Inject constructor(
|
|||
private fun unescape(id: String): String {
|
||||
return id.replace("%2F", "/")
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the permalink base URL according to the potential one in [MatrixConfiguration.clientPermalinkBaseUrl]
|
||||
* and the [forceMatrixTo] parameter.
|
||||
*
|
||||
* @param forceMatrixTo whether we should force using matrix.to base URL.
|
||||
*
|
||||
* @return the permalink base URL.
|
||||
*/
|
||||
private fun baseUrl(forceMatrixTo: Boolean): String {
|
||||
return matrixConfiguration.clientPermalinkBaseUrl
|
||||
?.takeUnless { forceMatrixTo }
|
||||
?: MATRIX_TO_URL_BASE
|
||||
}
|
||||
|
||||
private fun useClientFormat(forceMatrixTo: Boolean): Boolean {
|
||||
return !forceMatrixTo && matrixConfiguration.clientPermalinkBaseUrl != null
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val ROOM_PATH = "room/"
|
||||
private const val USER_PATH = "user/"
|
||||
private const val GROUP_PATH = "group/"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -165,8 +165,8 @@ internal class LocalEchoEventFactory @Inject constructor(
|
|||
newBodyAutoMarkdown: Boolean,
|
||||
msgType: String,
|
||||
compatibilityText: String): Event {
|
||||
val permalink = permalinkFactory.createPermalink(roomId, originalEvent.root.eventId ?: "")
|
||||
val userLink = originalEvent.root.senderId?.let { permalinkFactory.createPermalink(it) } ?: ""
|
||||
val permalink = permalinkFactory.createPermalink(roomId, originalEvent.root.eventId ?: "", false)
|
||||
val userLink = originalEvent.root.senderId?.let { permalinkFactory.createPermalink(it, false) } ?: ""
|
||||
|
||||
val body = bodyForReply(originalEvent.getLastMessageContent(), originalEvent.isReply())
|
||||
val replyFormatted = REPLY_PATTERN.format(
|
||||
|
@ -350,9 +350,9 @@ internal class LocalEchoEventFactory @Inject constructor(
|
|||
autoMarkdown: Boolean): Event? {
|
||||
// Fallbacks and event representation
|
||||
// TODO Add error/warning logs when any of this is null
|
||||
val permalink = permalinkFactory.createPermalink(eventReplied.root) ?: return null
|
||||
val permalink = permalinkFactory.createPermalink(eventReplied.root, false) ?: return null
|
||||
val userId = eventReplied.root.senderId ?: return null
|
||||
val userLink = permalinkFactory.createPermalink(userId) ?: return null
|
||||
val userLink = permalinkFactory.createPermalink(userId, false) ?: return null
|
||||
|
||||
val body = bodyForReply(eventReplied.getLastMessageContent(), eventReplied.isReply())
|
||||
val replyFormatted = REPLY_PATTERN.format(
|
||||
|
|
Loading…
Add table
Reference in a new issue