From 34a1ae16d8f4e3e20da9937827abeb4e7a50b094 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 11 Dec 2022 17:09:57 +0100 Subject: [PATCH] Fix issue #621 - Mentions in bio does not open the correct account --- .../android/helper/SpannableHelper.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java index 9508f750..0d5b7bf3 100644 --- a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java @@ -53,6 +53,11 @@ import androidx.preference.PreferenceManager; import com.bumptech.glide.Glide; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + import java.io.IOException; import java.lang.ref.WeakReference; import java.net.MalformedURLException; @@ -127,6 +132,19 @@ public class SpannableHelper { if (text == null) { return null; } + Document htmlContent = Jsoup.parse(text); + Elements mentionElements = htmlContent.select("a.mention"); + //We keep a reference to mentions + HashMap mentionsMap = new HashMap<>(); + if (mentionElements.size() > 0) { + for (int i = 0; i < mentionElements.size(); i++) { + Element mentionElement = mentionElements.get(i); + String href = mentionElement.attr("href"); + String mention = mentionElement.text(); + mentionsMap.put(mention, href); + } + } + text = text.replaceAll("((<\\s?p\\s?>|<\\s?br\\s?\\/?>)>(((?!([<])).)*))", "$2
$3
"); Pattern imgPattern = Pattern.compile("]*src=\"([^\"]+)\"[^>]*>"); Matcher matcherImg = imgPattern.matcher(text); @@ -178,7 +196,7 @@ public class SpannableHelper { content.removeSpan(span); } //Make tags, mentions, groups - interaction(context, content, status, mentionList, forceMentions); + interaction(context, content, status, mentionList, forceMentions, mentionsMap); //Make all links linkify(context, content, urlDetails); linkifyURL(context, content, urlDetails); @@ -759,7 +777,7 @@ public class SpannableHelper { } } - private static void interaction(Context context, Spannable content, Status status, List mentions, boolean forceMentions) { + private static void interaction(Context context, Spannable content, Status status, List mentions, boolean forceMentions, HashMap mentionsMap) { // --- For all patterns defined in Helper class --- for (Map.Entry entry : Helper.patternHashMap.entrySet()) { Helper.PatternType patternType = entry.getKey(); @@ -837,8 +855,9 @@ public class SpannableHelper { intent = new Intent(context, ProfileActivity.class); b = new Bundle(); Mention targetedMention = null; + String acct = null; HashMap countUsername = new HashMap<>(); - + //Mentions is retrieved with associated Mentions array if (mentions != null) { for (Mention mention : mentions) { Integer count = countUsername.get(mention.username); @@ -861,11 +880,19 @@ public class SpannableHelper { break; } } + } else if (mentionsMap.containsKey(word.trim())) {//Mentions will be find through its URL + URL url; + try { + url = new URL(mentionsMap.get(word.trim())); + acct = word.trim() + "@" + url.getHost(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } } if (targetedMention != null) { b.putString(Helper.ARG_USER_ID, targetedMention.id); } else { - b.putString(Helper.ARG_MENTION, word.trim()); + b.putString(Helper.ARG_MENTION, acct != null ? acct : word.trim()); } intent.putExtras(b);