Merge pull request #7356 from hayaksi1/fix/4008-fediverse-handle-not-email

Do not linkify the server part of a fediverse handle as an email address
This commit is contained in:
Benoit Marty
2026-08-11 14:48:40 +02:00
committed by GitHub
2 changed files with 48 additions and 0 deletions
@@ -47,6 +47,11 @@ object LinkifyHelper {
val start = spannable.getSpanStart(urlSpan)
val end = spannable.getSpanEnd(urlSpan)
if (urlSpan !in oldURLSpans && spannable.isEmailInsideFediverseHandle(urlSpan, start)) {
spannable.removeSpan(urlSpan)
continue
}
// Try to avoid including trailing punctuation in the link.
// Since this might fail in some edge cases, we catch the exception and just use the original end index.
val newEnd = runCatchingExceptions {
@@ -82,6 +87,10 @@ object LinkifyHelper {
return spannable
}
private fun Spannable.isEmailInsideFediverseHandle(urlSpan: URLSpan, start: Int): Boolean {
return urlSpan.url.startsWith("mailto:") && start > 0 && this[start - 1] == '@'
}
private fun adjustLinkifiedUrlSpanEndIndex(spannable: Spannable, start: Int, end: Int): Int {
var end = end
@@ -87,6 +87,45 @@ class LinkifierHelperTest : RobolectricTest() {
assertThat(urlSpans.first().url).isEqualTo("tel:1234")
}
@Test
fun `linkification ignores fediverse handle`() {
val text = "Follow me at @name@server.tld"
val result = LinkifyHelper.linkify(text)
val urlSpans = result.toSpannable().getSpans<URLSpan>()
assertThat(urlSpans).isEmpty()
}
@Test
fun `linkification ignores fediverse handle at the start of the text`() {
val text = "@name@server.tld posted this"
val result = LinkifyHelper.linkify(text)
val urlSpans = result.toSpannable().getSpans<URLSpan>()
assertThat(urlSpans).isEmpty()
}
@Test
fun `linkification finds email next to a fediverse handle`() {
val text = "@name@server.tld and john@doe.com"
val result = LinkifyHelper.linkify(text)
val urlSpans = result.toSpannable().getSpans<URLSpan>()
assertThat(urlSpans.size).isEqualTo(1)
assertThat(urlSpans.first().url).isEqualTo("mailto:john@doe.com")
}
@Test
fun `linkification keeps an existing mailto span preceded by an at sign`() {
val text = buildSpannedString {
append("Mail me @")
inSpans(URLSpan("mailto:john@doe.com")) {
append("john@doe.com")
}
}
val result = LinkifyHelper.linkify(text)
val urlSpans = result.toSpannable().getSpans<URLSpan>()
assertThat(urlSpans.size).isEqualTo(1)
assertThat(urlSpans.first().url).isEqualTo("mailto:john@doe.com")
}
@Test
fun `linkification handles trailing dot`() {
val text = "A url https://matrix.org."