Merge pull request #5544 from vector-im/aringenbach/5521_permalink_base_url_mention

Fix mentions using matrix.to rather than client defined permalink base url
This commit is contained in:
aringenbach 2022-03-15 17:28:16 +01:00 committed by GitHub
commit a7639f4424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 10 deletions

1
changelog.d/5521.bugfix Normal file
View file

@ -0,0 +1 @@
Fix mentions using matrix.to rather than client defined permalink base url

View file

@ -60,7 +60,9 @@ class MarkdownParserTest : InstrumentedTest {
applicationFlavor = "TestFlavor",
roomDisplayNameFallbackProvider = TestRoomDisplayNameFallbackProvider()
)
))
),
TestPermalinkService()
)
)
@Test

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.HTML
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.MARKDOWN
class TestPermalinkService : PermalinkService {
override fun createPermalink(event: Event, forceMatrixTo: Boolean): String? {
return null
}
override fun createPermalink(id: String, forceMatrixTo: Boolean): String? {
return ""
}
override fun createPermalink(roomId: String, eventId: String, forceMatrixTo: Boolean): String {
return ""
}
override fun createRoomPermalink(roomId: String, viaServers: List<String>?, forceMatrixTo: Boolean): String? {
return null
}
override fun getLinkedId(url: String): String? {
return null
}
override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return when (type) {
HTML -> "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
MARKDOWN -> "[%2\$s](https://matrix.to/#/%1\$s)"
}
}
}

View file

@ -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"
@ -80,4 +85,15 @@ 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 or Markdown 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>" 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 created template
*/
fun createMentionSpanTemplate(type: SpanTemplateType, forceMatrixTo: Boolean = false): String
}

View file

@ -43,4 +43,8 @@ internal class DefaultPermalinkService @Inject constructor(
override fun getLinkedId(url: String): String? {
return permalinkFactory.getLinkedId(url)
}
override fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return permalinkFactory.createMentionSpanTemplate(type, forceMatrixTo)
}
}

View file

@ -21,7 +21,10 @@ 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.HTML
import org.matrix.android.sdk.api.session.permalinks.PermalinkService.SpanTemplateType.MARKDOWN
import org.matrix.android.sdk.internal.di.UserId
import javax.inject.Inject
@ -105,6 +108,23 @@ internal class PermalinkFactory @Inject constructor(
?.substringBeforeLast("?")
}
fun createMentionSpanTemplate(type: PermalinkService.SpanTemplateType, forceMatrixTo: Boolean): String {
return buildString {
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)
}
when (type) {
HTML -> append(MENTION_SPAN_TO_HTML_TEMPLATE_END)
MARKDOWN -> append(MENTION_SPAN_TO_MD_TEMPLATE_END)
}
}
}
/**
* Escape '/' in id, because it is used as a separator
*
@ -147,5 +167,9 @@ internal class PermalinkFactory @Inject constructor(
private const val ROOM_PATH = "room/"
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

@ -16,6 +16,7 @@
package org.matrix.android.sdk.internal.session.room.send.pills
import android.text.SpannableString
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.session.room.send.MatrixItemSpan
import org.matrix.android.sdk.api.util.MatrixItem
import org.matrix.android.sdk.internal.session.displayname.DisplayNameResolver
@ -28,7 +29,8 @@ import javax.inject.Inject
*/
internal class TextPillsUtils @Inject constructor(
private val mentionLinkSpecComparator: MentionLinkSpecComparator,
private val displayNameResolver: DisplayNameResolver
private val displayNameResolver: DisplayNameResolver,
private val permalinkService: PermalinkService
) {
/**
@ -36,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, MENTION_SPAN_TO_HTML_TEMPLATE)
return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.HTML))
}
/**
@ -44,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, MENTION_SPAN_TO_MD_TEMPLATE)
return transformPills(text, permalinkService.createMentionSpanTemplate(PermalinkService.SpanTemplateType.MARKDOWN))
}
private fun transformPills(text: CharSequence, template: String): String? {
@ -108,10 +110,4 @@ internal class TextPillsUtils @Inject constructor(
i++
}
}
companion object {
private const val MENTION_SPAN_TO_HTML_TEMPLATE = "<a href=\"https://matrix.to/#/%1\$s\">%2\$s</a>"
private const val MENTION_SPAN_TO_MD_TEMPLATE = "[%2\$s](https://matrix.to/#/%1\$s)"
}
}