Support legacy riot.im url.

This commit is contained in:
Onuray Sahin 2020-09-04 14:20:03 +03:00
parent a151f42495
commit f186a00515
1 changed files with 35 additions and 34 deletions

View File

@ -30,7 +30,6 @@ import im.vector.app.features.login.LoginConfig
import im.vector.app.features.permalink.PermalinkHandler import im.vector.app.features.permalink.PermalinkHandler
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import org.matrix.android.sdk.api.MatrixCallback import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
@ -61,28 +60,43 @@ class LinkHandlerActivity : VectorBaseActivity() {
} }
if (uri.path == PATH_CONFIG) { if (uri.path == PATH_CONFIG) {
if (sessionHolder.hasActiveSession()) { handleConfigUrl(uri)
displayAlreadyLoginPopup(uri) } else if (SUPPORTED_HOSTS.contains(uri.host)) {
} else { handleSupportedHostUrl(uri)
// user is not yet logged in, this is the nominal case }
startLoginActivity(uri) }
}
private fun handleConfigUrl(uri: Uri) {
if (sessionHolder.hasActiveSession()) {
displayAlreadyLoginPopup(uri)
} else { } else {
if (!sessionHolder.hasActiveSession()) { // user is not yet logged in, this is the nominal case
startLoginActivity(uri) startLoginActivity(uri)
}
}
private fun handleSupportedHostUrl(uri: Uri) {
if (!sessionHolder.hasActiveSession()) {
startLoginActivity(uri)
finish()
} else {
convertUriToPermalink(uri)?.let { permalink ->
startPermalinkHandler(permalink)
} ?: run {
// Host is correct but we do not recognize path
Timber.w("Unable to handle this uri: $uri")
finish() finish()
} else {
convertUrlToPermalink(uri.toString())?.let { permalink ->
startPermalinkHandler(permalink)
} ?: run {
// Other link are not yet handled, but should not comes here (manifest configuration error?)
Timber.w("Unable to handle this uir: $uri")
finish()
}
} }
} }
} }
private fun convertUriToPermalink(uri: Uri): String? {
val path = SUPPORTED_PATHS.find { it in uri.toString() } ?: return null
return uri
.toString()
.replace(uri.host + path, "$MATRIX_TO_HOST/#")
}
private fun startPermalinkHandler(permalink: String) { private fun startPermalinkHandler(permalink: String) {
permalinkHandler.launch(this, permalink, buildTask = true) permalinkHandler.launch(this, permalink, buildTask = true)
.delay(500, TimeUnit.MILLISECONDS) .delay(500, TimeUnit.MILLISECONDS)
@ -96,20 +110,6 @@ class LinkHandlerActivity : VectorBaseActivity() {
.disposeOnDestroy() .disposeOnDestroy()
} }
/**
* Converts domain urls to matrix.to urls
* @param url full url like https://app.element.io/#/room/#dummy_room:matrix.org
* @return matrix.to url like https://matrix.to/#/#dummy_room:matrix.org
*/
private fun convertUrlToPermalink(url: String): String? {
return when {
url.startsWith(ROOM_BASE_URL) -> url.replace(ROOM_BASE_URL, PermalinkService.MATRIX_TO_URL_BASE)
url.startsWith(USER_BASE_URL) -> url.replace(USER_BASE_URL, PermalinkService.MATRIX_TO_URL_BASE)
url.startsWith(GROUP_BASE_URL) -> url.replace(GROUP_BASE_URL, PermalinkService.MATRIX_TO_URL_BASE)
else -> null
}
}
/** /**
* Start the login screen with identity server and home server pre-filled * Start the login screen with identity server and home server pre-filled
*/ */
@ -156,8 +156,9 @@ class LinkHandlerActivity : VectorBaseActivity() {
companion object { companion object {
private const val PATH_CONFIG = "/config/config" private const val PATH_CONFIG = "/config/config"
private const val ROOM_BASE_URL = "https://app.element.io/#/room/"
private const val USER_BASE_URL = "https://app.element.io/#/user/" private val SUPPORTED_HOSTS = arrayOf("app.element.io", "riot.im")
private const val GROUP_BASE_URL = "https://app.element.io/#/group/" private val SUPPORTED_PATHS = arrayOf("/#/room", "/#/user", "/#/group")
private const val MATRIX_TO_HOST = "matrix.to"
} }
} }