Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle /c/community@host URLs #583

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,50 @@ fun LazyListState.isScrolledToEnd(): Boolean {
return out
}

/*
* Parses a "url" and returns a spec-compliant Url:
*
* - https://host/path - leave as-is
* - http://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://") || url.startsWith("http://")) {
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
17 changes: 17 additions & 0 deletions app/src/test/java/com/jerboa/UtilsKtTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jerboa

import androidx.compose.ui.unit.dp
import com.jerboa.api.API
import com.jerboa.datatypes.api.GetUnreadCountResponse
import com.jerboa.ui.theme.SMALL_PADDING
import org.junit.Assert.*
Expand Down Expand Up @@ -136,4 +137,20 @@ class UtilsKtTest {
assertEquals("1.2M", siFormat(1234500))
assertEquals("12M", siFormat(12345000))
}

@Test
fun testParseUrl() {
val cases = mapOf(
"https://feddit.de" to "https://feddit.de",
"http://example.com" to "http://example.com",
"/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)) }
}
}