Unverified Commit 1a1213da authored by Rafael Kellermann Streit's avatar Rafael Kellermann Streit Committed by GitHub

Merge pull request #1258 from RocketChat/feature/detect-redirect-on-server-info

[NEW] Detect redirect on server info
parents 20d8fdb9 db371a22
...@@ -13,8 +13,8 @@ android { ...@@ -13,8 +13,8 @@ android {
applicationId "chat.rocket.android" applicationId "chat.rocket.android"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion versions.targetSdk targetSdkVersion versions.targetSdk
versionCode 2019 versionCode 2020
versionName "2.1.0" versionName "2.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true multiDexEnabled true
} }
......
package chat.rocket.android.authentication.server.presentation package chat.rocket.android.authentication.server.presentation
import okhttp3.HttpUrl
interface VersionCheckView { interface VersionCheckView {
/** /**
* Alerts the user about the server version not meeting the recommended server version. * Alerts the user about the server version not meeting the recommended server version.
...@@ -26,4 +28,9 @@ interface VersionCheckView { ...@@ -26,4 +28,9 @@ interface VersionCheckView {
* Alters the user this protocol is invalid. This is optional. * Alters the user this protocol is invalid. This is optional.
*/ */
fun errorInvalidProtocol() {} fun errorInvalidProtocol() {}
/**
* Updates the server URL after a URL redirection
*/
fun updateServerUrl(url: HttpUrl) {}
} }
\ No newline at end of file
...@@ -20,6 +20,7 @@ import chat.rocket.android.util.extensions.* ...@@ -20,6 +20,7 @@ import chat.rocket.android.util.extensions.*
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
import dagger.android.support.AndroidSupportInjection import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_authentication_server.* import kotlinx.android.synthetic.main.fragment_authentication_server.*
import okhttp3.HttpUrl
import javax.inject.Inject import javax.inject.Inject
class ServerFragment : Fragment(), ServerView { class ServerFragment : Fragment(), ServerView {
...@@ -41,6 +42,7 @@ class ServerFragment : Fragment(), ServerView { ...@@ -41,6 +42,7 @@ class ServerFragment : Fragment(), ServerView {
} }
private var protocol = "https://" private var protocol = "https://"
private var ignoreChange = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -72,22 +74,27 @@ class ServerFragment : Fragment(), ServerView { ...@@ -72,22 +74,27 @@ class ServerFragment : Fragment(), ServerView {
protocol = "https://" protocol = "https://"
} }
1 -> { 1 -> {
ui{ if (ignoreChange) {
AlertDialog.Builder(it) protocol = "http://"
.setTitle(R.string.msg_warning) } else {
.setMessage(R.string.msg_http_insecure) ui {
.setPositiveButton(R.string.msg_proceed) { _, _ -> AlertDialog.Builder(it)
protocol = "http://" .setTitle(R.string.msg_warning)
} .setMessage(R.string.msg_http_insecure)
.setNegativeButton(R.string.msg_cancel) { _, _ -> .setPositiveButton(R.string.msg_proceed) { _, _ ->
text_server_protocol.setSelection(0) protocol = "http://"
} }
.setCancelable(false) .setNegativeButton(R.string.msg_cancel) { _, _ ->
.create() text_server_protocol.setSelection(0)
.show() }
.setCancelable(false)
.create()
.show()
}
} }
} }
} }
ignoreChange = false
} }
override fun onNothingSelected(parent: AdapterView<*>?) { override fun onNothingSelected(parent: AdapterView<*>?) {
...@@ -174,13 +181,23 @@ class ServerFragment : Fragment(), ServerView { ...@@ -174,13 +181,23 @@ class ServerFragment : Fragment(), ServerView {
showMessage(R.string.msg_invalid_server_protocol) showMessage(R.string.msg_invalid_server_protocol)
} }
override fun updateServerUrl(url: HttpUrl) {
if (activity != null && view != null) {
if (url.scheme() == "https") text_server_protocol.setSelection(0) else text_server_protocol.setSelection(1)
protocol = "${url.scheme()}://"
val serverUrl = url.toString().removePrefix("${url.scheme()}://")
text_server_url.textContent = serverUrl
}
}
private fun performConnect() { private fun performConnect() {
ui { ui {
deepLinkInfo?.let { deepLinkInfo?.let {
presenter.deepLink(it) presenter.deepLink(it)
}.ifNull { }.ifNull {
val url = text_server_url.textContent.ifEmpty(text_server_url.hintContent) val url = text_server_url.textContent.ifEmpty(text_server_url.hintContent)
presenter.connect("${protocol}${url.sanitize()}") presenter.connect("$protocol${url.sanitize()}")
} }
} }
} }
......
...@@ -150,9 +150,24 @@ class ChatRoomPresenter @Inject constructor( ...@@ -150,9 +150,24 @@ class ChatRoomPresenter @Inject constructor(
urls = null, urls = null,
isTemporary = true isTemporary = true
) )
messagesRepository.save(newMessage) try {
view.showNewMessage(mapper.map(newMessage)) val message = client.sendMessage(id, chatRoomId, text)
client.sendMessage(id, chatRoomId, text) messagesRepository.save(newMessage)
view.showNewMessage(mapper.map(newMessage))
message
} catch (ex: Exception) {
// Ok, not very beautiful, but the backend sends us a not valid response
// When someone sends a message on a read-only channel, so we just ignore it
// and show a generic error message
// TODO - remove the generic message when we implement :userId:/message subscription
if (ex is IllegalStateException) {
Timber.d(ex, "Probably a read-only problem...")
view.showGenericErrorMessage()
} else {
// some other error, just rethrow it...
throw ex
}
}
} else { } else {
client.updateMessage(chatRoomId, messageId, text) client.updateMessage(chatRoomId, messageId, text)
} }
......
...@@ -8,6 +8,7 @@ import chat.rocket.android.util.VersionInfo ...@@ -8,6 +8,7 @@ import chat.rocket.android.util.VersionInfo
import chat.rocket.android.util.extensions.launchUI import chat.rocket.android.util.extensions.launchUI
import chat.rocket.android.util.retryIO import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatInvalidProtocolException import chat.rocket.common.RocketChatInvalidProtocolException
import chat.rocket.common.model.ServerInfo
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.serverInfo import chat.rocket.core.internal.rest.serverInfo
import kotlinx.coroutines.experimental.Deferred import kotlinx.coroutines.experimental.Deferred
...@@ -26,7 +27,13 @@ abstract class CheckServerPresenter constructor(private val strategy: CancelStra ...@@ -26,7 +27,13 @@ abstract class CheckServerPresenter constructor(private val strategy: CancelStra
try { try {
currentServer = serverUrl currentServer = serverUrl
client = factory.create(currentServer) client = factory.create(currentServer)
val version = checkServerVersion(serverUrl).await() val serverInfo = retryIO(description = "serverInfo", times = 5) {
client.serverInfo()
}
if (serverInfo.redirected) {
view.updateServerUrl(serverInfo.url)
}
val version = checkServerVersion(serverInfo)
when (version) { when (version) {
is Version.VersionOk -> { is Version.VersionOk -> {
Timber.i("Your version is nice! (Requires: 0.62.0, Yours: ${version.version})") Timber.i("Your version is nice! (Requires: 0.62.0, Yours: ${version.version})")
...@@ -55,23 +62,19 @@ abstract class CheckServerPresenter constructor(private val strategy: CancelStra ...@@ -55,23 +62,19 @@ abstract class CheckServerPresenter constructor(private val strategy: CancelStra
} }
} }
internal fun checkServerVersion(serverUrl: String): Deferred<Version> { private fun checkServerVersion(serverInfo: ServerInfo): Version {
currentServer = serverUrl val thisServerVersion = serverInfo.version
return async { val isRequiredVersion = isRequiredServerVersion(thisServerVersion)
val serverInfo = retryIO(description = "serverInfo", times = 5) { client.serverInfo() } val isRecommendedVersion = isRecommendedServerVersion(thisServerVersion)
val thisServerVersion = serverInfo.version return if (isRequiredVersion) {
val isRequiredVersion = isRequiredServerVersion(thisServerVersion) if (isRecommendedVersion) {
val isRecommendedVersion = isRecommendedServerVersion(thisServerVersion) Timber.i("Your version is nice! (Requires: 0.62.0, Yours: $thisServerVersion)")
if (isRequiredVersion) { Version.VersionOk(thisServerVersion)
if (isRecommendedVersion) {
Timber.i("Your version is nice! (Requires: 0.62.0, Yours: $thisServerVersion)")
return@async Version.VersionOk(thisServerVersion)
} else {
return@async Version.RecommendedVersionWarning(thisServerVersion)
}
} else { } else {
return@async Version.OutOfDateError(thisServerVersion) Version.RecommendedVersionWarning(thisServerVersion)
} }
} else {
Version.OutOfDateError(thisServerVersion)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment