InvalidToken: notify the app - WIP

This commit is contained in:
Benoit Marty 2019-12-10 12:42:04 +01:00
parent d5935a13ac
commit 18649ebddb
7 changed files with 40 additions and 35 deletions

View File

@ -16,7 +16,8 @@
package im.vector.matrix.android.api.failure package im.vector.matrix.android.api.failure
// This data class will be sent to the bus // This class will be sent to the bus
data class ConsentNotGivenError( sealed class GlobalError {
val consentUri: String data class InvalidToken(val softLogout: Boolean) : GlobalError()
) data class ConsentNotGivenError(val consentUri: String) : GlobalError()
}

View File

@ -19,7 +19,7 @@ package im.vector.matrix.android.api.session
import androidx.annotation.MainThread import androidx.annotation.MainThread
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.failure.GlobalError
import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.pushrules.PushRuleService
import im.vector.matrix.android.api.session.cache.CacheService import im.vector.matrix.android.api.session.cache.CacheService
import im.vector.matrix.android.api.session.content.ContentUploadStateTracker import im.vector.matrix.android.api.session.content.ContentUploadStateTracker
@ -136,13 +136,10 @@ interface Session :
*/ */
interface Listener { interface Listener {
/** /**
* The access token is not valid anymore * Possible cases:
* - The access token is not valid anymore,
* - a M_CONSENT_NOT_GIVEN error has been received from the homeserver
*/ */
fun onInvalidToken() fun onGlobalError(globalError: GlobalError)
/**
* A M_CONSENT_NOT_GIVEN error has been received from the homeserver
*/
fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError)
} }
} }

View File

@ -20,8 +20,8 @@ package im.vector.matrix.android.internal.network
import com.squareup.moshi.JsonDataException import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonEncodingException import com.squareup.moshi.JsonEncodingException
import im.vector.matrix.android.api.failure.ConsentNotGivenError
import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.GlobalError
import im.vector.matrix.android.api.failure.MatrixError import im.vector.matrix.android.api.failure.MatrixError
import im.vector.matrix.android.internal.di.MoshiProvider import im.vector.matrix.android.internal.di.MoshiProvider
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
@ -99,7 +99,11 @@ private fun toFailure(errorBody: ResponseBody?, httpCode: Int): Failure {
if (matrixError != null) { if (matrixError != null) {
if (matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank()) { if (matrixError.code == MatrixError.M_CONSENT_NOT_GIVEN && !matrixError.consentUri.isNullOrBlank()) {
// Also send this error to the bus, for a global management // Also send this error to the bus, for a global management
EventBus.getDefault().post(ConsentNotGivenError(matrixError.consentUri)) EventBus.getDefault().post(GlobalError.ConsentNotGivenError(matrixError.consentUri))
} else if (matrixError.code == MatrixError.M_UNAUTHORIZED) {
// TODO Check that this is ok during the login flow
// Also send this error to the bus, for a global management
EventBus.getDefault().post(GlobalError.InvalidToken(matrixError.isSoftLogout == true))
} }
return Failure.ServerError(matrixError, httpCode) return Failure.ServerError(matrixError, httpCode)

View File

@ -23,7 +23,7 @@ import androidx.lifecycle.LiveData
import dagger.Lazy import dagger.Lazy
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.failure.GlobalError
import im.vector.matrix.android.api.pushrules.PushRuleService import im.vector.matrix.android.api.pushrules.PushRuleService
import im.vector.matrix.android.api.session.InitialSyncProgressService import im.vector.matrix.android.api.session.InitialSyncProgressService
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
@ -170,8 +170,8 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) { fun onGlobalError(globalError: GlobalError) {
sessionListeners.dispatchConsentNotGiven(consentNotGivenError) sessionListeners.dispatchGlobalError(globalError)
} }
override fun contentUrlResolver() = contentUrlResolver override fun contentUrlResolver() = contentUrlResolver

View File

@ -16,7 +16,7 @@
package im.vector.matrix.android.internal.session package im.vector.matrix.android.internal.session
import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.failure.GlobalError
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import javax.inject.Inject import javax.inject.Inject
@ -36,10 +36,10 @@ internal class SessionListeners @Inject constructor() {
} }
} }
fun dispatchConsentNotGiven(consentNotGivenError: ConsentNotGivenError) { fun dispatchGlobalError(globalError: GlobalError) {
synchronized(listeners) { synchronized(listeners) {
listeners.forEach { listeners.forEach {
it.onConsentNotGivenError(consentNotGivenError) it.onGlobalError(globalError)
} }
} }
} }

View File

@ -38,6 +38,7 @@ import butterknife.Unbinder
import com.airbnb.mvrx.MvRx import com.airbnb.mvrx.MvRx
import com.bumptech.glide.util.Util import com.bumptech.glide.util.Util
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import im.vector.matrix.android.api.failure.GlobalError
import im.vector.riotx.BuildConfig import im.vector.riotx.BuildConfig
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.di.* import im.vector.riotx.core.di.*
@ -153,9 +154,8 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasScreenInjector {
}) })
sessionListener = getVectorComponent().sessionListener() sessionListener = getVectorComponent().sessionListener()
sessionListener.consentNotGivenLiveData.observeEvent(this) { sessionListener.globalErrorLiveData.observeEvent(this) {
consentNotGivenHelper.displayDialog(it.consentUri, handleGlobalError(it)
activeSessionHolder.getActiveSession().sessionParams.homeServerConnectionConfig.homeServerUri.host ?: "")
} }
doBeforeSetContentView() doBeforeSetContentView()
@ -180,6 +180,15 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasScreenInjector {
} }
} }
private fun handleGlobalError(globalError: GlobalError) {
when (globalError) {
is GlobalError.InvalidToken -> TODO()
is GlobalError.ConsentNotGivenError ->
consentNotGivenHelper.displayDialog(globalError.consentUri,
activeSessionHolder.getActiveSession().sessionParams.homeServerConnectionConfig.homeServerUri.host ?: "")
}
}
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
unBinder?.unbind() unBinder?.unbind()

View File

@ -18,27 +18,21 @@ package im.vector.riotx.features.session
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import im.vector.matrix.android.api.failure.ConsentNotGivenError import im.vector.matrix.android.api.failure.GlobalError
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.riotx.core.extensions.postLiveEvent import im.vector.riotx.core.extensions.postLiveEvent
import im.vector.riotx.core.utils.LiveEvent import im.vector.riotx.core.utils.LiveEvent
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SessionListener @Inject constructor() : Session.Listener { class SessionListener @Inject constructor() : Session.Listener {
private val _consentNotGivenLiveData = MutableLiveData<LiveEvent<ConsentNotGivenError>>() private val _globalErrorLiveData = MutableLiveData<LiveEvent<GlobalError>>()
val consentNotGivenLiveData: LiveData<LiveEvent<ConsentNotGivenError>> val globalErrorLiveData: LiveData<LiveEvent<GlobalError>>
get() = _consentNotGivenLiveData get() = _globalErrorLiveData
override fun onInvalidToken() { override fun onGlobalError(globalError: GlobalError) {
// TODO Handle this error _globalErrorLiveData.postLiveEvent(globalError)
Timber.e("Token is not valid anymore: handle this properly")
}
override fun onConsentNotGivenError(consentNotGivenError: ConsentNotGivenError) {
_consentNotGivenLiveData.postLiveEvent(consentNotGivenError)
} }
} }