From 7bc40b4b03a1d9dc390d327253aa82ee71bf3ea4 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Sat, 4 Jan 2025 19:13:52 +0100 Subject: [PATCH] Fix comparison of nonnormalized latin tags (#4845) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mastodon "normalizes" tag text for latin characters when storing tag names, so the hashtag object it sends for `#Über` has the name `uber`. With the new trailing hashtag bar, this was causing nonascii hashtags from the post content to be duplicated with their normalized versions, e.g. `#Über #uber`. This change ensures that we're always comparing normalized tag names. --- .../keylesspalace/tusky/util/LinkHelper.kt | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt index e26558284b..3157919e1b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt +++ b/app/src/main/java/com/keylesspalace/tusky/util/LinkHelper.kt @@ -93,7 +93,7 @@ fun setClickableText( getSpans(0, endOfContent, URLSpan::class.java).forEach { span -> val start = getSpanStart(span) if (get(start) == '#') { - inlineHashtags.add(subSequence(start + 1, getSpanEnd(span))) + inlineHashtags.add(normalizeToASCII(subSequence(start + 1, getSpanEnd(span)))) } setClickableText(span, this, mentions, tags, listener) } @@ -127,7 +127,11 @@ private fun buildTrailingHashtagText(tagsFromServer: List?, trailingHas // we apply the tags scraped from the content first to preserve the casing // (tags from the server are often downcased) val additionalTags = tagsFromServer?.let { - it.filter { serverTag -> trailingHashtagsFromContent.none { serverTag.name.equals(it.name, ignoreCase = true) } } + it.filter { serverTag -> + trailingHashtagsFromContent.none { + serverTag.name.equals(normalizeToASCII(it.name), ignoreCase = true) + } + } } ?: emptyList() appendTags(trailingHashtagsFromContent.plus(additionalTags), listener) } @@ -140,8 +144,7 @@ private fun buildTrailingHashtagText(tagsFromServer: List?, trailingHas */ private fun SpannableStringBuilder.appendTags(tags: List, listener: LinkListener) { tags.forEachIndexed { index, tag -> - val text = "#${tag.name}" - append(text, getCustomSpanForTag(text, tags, URLSpan(tag.url), listener), 0) + append("#${tag.name}", getCustomSpanForTag(normalizeToASCII(tag.name), URLSpan(tag.url), listener), 0) if (index != tags.lastIndex) { append(" ") } @@ -276,12 +279,14 @@ private fun getCustomSpanForTag( tags: List?, span: URLSpan, listener: LinkListener -): ClickableSpan? { - return getTagName(text, tags)?.let { - object : NoUnderlineURLSpan(span.url) { - override fun onClick(view: View) = listener.onViewTag(it) - } - } +) = getTagName(text, tags)?.let { getCustomSpanForTag(it, span, listener) } + +private fun getCustomSpanForTag( + tagName: String, + span: URLSpan, + listener: LinkListener +) = object : NoUnderlineURLSpan(span.url) { + override fun onClick(view: View) = listener.onViewTag(tagName) } private fun getCustomSpanForMention(