Skip to content

Commit

Permalink
feat(LinkCard): open redirected URL
Browse files Browse the repository at this point in the history
  • Loading branch information
FineFindus committed Jun 27, 2024
1 parent e9f28cf commit a8164e0
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

Expand All @@ -19,15 +18,13 @@
import org.joinmastodon.android.ui.OutlineProviders;
import org.joinmastodon.android.ui.drawables.BlurhashCrossfadeDrawable;
import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.text.LinkSpan;
import org.joinmastodon.android.ui.utils.UiUtils;

import java.util.regex.Matcher;

import me.grishka.appkit.imageloader.ImageLoaderViewHolder;
import me.grishka.appkit.imageloader.requests.ImageLoaderRequest;
import me.grishka.appkit.imageloader.requests.UrlImageLoaderRequest;
import me.grishka.appkit.utils.V;

public class LinkCardStatusDisplayItem extends StatusDisplayItem{
private final UrlImageLoaderRequest imgRequest;
Expand Down Expand Up @@ -148,17 +145,24 @@ public void clearImage(int index){
private void onClick(View v){
String url=item.status.card.url;
// Mastodon.social sometimes adds an additional redirect page
// e.g. https://mastodon.social/@GenuineHuman/112683634483993833 (needs to be opened on another server)
// this is really disruptive on mobile, especially since it breaks the loopUp/openURL functionality
if(url.startsWith("https://mastodon.social/redirect/statuses/")){
Uri parsedURL=Uri.parse(url);
// find actually linked url in status content
Matcher matcher=HtmlParser.URL_PATTERN.matcher(item.status.content);
String foundURL;
while(matcher.find() && parsedURL.getLastPathSegment()!=null){
url=matcher.group(3);
foundURL=matcher.group(3);
if(TextUtils.isEmpty(matcher.group(4)))
url="http://"+url;
if(url.endsWith(parsedURL.getLastPathSegment()))
foundURL="http://"+foundURL;
// SAFETY: Cannot be null, as otherwise the matcher wouldn't find it
// also, group is marked as non-null
assert foundURL!=null;
if(foundURL.endsWith(parsedURL.getLastPathSegment())) {
// found correct URL
url=foundURL;
break;
}
}
}
UiUtils.openURL(itemView.getContext(), item.parentFragment.getAccountID(), url);
Expand Down

0 comments on commit a8164e0

Please sign in to comment.