Skip to content

Commit

Permalink
Merge pull request #438
Browse files Browse the repository at this point in the history
Fix/mastodon social redirect
  • Loading branch information
LucasGGamerM authored Jul 6, 2024
2 parents 3265cfe + f590fde commit b5a0c29
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 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 @@ -18,12 +17,15 @@
import org.joinmastodon.android.model.Status;
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.utils.UiUtils;

import java.util.Optional;
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 @@ -142,7 +144,35 @@ public void clearImage(int index){
}

private void onClick(View v){
UiUtils.openURL(itemView.getContext(), item.parentFragment.getAccountID(), item.status.card.url);
String url=item.status.card.url;
// Mastodon.social sometimes adds an additional redirect page
// this is really disruptive on mobile, especially since it breaks the loopUp/openURL functionality
Uri parsedURL=Uri.parse(url);
if(parsedURL.getPath()!=null && parsedURL.getPath().startsWith("/redirect/")){
url=findRedirectedURL(parsedURL).orElse(url);
}
UiUtils.openURL(itemView.getContext(), item.parentFragment.getAccountID(), url);
}

private Optional<String> findRedirectedURL(Uri url){
// find actually linked url in status content
Matcher matcher=HtmlParser.URL_PATTERN.matcher(item.status.content);
boolean isAccountRedirect=url.getPath().startsWith("/redirect/accounts");
String foundURL;
while(matcher.find()){
foundURL=matcher.group(3);
if(TextUtils.isEmpty(matcher.group(4)))
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 && url.getLastPathSegment()!=null;
if(foundURL.endsWith(url.getLastPathSegment()) ||
(isAccountRedirect && foundURL.matches("https://"+url.getHost()+"/@[a-zA-Z0-9_]+@[a-zA-Z0-9._]+$"))){
// found correct URL
return Optional.of(foundURL);
}
}
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ public void onSuccess(SearchResults results) {
return;
}
Optional<Account> account = results.accounts.stream()
.filter(a -> uri.equals(Uri.parse(a.url))).findAny();
.filter(a -> uri.getPath().contains(a.getFullyQualifiedName())).findAny();
if (account.isPresent()) {
args.putParcelable("profileAccount", Parcels.wrap(account.get()));
go.accept(ProfileFragment.class, args);
Expand Down

0 comments on commit b5a0c29

Please sign in to comment.