mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-19 20:08:27 +00:00
Minor refactoring
This commit is contained in:
parent
5fd99cd8f1
commit
cf3f0fbe57
@ -48,7 +48,7 @@ class OpenGroupInvitationView : FrameLayout {
|
||||
|
||||
fun setOpenGroup(name: String, url: String, isOutgoing: Boolean = false) {
|
||||
groupName.text = name
|
||||
displayedUrl.text = OpenGroupUrlParser.trimParameter(url)
|
||||
displayedUrl.text = OpenGroupUrlParser.trimQueryParameter(url)
|
||||
groupUrl = url
|
||||
|
||||
if(isOutgoing) {
|
||||
|
@ -4,37 +4,33 @@ import okhttp3.HttpUrl
|
||||
|
||||
object OpenGroupUrlParser {
|
||||
|
||||
// Error
|
||||
sealed class Error(val description: String) : Exception(description) {
|
||||
class MalformedUrl() : Error("Malformed URL.")
|
||||
object NoRoomSpecified : Error("No room specified in the URL.")
|
||||
object NoPublicKeySpecified : Error("No public key specified in the URL.")
|
||||
object InvalidPublicKeyProvided : Error("Invalid public key provided.")
|
||||
object MalformedURL : Error("Malformed URL.")
|
||||
object NoRoom : Error("No room specified in the URL.")
|
||||
object NoPublicKey : Error("No public key specified in the URL.")
|
||||
object InvalidPublicKey : Error("Invalid public key provided.")
|
||||
}
|
||||
|
||||
private const val suffix = "/"
|
||||
private const val queryPrefix = "public_key"
|
||||
|
||||
fun parseUrl(stringUrl: String): OpenGroupRoom {
|
||||
// Url have to start with 'http://'
|
||||
val url = if (!stringUrl.startsWith("http")) "http://$stringUrl" else stringUrl
|
||||
// If the URL is malformed, it will throw an exception
|
||||
val httpUrl = HttpUrl.parse(url) ?: throw Error.MalformedUrl()
|
||||
|
||||
val server = HttpUrl.Builder().scheme(httpUrl.scheme()).host(httpUrl.host()).port(httpUrl.port()).build().toString().removeSuffix(suffix)
|
||||
// Test if the room is specified in the URL
|
||||
val room = httpUrl.pathSegments().firstOrNull { !it.isNullOrEmpty() } ?: throw Error.NoRoomSpecified
|
||||
// Test if the query is specified in the URL
|
||||
val publicKey = httpUrl.queryParameter(queryPrefix) ?: throw Error.NoPublicKeySpecified
|
||||
// Public key must be 64 characters
|
||||
if (publicKey.length != 64) throw Error.InvalidPublicKeyProvided
|
||||
|
||||
return OpenGroupRoom(server,room,publicKey)
|
||||
fun parseUrl(string: String): V2OpenGroupInfo {
|
||||
// URL has to start with 'http://'
|
||||
val urlWithPrefix = if (!string.startsWith("http")) "http://$string" else string
|
||||
// If the URL is malformed, throw an exception
|
||||
val url = HttpUrl.parse(urlWithPrefix) ?: throw Error.MalformedURL
|
||||
// Parse components
|
||||
val server = HttpUrl.Builder().scheme(url.scheme()).host(url.host()).port(url.port()).build().toString().removeSuffix(suffix)
|
||||
val room = url.pathSegments().firstOrNull { !it.isNullOrEmpty() } ?: throw Error.NoRoom
|
||||
val publicKey = url.queryParameter(queryPrefix) ?: throw Error.NoPublicKey
|
||||
if (publicKey.length != 64) throw Error.InvalidPublicKey
|
||||
// Return
|
||||
return V2OpenGroupInfo(server,room,publicKey)
|
||||
}
|
||||
|
||||
fun trimParameter(stringUrl: String): String {
|
||||
return stringUrl.substringBefore("?$queryPrefix")
|
||||
fun trimQueryParameter(string: String): String {
|
||||
return string.substringBefore("?$queryPrefix")
|
||||
}
|
||||
}
|
||||
|
||||
class OpenGroupRoom(val server: String, val room: String, val serverPublicKey: String) {}
|
||||
class V2OpenGroupInfo(val server: String, val room: String, val serverPublicKey: String) {}
|
||||
|
@ -61,25 +61,25 @@ class OpenGroupUrlParserTest {
|
||||
assertEquals(expectedPublicKey, result.serverPublicKey)
|
||||
}
|
||||
|
||||
@Test(expected = OpenGroupUrlParser.Error.MalformedUrl::class)
|
||||
@Test(expected = OpenGroupUrlParser.Error.MalformedURL::class)
|
||||
fun parseUrlMalformedUrlTest() {
|
||||
val inputUrl = "file:sessionopengroup.co/main?public_key=658d29b91892a2389505596b135e76a53db6e11d613a51dbd3d0816adffb231c"
|
||||
OpenGroupUrlParser.parseUrl(inputUrl)
|
||||
}
|
||||
|
||||
@Test(expected = OpenGroupUrlParser.Error.NoRoomSpecified::class)
|
||||
@Test(expected = OpenGroupUrlParser.Error.NoRoom::class)
|
||||
fun parseUrlNoRoomSpecifiedTest() {
|
||||
val inputUrl = "https://sessionopengroup.comain?public_key=658d29b91892a2389505596b135e76a53db6e11d613a51dbd3d0816adffb231c"
|
||||
OpenGroupUrlParser.parseUrl(inputUrl)
|
||||
}
|
||||
|
||||
@Test(expected = OpenGroupUrlParser.Error.NoPublicKeySpecified::class)
|
||||
@Test(expected = OpenGroupUrlParser.Error.NoPublicKey::class)
|
||||
fun parseUrlNoPublicKeySpecifiedTest() {
|
||||
val inputUrl = "https://sessionopengroup.co/main"
|
||||
OpenGroupUrlParser.parseUrl(inputUrl)
|
||||
}
|
||||
|
||||
@Test(expected = OpenGroupUrlParser.Error.InvalidPublicKeyProvided::class)
|
||||
@Test(expected = OpenGroupUrlParser.Error.InvalidPublicKey::class)
|
||||
fun parseUrlInvalidPublicKeyProviedTest() {
|
||||
val inputUrl = "https://sessionopengroup.co/main?public_key=658d29b91892a2389505596b135e76a53db6e11d613a51dbd3d0816adff"
|
||||
OpenGroupUrlParser.parseUrl(inputUrl)
|
||||
|
Loading…
x
Reference in New Issue
Block a user