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