- Re-normalizes all relays urls before connecting to reduce duplications

- Normalizes all new relays in the edit screens.
This commit is contained in:
Vitor Pamplona
2024-06-04 18:30:01 -04:00
parent f1e516662c
commit d10b4c6bde
9 changed files with 63 additions and 41 deletions
+3
View File
@@ -67,6 +67,9 @@ dependencies {
// Parses URLs from Text:
api libs.url.detector
// Parses URLs from Text:
api libs.rfc3986.normalizer
testImplementation libs.junit
androidTestImplementation platform(libs.androidx.compose.bom)
androidTestImplementation libs.androidx.junit
@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.quartz.encoders
import org.czeal.rfc3986.URIReference
class RelayUrlFormatter {
companion object {
fun displayUrl(url: String): String {
@@ -27,20 +29,22 @@ class RelayUrlFormatter {
}
fun normalize(url: String): String {
var newUrl =
val newUrl =
if (!url.startsWith("wss://") && !url.startsWith("ws://")) {
if (url.endsWith(".onion") || url.endsWith(".onion/")) {
"ws://$url"
"ws://${url.trim()}"
} else {
"wss://$url"
"wss://${url.trim()}"
}
} else {
url
url.trim()
}
if (url.endsWith("/")) newUrl = newUrl.dropLast(1)
return newUrl
return try {
URIReference.parse(newUrl).normalize().toString()
} catch (e: Exception) {
newUrl
}
}
fun getHttpsUrl(dirtyUrl: String): String {