feat(Compose): remove tracking params in URLs

This commit is contained in:
FineFindus 2024-07-19 19:54:35 +02:00
parent 3020c826ed
commit 72d486e992
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
2 changed files with 23 additions and 0 deletions

View File

@ -99,6 +99,7 @@ import org.joinmastodon.android.ui.text.ComposeAutocompleteSpan;
import org.joinmastodon.android.ui.text.ComposeHashtagOrMentionSpan;
import org.joinmastodon.android.ui.text.HtmlParser;
import org.joinmastodon.android.ui.utils.SimpleTextWatcher;
import org.joinmastodon.android.utils.Tracking;
import org.joinmastodon.android.utils.TransferSpeedTracker;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.ui.viewcontrollers.ComposeAutocompleteViewController;
@ -1175,6 +1176,8 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr
private void actuallyPublish(boolean preview){
String text=mainEditText.getText().toString();
if(GlobalUserPreferences.removeTrackingParams)
text=Tracking.cleanUrlsInText(text);
CreateStatus.Request req=new CreateStatus.Request();
if("bottom".equals(postLang.encoding)){
text=new StatusTextEncoder(Bottom::encode).encode(text);

View File

@ -1,9 +1,11 @@
package org.joinmastodon.android.utils;
import android.net.Uri;
import android.util.Patterns;
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.regex.Matcher;
// Inspired by https://github.com/GeopJr/Tuba/blob/91a036edff9ab1ffb38d5b54a33023e5db551051/src/Utils/Tracking.vala
@ -75,6 +77,24 @@ public class Tracking{
return uriBuilder.build().toString();
}
/**
* Cleans URLs within the provided text, removing the tracking parameters from them.
*
* @param text The text that may contain URLs.
* @return The given text with cleaned URLs.
*/
public static String cleanUrlsInText(String text) {
Matcher matcher = Patterns.WEB_URL.matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String url = matcher.group();
matcher.appendReplacement(sb, removeTrackingParameters(url));
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* Returns true if the given parameter is used for tracking.
*/