diff --git a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/TestPermalinkService.kt b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/TestPermalinkService.kt
index 695024a6b5..3bbb40f66a 100644
--- a/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/TestPermalinkService.kt
+++ b/matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/session/room/send/TestPermalinkService.kt
@@ -18,6 +18,7 @@ package org.matrix.android.sdk.internal.session.room.send
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
+import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.*
class TestPermalinkService : PermalinkService {
override fun createPermalink(event: Event, forceMatrixTo: Boolean): String? {
@@ -40,11 +41,10 @@ class TestPermalinkService : PermalinkService {
return null
}
- override fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
- return "%2\$s"
- }
-
- override fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
- return "[%2\$s](https://matrix.to/#/%1\$s)"
+ override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
+ return when (type) {
+ HTML -> "%2\$s"
+ MARKDOWN -> "[%2\$s](https://matrix.to/#/%1\$s)"
+ }
}
}
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 5666975d37..c139da813a 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
@@ -28,6 +28,11 @@ interface PermalinkService {
const val MATRIX_TO_URL_BASE = "https://matrix.to/#/"
}
+ enum class SpanTemplateType {
+ HTML,
+ MARKDOWN
+ }
+
/**
* Creates a permalink for an event.
* Ex: "https://matrix.to/#/!nbzmcXAqpxBXjAdgoX:matrix.org/$1531497316352799BevdV:matrix.org"
@@ -82,22 +87,13 @@ interface PermalinkService {
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"
+ * Creates a HTML or Markdown mention span template. Can be used to replace a mention with a permalink to mentioned user.
+ * Ex: "%2\$s" or "[%2\$s](https://matrix.to/#/%1\$s)"
*
+ * @param type: type of template to create
* @param forceMatrixTo whether we should force using matrix.to base URL
*
- * @return the HTML template
+ * @return the created 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 Markdown template
- */
- fun createMdMentionSpanTemplate(forceMatrixTo: Boolean = false): String
+ fun createMentionSpanTemplate(type: SpanTemplateType, 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 fb46a26751..196a8c122d 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
@@ -44,11 +44,7 @@ internal class DefaultPermalinkService @Inject constructor(
return permalinkFactory.getLinkedId(url)
}
- override fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
- return permalinkFactory.createHtmlMentionSpanTemplate(forceMatrixTo)
- }
-
- override fun createMdMentionSpanTemplate(forceMatrixTo: Boolean): String {
- return permalinkFactory.createMdMentionSpanTemplate(forceMatrixTo)
+ override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
+ return permalinkFactory.createMentionSpanTemplate(type, 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 affd6c137c..d3cd945032 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
@@ -21,7 +21,9 @@ 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
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.Companion.MATRIX_TO_URL_BASE
+import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.*
import org.matrix.android.sdk.internal.di.UserId
import javax.inject.Inject
@@ -105,25 +107,20 @@ internal class PermalinkFactory @Inject constructor(
?.substringBeforeLast("?")
}
- fun createHtmlMentionSpanTemplate(forceMatrixTo: Boolean): String {
+ fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return buildString {
- append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
+ when (type) {
+ HTML -> append(MENTION_SPAN_TO_HTML_TEMPLATE_BEGIN)
+ MARKDOWN -> append(MENTION_SPAN_TO_MD_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)
+ when (type) {
+ HTML -> append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
+ MARKDOWN -> append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
- append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
}
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 07fdeb7e3e..fa2e0052ab 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
@@ -38,7 +38,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToHtml(text: CharSequence): String? {
- return transformPills(text, permalinkService.createHtmlMentionSpanTemplate())
+ return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.HTML))
}
/**
@@ -46,7 +46,7 @@ internal class TextPillsUtils @Inject constructor(
* @return the transformed String or null if no Span found
*/
fun processSpecialSpansToMarkdown(text: CharSequence): String? {
- return transformPills(text, permalinkService.createMdMentionSpanTemplate())
+ return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.MARKDOWN))
}
private fun transformPills(text: CharSequence, template: String): String? {