Skip to content

Commit

Permalink
Handle more URL types
Browse files Browse the repository at this point in the history
This is a partial fix to #556, but it at least prevents crashing when an unknown
URL type is encountered. There is still some work to do, since the
markdown plugin doesn't properly interpret URLs the way we want.

User-related parsing still doesn't work because there's work needed in
the markdown layer, but once that's done, this should just work as
intended.
  • Loading branch information
beatgammit committed Jun 14, 2023
1 parent 63623cc commit fcb80a4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,49 @@ fun LazyListState.isScrolledToEnd(): Boolean {
return out
}

/*
* Parses a "url" and returns a spec-compliant Url:
*
* - https://host/path - leave as-is
* - /c/community -> https://currentInstance/c/community
* - /c/community@instance -> https://instance/c/community
* - !community@instance -> https://instance/c/community
* - @user@instance -> https://instance/u/user
*/
fun parseUrl(url: String): String? {
if (url.startsWith("https://")) {
return url
} else if (url.startsWith("/c/")) {
if (url.count({ c -> c == '@' }) == 1) {
val (community, host) = url.split("@", limit = 2)
return "https://$host$community"
}
return "https://${API.currentInstance}$url"
} else if (url.startsWith("/u/")) {
if (url.count({ c -> c == '@' }) == 1) {
val (userPath, host) = url.split("@", limit = 2)
return "https://$host$userPath"
}
return "https://${API.currentInstance}$url"
} else if (url.startsWith("!")) {
if (url.count({ c -> c == '@' }) == 1) {
val (community, host) = url.substring(1).split("@", limit = 2)
return "https://$host/c/$community"
}
return "https://${API.currentInstance}/c/${url.substring(1)}"
} else if (url.startsWith("@")) {
if (url.count({ c -> c == '@' }) == 2) {
val (user, host) = url.substring(1).split("@", limit = 2)
return "https://$host/u/$user"
}
return "https://${API.currentInstance}/u/${url.substring(1)}"
}
return null
}

fun openLink(url: String, ctx: Context, useCustomTab: Boolean, usePrivateTab: Boolean) {
val url = parseUrl(url) ?: return

if (useCustomTab) {
val intent = CustomTabsIntent.Builder()
.build().apply {
Expand Down
24 changes: 24 additions & 0 deletions app/src/test/java/com/jerboa/UtilsKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jerboa

import com.jerboa.api.API
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import java.net.URL

class UtilsKtTest {
@Test
fun testParseUrl() {
val cases = mapOf(
"https://feddit.de" to "https://feddit.de",
"/c/community" to "https://${API.currentInstance}/c/community",
"/c/[email protected]" to "https://instance.ml/c/community",
"[email protected]" to "https://instance.ml/c/community",
"!community" to "https://${API.currentInstance}/c/community",
"/u/[email protected]" to "https://instance.ml/u/user",
"@[email protected]" to "https://instance.ml/u/user",
)

cases.forEach { (url, exp) -> assertEquals(exp, parseUrl(url)) }
}
}

0 comments on commit fcb80a4

Please sign in to comment.