|
|
|
@ -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) {}
|
|
|
|
|