fix(login): added a fallback to the official stoat instance if can't connect to self-hosted
This commit is contained in:
parent
eb9a4fe344
commit
14eca35adb
|
|
@ -76,6 +76,9 @@ import chat.stoat.BuildConfig
|
|||
import chat.stoat.R
|
||||
import chat.stoat.StoatApplication
|
||||
import chat.stoat.api.HitRateLimitException
|
||||
import chat.stoat.api.STOAT_BASE
|
||||
import chat.stoat.api.STOAT_BASE_DEFAULT
|
||||
import chat.stoat.api.STOAT_WEB_APP_DEFAULT
|
||||
import chat.stoat.api.StoatAPI
|
||||
import chat.stoat.api.StoatHttp
|
||||
import chat.stoat.api.api
|
||||
|
|
@ -228,13 +231,29 @@ class MainActivityViewModel @Inject constructor(
|
|||
"We have a session token, checking if it's valid and if we can still reach Stoat"
|
||||
)
|
||||
|
||||
val canReachStoat = canReachStoat()
|
||||
val valid = try {
|
||||
StoatAPI.checkSessionToken(token)
|
||||
} catch (e: Throwable) {
|
||||
false
|
||||
var valid = false;
|
||||
var canReachStoat = false;
|
||||
for(attempt in 1..2) {
|
||||
canReachStoat = canReachStoat();
|
||||
if(canReachStoat) {
|
||||
valid = try {
|
||||
StoatAPI.checkSessionToken(token)
|
||||
} catch (e: Throwable) {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
Log.d("MainActivity", "Cannot reach $STOAT_BASE, trying $STOAT_BASE_DEFAULT");
|
||||
// Fall back to the default instance,
|
||||
// otherwise the user might get stuck trying to connect to an unreachable instance
|
||||
updateStoatWebApp(STOAT_WEB_APP_DEFAULT);
|
||||
}
|
||||
|
||||
if(canReachStoat && valid) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (canReachStoat && !valid) {
|
||||
Log.d("MainActivity", "Session token is invalid, could not log in")
|
||||
couldNotLogIn.emit(true)
|
||||
|
|
|
|||
|
|
@ -87,13 +87,19 @@ fun updateStoatWebApp(webApp: String = STOAT_WEB_APP_DEFAULT) {
|
|||
sanitisedBaseUrl = STOAT_WEB_APP_DEFAULT;
|
||||
}
|
||||
|
||||
if(!sanitisedBaseUrl.matches(Regex("^https?://.+"))) {
|
||||
// Default to https if no scheme provided
|
||||
sanitisedBaseUrl = "https://${sanitisedBaseUrl}";
|
||||
}
|
||||
|
||||
var parsed = sanitisedBaseUrl.toUri();
|
||||
|
||||
val portPart = if (parsed.port == 80 || parsed.port == 443) "" else ":${parsed.port}";
|
||||
val root = "${parsed.scheme}://${parsed.host}${portPart}";
|
||||
val rootWithPort = "${parsed.scheme}://${parsed.host}:${parsed.port}";
|
||||
if(sanitisedBaseUrl.matches(Regex("^$root/+$")) || sanitisedBaseUrl.matches(Regex("^$rootWithPort/+$"))) {
|
||||
// Remove trailing slashes (not sure if it'd actually cause an issue but might as well clean it up)
|
||||
// Remove trailing slashes
|
||||
// (not sure if it'd actually cause an issue but might as well clean it up)
|
||||
sanitisedBaseUrl = rootWithPort;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue