From df794ee41f9af3d48e87071fce7895691293e9e8 Mon Sep 17 00:00:00 2001 From: Arnaud Ringenbach Date: Tue, 15 Mar 2022 11:26:47 +0100 Subject: [PATCH] Move template creation to PermalinkService --- .../session/permalinks/PermalinkService.kt | 21 +++++++++ .../permalinks/DefaultPermalinkService.kt | 8 ++++ .../session/permalinks/PermalinkFactory.kt | 28 ++++++++++- .../session/room/send/pills/TextPillsUtils.kt | 46 ++----------------- 4 files changed, 60 insertions(+), 43 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/permalinks/PermalinkService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/permalinks/PermalinkService.kt index 920dc85c7a..36aec29b8d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/permalinks/PermalinkService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/permalinks/PermalinkService.kt @@ -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: "%2\$s" + * + * @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 } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/DefaultPermalinkService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/DefaultPermalinkService.kt index 144ebb5404..fb46a26751 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/DefaultPermalinkService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/DefaultPermalinkService.kt @@ -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) + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/PermalinkFactory.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/PermalinkFactory.kt index 377b083bd6..affd6c137c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/PermalinkFactory.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/permalinks/PermalinkFactory.kt @@ -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 = "%2\$s" + private const val MENTION_SPAN_TO_MD_TEMPLATE_BEGIN = "[%2\$s](" + private const val MENTION_SPAN_TO_MD_TEMPLATE_END = "%1\$s)" } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/pills/TextPillsUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/pills/TextPillsUtils.kt index 3edae5e26a..427f4d966e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/pills/TextPillsUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/pills/TextPillsUtils.kt @@ -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 = "%2\$s" - private const val MENTION_SPAN_TO_MD_TEMPLATE_BEGIN = "[%2\$s](" - private const val MENTION_SPAN_TO_MD_TEMPLATE_END = "%1\$s)" - } }