From a42bf86a1e64dd66a4b6b8a82d52b39e4c9bd467 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Thu, 1 Aug 2024 19:32:53 +0200 Subject: [PATCH] feat: display ScheduledStatus rendered Fakes the highlighting and formatting of ScheduledStatus by injecting the correct HTML tags. Fixes https://github.com/LucasGGamerM/moshidon/issues/478. --- .../ScheduledStatusListFragment.java | 2 +- .../android/model/ScheduledStatus.java | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ScheduledStatusListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ScheduledStatusListFragment.java index a8001f59b..e254297e1 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ScheduledStatusListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ScheduledStatusListFragment.java @@ -85,7 +85,7 @@ public class ScheduledStatusListFragment extends BaseStatusListFragment buildDisplayItems(ScheduledStatus s) { - return StatusDisplayItem.buildItems(this, s.toStatus(), accountID, s, knownAccounts, null, + return StatusDisplayItem.buildItems(this, s.toFormattedStatus(accountID), accountID, s, knownAccounts, null, StatusDisplayItem.FLAG_NO_EMOJI_REACTIONS | StatusDisplayItem.FLAG_NO_FOOTER | StatusDisplayItem.FLAG_NO_TRANSLATE); diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/ScheduledStatus.java b/mastodon/src/main/java/org/joinmastodon/android/model/ScheduledStatus.java index 3686b6cd1..b16691376 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/ScheduledStatus.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/ScheduledStatus.java @@ -1,17 +1,27 @@ package org.joinmastodon.android.model; +import android.util.Patterns; + +import androidx.annotation.NonNull; + import org.joinmastodon.android.api.ObjectValidationException; import org.joinmastodon.android.api.RequiredField; +import org.joinmastodon.android.api.session.AccountSession; +import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.model.Poll.Option; import org.parceler.Parcel; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; @Parcel public class ScheduledStatus extends BaseModel implements DisplayItemsParent{ + private static final Pattern HIGHLIGHT_PATTER=Pattern.compile("(?"); + if(!s.content.contains("@") && !s.content.contains("#") && !s.content.contains(":")) + return s; + + StringBuffer sb=new StringBuffer(); + Matcher matcher=HIGHLIGHT_PATTER.matcher(s.content); + + // I'm sure this will cause problems at some point... + while(matcher.find()){ + String content=matcher.group(); + String href=""; + // add relevant links, so on-click actions work + // hashtags are done by the parser + if(content.startsWith("@")) + href=" href=\""+formatMention(content, self.domain)+"\" class=\"u-url mention\""; + else if(content.startsWith("https://")) + href=" href=\""+content+"\""; + + matcher.appendReplacement(sb, ""+content+""); + } + matcher.appendTail(sb); + s.content=sb.toString(); + return s; + } + + /** + * Converts a string mention into a URL of the account. + * @param mention Mention in the form a of user name with an optional instance URL + * @param instanceURL URL of the home instance of the user + * @return Formatted HTML or the mention + */ + @NonNull + private static String formatMention(@NonNull String mention, @NonNull String instanceURL){ + String[] parts=mention.split("@"); + if(parts.length>1){ + String username=parts[1]; + String domain=parts.length==3 ? parts[2] : instanceURL; + return "https://"+domain+"/@"+username; + } + return mention; + } }