From 7859f4cd05214d50c8a2d7de795a46863f129cce Mon Sep 17 00:00:00 2001 From: sk Date: Sat, 3 Jun 2023 23:39:43 +0200 Subject: [PATCH] support parsing mailto links i mean, why not - if github decided every @username@example.social is actually an email address, might as well support sharing that mailto link to megalodon --- .../android/ui/utils/UiUtilsTest.java | 19 ++++++++----- .../android/ExternalShareActivity.java | 2 +- .../android/ui/utils/UiUtils.java | 27 ++++++++++--------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/mastodon/src/androidTest/java/org/joinmastodon/android/ui/utils/UiUtilsTest.java b/mastodon/src/androidTest/java/org/joinmastodon/android/ui/utils/UiUtilsTest.java index 2fd84eb77..63c3fb866 100644 --- a/mastodon/src/androidTest/java/org/joinmastodon/android/ui/utils/UiUtilsTest.java +++ b/mastodon/src/androidTest/java/org/joinmastodon/android/ui/utils/UiUtilsTest.java @@ -29,35 +29,40 @@ public class UiUtilsTest { } @Test - public void looksLikeFediverseHandle() { + public void parseFediverseHandle() { assertEquals( Optional.of(Pair.create("megalodon", Optional.of("floss.social"))), - UiUtils.looksLikeFediverseHandle("megalodon@floss.social") + UiUtils.parseFediverseHandle("megalodon@floss.social") ); assertEquals( Optional.of(Pair.create("megalodon", Optional.of("floss.social"))), - UiUtils.looksLikeFediverseHandle("@megalodon@floss.social") + UiUtils.parseFediverseHandle("@megalodon@floss.social") ); assertEquals( Optional.of(Pair.create("megalodon", Optional.empty())), - UiUtils.looksLikeFediverseHandle("@megalodon") + UiUtils.parseFediverseHandle("@megalodon") + ); + + assertEquals( + Optional.of(Pair.create("megalodon", Optional.of("floss.social"))), + UiUtils.parseFediverseHandle("mailto:megalodon@floss.social") ); assertEquals( Optional.empty(), - UiUtils.looksLikeFediverseHandle("megalodon") + UiUtils.parseFediverseHandle("megalodon") ); assertEquals( Optional.empty(), - UiUtils.looksLikeFediverseHandle("this is not a fedi handle") + UiUtils.parseFediverseHandle("this is not a fedi handle") ); assertEquals( Optional.empty(), - UiUtils.looksLikeFediverseHandle("not@a-domain") + UiUtils.parseFediverseHandle("not@a-domain") ); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java index d1ad5468b..972e7504d 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ExternalShareActivity.java @@ -33,7 +33,7 @@ public class ExternalShareActivity extends FragmentStackActivity{ if(savedInstanceState==null){ Optional text = Optional.ofNullable(getIntent().getStringExtra(Intent.EXTRA_TEXT)); - Optional>> fediHandle = text.flatMap(UiUtils::looksLikeFediverseHandle); + Optional>> fediHandle = text.flatMap(UiUtils::parseFediverseHandle); boolean isFediUrl = text.map(UiUtils::looksLikeFediverseUrl).orElse(false); boolean isOpenable = isFediUrl || fediHandle.isPresent(); diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java index 7cace4851..25ca9e1d0 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/utils/UiUtils.java @@ -907,25 +907,28 @@ public class UiUtils { return theme == GlobalUserPreferences.ThemePreference.DARK; } - public static Optional>> looksLikeFediverseHandle(String maybeFediHandle) { + public static Optional>> parseFediverseHandle(String maybeFediHandle) { // https://stackoverflow.com/a/26987741, except i put a + here ... v String domainRegex = "^(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]\\.)+(xn--)?([a-z0-9][a-z0-9\\-]{0,60}|[a-z0-9-]{1,30}\\.[a-z]{2,})$"; - try { - List parts = Arrays.stream(maybeFediHandle.split("@")) - .filter(part -> !part.isEmpty()) - .collect(Collectors.toList()); - if (parts.size() == 0 || !parts.get(0).matches("^[^/\\s]+$")) { - return Optional.empty(); - } else if (parts.size() == 2) { + if (maybeFediHandle.toLowerCase().startsWith("mailto:")) { + maybeFediHandle = maybeFediHandle.substring("mailto:".length()); + } + List parts = Arrays.stream(maybeFediHandle.split("@")) + .filter(part -> !part.isEmpty()) + .collect(Collectors.toList()); + if (parts.size() == 0 || !parts.get(0).matches("^[^/\\s]+$")) { + return Optional.empty(); + } else if (parts.size() == 2) { + try { String domain = IDN.toASCII(parts.get(1)); if (!domain.matches(domainRegex)) return Optional.empty(); return Optional.of(Pair.create(parts.get(0), Optional.of(parts.get(1)))); - } else if (maybeFediHandle.startsWith("@")) { - return Optional.of(Pair.create(parts.get(0), Optional.empty())); - } else { + } catch (IllegalArgumentException ignored) { return Optional.empty(); } - } catch (IllegalArgumentException ignored) { + } else if (maybeFediHandle.startsWith("@")) { + return Optional.of(Pair.create(parts.get(0), Optional.empty())); + } else { return Optional.empty(); } }