Move template creation to PermalinkService

This commit is contained in:
Arnaud Ringenbach 2022-03-15 11:26:47 +01:00
parent 4cf820cb12
commit df794ee41f
4 changed files with 60 additions and 43 deletions

View File

@ -17,6 +17,7 @@
package org.matrix.android.sdk.api.session.permalinks
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.internal.session.permalinks.PermalinkFactory
/**
* Useful methods to create permalink (like matrix.to links or client permalinks).
@ -80,4 +81,24 @@ interface PermalinkService {
* @return the id from the url, ex: "@benoit:matrix.org", or null if the url is not a permalink
*/
fun getLinkedId(url: String): String?
/**
* Creates a HTML mention span template. Can be used to replace a mention with a permalink to mentioned user.
* Ex: "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
*
* @param forceMatrixTo whether we should force using matrix.to base URL
*
* @return the HTML template
*/
fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean = false): String
/**
* Creates a Markdown mention span template. Can be used to replace a mention with a permalink to mentioned user.
* Ex: "[%2\$s](https://matrix.to/#/%1\$s)"
*
* @param forceMatrixTo whether we should force using matrix.to base URL
*
* @return the HTML template
*/
fun createMdMentionSpanTemplate(forceMatrixTo: Boolean = false): String
}

View File

@ -43,4 +43,12 @@ internal class DefaultPermalinkService @Inject constructor(
override fun getLinkedId(url: String): String? {
return permalinkFactory.getLinkedId(url)
}
override fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
return permalinkFactory.createHtmlMentionSpanTemplate(forceMatrixTo)
}
override fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return permalinkFactory.createMdMentionSpanTemplate(forceMatrixTo)
}
}

View File

@ -105,6 +105,28 @@ internal class PermalinkFactory @Inject constructor(
?.substringBeforeLast("?")
}
fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(USER_PATH)
}
append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
}
}
fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_MD_TEMPLATE_BEGIN)
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(USER_PATH)
}
append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
}
/**
* Escape '/' in id, because it is used as a separator
*
@ -145,7 +167,11 @@ internal class PermalinkFactory @Inject constructor(
companion object {
private const val ROOM_PATH = "room/"
const val USER_PATH = "user/"
private const val USER_PATH = "user/"
private const val GROUP_PATH = "group/"
private const val MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN = "<a href=\""
private const val MENTION_SPAN_TO_HTML_TEMPLATE_END = "%1\$s\">%2\$s</a>"
private const val MENTION_SPAN_TO_MD_TEMPLATE_BEGIN = "[%2\$s]("
private const val MENTION_SPAN_TO_MD_TEMPLATE_END = "%1\$s)"
}
}

View File

@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.session.room.send.pills
import android.text.SpannableString
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.Companion.MATRIX_TO_URL_BASE
import org.matrix.android.sdk.api.session.room.send.MatrixItemSpan
import org.matrix.android.sdk.api.util.MatrixItem
@ -32,7 +33,7 @@ import javax.inject.Inject
internal class TextPillsUtils @Inject constructor(
private val mentionLinkSpecComparator: MentionLinkSpecComparator,
private val displayNameResolver: DisplayNameResolver,
private val matrixConfiguration: MatrixConfiguration
private val permalinkService: PermalinkService
) {
/**
@ -40,7 +41,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToHtml(text: CharSequence): String? {
return transformPills(text, createHtmlMentionSpanTemplate(forceMatrixTo = false))
return transformPills(text, permalinkService.createHtmlMentionSpanTemplate())
}
/**
@ -48,7 +49,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToMarkdown(text: CharSequence): String? {
return transformPills(text, createMdMentionSpanTemplate(forceMatrixTo = false))
return transformPills(text, permalinkService.createMdMentionSpanTemplate())
}
private fun transformPills(text: CharSequence, template: String): String? {
@ -112,43 +113,4 @@ internal class TextPillsUtils @Inject constructor(
i++
}
}
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
}
private fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(PermalinkFactory.USER_PATH)
}
append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
}
}
private fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
return buildString {
append(MENTION_SPAN_TO_MD_TEMPLATE_BEGIN)
append(baseUrl(forceMatrixTo))
if (useClientFormat(forceMatrixTo)) {
append(PermalinkFactory.USER_PATH)
}
append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
}
companion object {
private const val MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN = "<a href=\""
private const val MENTION_SPAN_TO_HTML_TEMPLATE_END = "%1\$s\">%2\$s</a>"
private const val MENTION_SPAN_TO_MD_TEMPLATE_BEGIN = "[%2\$s]("
private const val MENTION_SPAN_TO_MD_TEMPLATE_END = "%1\$s)"
}
}