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.
This commit is contained in:
Claude
2026-05-24 23:38:58 +00:00
parent 39008f90d3
commit 90fbe06f19
4 changed files with 74 additions and 18 deletions
@@ -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)
@@ -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
@@ -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
}