From 90fbe06f196f9f4c18a9e901d192cc596bec68da Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 24 May 2026 23:38:58 +0000 Subject: [PATCH] refactor: KMP URL validation + move UrlInfoItem to jvmAndroid Clears the two java.net.* importers from commons/commonMain. - Adds expect fun isValidUrl(url: String?): Boolean in commons/commonMain/util/. The jvmAndroid actual preserves the exact JVM semantics (URI.toURL() + the same 3 catch arms); iOS actual will use NSURL when the target lands. - RichTextParser.isValidURL becomes a thin wrapper around isValidUrl. Keeps the existing static call site so callers in the Android app and Desktop need no change. - UrlInfoItem.kt (link-preview model that wraps URI) moves to jvmAndroid; its only consumers are the Android link-preview pipeline (HtmlCharsetParser, UrlPreviewState, UrlPreviewCard), which already live outside commonMain. commons/commonMain is now down to 3 java.* importers: Note (BigDecimal) and the two SortedSet-based observables. --- .../commons/richtext/RichTextParser.kt | 20 +-------- .../amethyst/commons/util/UrlValidation.kt | 31 ++++++++++++++ .../amethyst/commons/preview/UrlInfoItem.kt | 0 .../commons/util/UrlValidation.jvmAndroid.kt | 41 +++++++++++++++++++ 4 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.kt rename commons/src/{commonMain => jvmAndroid}/kotlin/com/vitorpamplona/amethyst/commons/preview/UrlInfoItem.kt (100%) create mode 100644 commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.jvmAndroid.kt diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/richtext/RichTextParser.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/richtext/RichTextParser.kt index 05399c493e..b8f8736716 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/richtext/RichTextParser.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/richtext/RichTextParser.kt @@ -22,6 +22,7 @@ package com.vitorpamplona.amethyst.commons.richtext import com.vitorpamplona.amethyst.commons.emojicoder.EmojiCoder import com.vitorpamplona.amethyst.commons.model.ImmutableListOfLists +import com.vitorpamplona.amethyst.commons.util.isValidUrl import com.vitorpamplona.quartz.experimental.inlineMetadata.Nip54InlineMetadata import com.vitorpamplona.quartz.nip30CustomEmoji.CustomEmoji import com.vitorpamplona.quartz.nip31Alts.AltTag @@ -40,9 +41,6 @@ import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableMap import kotlinx.collections.immutable.toPersistentList -import java.net.MalformedURLException -import java.net.URI -import java.net.URISyntaxException import kotlin.coroutines.cancellation.CancellationException class RichTextParser { @@ -489,21 +487,7 @@ class RichTextParser { return pdfExtensions.any { removedParamsFromUrl.endsWith(it) } } - fun isValidURL(url: String?): Boolean = - try { - if (url != null) { - URI(url).toURL() - true - } else { - false - } - } catch (e: MalformedURLException) { - false - } catch (e: URISyntaxException) { - false - } catch (e: IllegalArgumentException) { - false - } + fun isValidURL(url: String?): Boolean = isValidUrl(url) fun parseImageOrVideo(fullUrl: String): BaseMediaContent { val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl) diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.kt new file mode 100644 index 0000000000..9a63884678 --- /dev/null +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.util + +/** + * True iff [url] is a syntactically valid absolute URL with a recognized + * scheme. JVM/Android delegate to `java.net.URI(url).toURL()`; the iOS actual + * will use `NSURL(string:)` when that target is added. + * + * Called per-URL during feed render — actuals should keep this fast and + * allocation-light. + */ +expect fun isValidUrl(url: String?): Boolean diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/preview/UrlInfoItem.kt b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/preview/UrlInfoItem.kt similarity index 100% rename from commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/preview/UrlInfoItem.kt rename to commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/preview/UrlInfoItem.kt diff --git a/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.jvmAndroid.kt b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.jvmAndroid.kt new file mode 100644 index 0000000000..70905418b4 --- /dev/null +++ b/commons/src/jvmAndroid/kotlin/com/vitorpamplona/amethyst/commons/util/UrlValidation.jvmAndroid.kt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.commons.util + +import java.net.MalformedURLException +import java.net.URI +import java.net.URISyntaxException + +actual fun isValidUrl(url: String?): Boolean = + try { + if (url != null) { + URI(url).toURL() + true + } else { + false + } + } catch (_: MalformedURLException) { + false + } catch (_: URISyntaxException) { + false + } catch (_: IllegalArgumentException) { + false + }