Improve code

This commit is contained in:
Benoit Marty 2020-06-18 22:00:44 +02:00
parent a0a8f95d37
commit fe8f79698c

View file

@ -30,6 +30,8 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import im.vector.matrix.android.internal.legacy.riot.Fingerprint as LegacyFingerprint
import im.vector.matrix.android.internal.legacy.riot.HomeServerConnectionConfig as LegacyHomeServerConnectionConfig
internal class DefaultLegacySessionImporter @Inject constructor( internal class DefaultLegacySessionImporter @Inject constructor(
context: Context, context: Context,
@ -45,19 +47,32 @@ internal class DefaultLegacySessionImporter @Inject constructor(
Timber.d("Migration: found ${list.size} session(s).") Timber.d("Migration: found ${list.size} session(s).")
val firstConfig = list.firstOrNull() ?: return val legacyConfig = list.firstOrNull() ?: return
GlobalScope.launch {
Timber.d("Migration: importing a session") Timber.d("Migration: importing a session")
importCredentials(legacyConfig)
Timber.d("Migration: importing crypto DB")
importCryptoDb(legacyConfig)
Timber.d("Migration: clear legacy session")
// Delete to avoid doing this several times
loginStorage.clear()
}
}
private suspend fun importCredentials(legacyConfig: LegacyHomeServerConnectionConfig) {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
val sessionParams = SessionParams( val sessionParams = SessionParams(
credentials = Credentials( credentials = Credentials(
userId = firstConfig.credentials.userId, userId = legacyConfig.credentials.userId,
accessToken = firstConfig.credentials.accessToken, accessToken = legacyConfig.credentials.accessToken,
refreshToken = firstConfig.credentials.refreshToken, refreshToken = legacyConfig.credentials.refreshToken,
homeServer = firstConfig.credentials.homeServer, homeServer = legacyConfig.credentials.homeServer,
deviceId = firstConfig.credentials.deviceId, deviceId = legacyConfig.credentials.deviceId,
discoveryInformation = firstConfig.credentials.wellKnown?.let { wellKnown -> discoveryInformation = legacyConfig.credentials.wellKnown?.let { wellKnown ->
// Note credentials.wellKnown is not serialized in the LoginStorage, so this code is a bit useless... // Note credentials.wellKnown is not serialized in the LoginStorage, so this code is a bit useless...
if (wellKnown.homeServer?.baseURL != null if (wellKnown.homeServer?.baseURL != null
|| wellKnown.identityServer?.baseURL != null) { || wellKnown.identityServer?.baseURL != null) {
@ -71,44 +86,35 @@ internal class DefaultLegacySessionImporter @Inject constructor(
} }
), ),
homeServerConnectionConfig = HomeServerConnectionConfig( homeServerConnectionConfig = HomeServerConnectionConfig(
homeServerUri = firstConfig.homeserverUri, homeServerUri = legacyConfig.homeserverUri,
identityServerUri = firstConfig.identityServerUri, identityServerUri = legacyConfig.identityServerUri,
antiVirusServerUri = firstConfig.antiVirusServerUri, antiVirusServerUri = legacyConfig.antiVirusServerUri,
allowedFingerprints = firstConfig.allowedFingerprints.map { allowedFingerprints = legacyConfig.allowedFingerprints.map {
Fingerprint( Fingerprint(
bytes = it.bytes, bytes = it.bytes,
hashType = when (it.type) { hashType = when (it.type) {
im.vector.matrix.android.internal.legacy.riot.Fingerprint.HashType.SHA1, LegacyFingerprint.HashType.SHA1,
null -> Fingerprint.HashType.SHA1 null -> Fingerprint.HashType.SHA1
im.vector.matrix.android.internal.legacy.riot.Fingerprint.HashType.SHA256 -> Fingerprint.HashType.SHA256 LegacyFingerprint.HashType.SHA256 -> Fingerprint.HashType.SHA256
} }
) )
}, },
shouldPin = firstConfig.shouldPin(), shouldPin = legacyConfig.shouldPin(),
tlsVersions = firstConfig.acceptedTlsVersions, tlsVersions = legacyConfig.acceptedTlsVersions,
tlsCipherSuites = firstConfig.acceptedTlsCipherSuites, tlsCipherSuites = legacyConfig.acceptedTlsCipherSuites,
shouldAcceptTlsExtensions = firstConfig.shouldAcceptTlsExtensions(), shouldAcceptTlsExtensions = legacyConfig.shouldAcceptTlsExtensions(),
allowHttpExtension = false, // TODO allowHttpExtension = false, // TODO
forceUsageTlsVersions = firstConfig.forceUsageOfTlsVersions() forceUsageTlsVersions = legacyConfig.forceUsageOfTlsVersions()
), ),
// If token is not valid, this boolean will be updated later // If token is not valid, this boolean will be updated later
isTokenValid = true isTokenValid = true
) )
Timber.d("Migration: save session") Timber.d("Migration: save session")
GlobalScope.launch {
sessionParamsStore.save(sessionParams) sessionParamsStore.save(sessionParams)
} }
Timber.d("Migration: clear legacy session") private suspend fun importCryptoDb(legacyConfig: LegacyHomeServerConnectionConfig) {
TODO("Not yet implemented")
// Delete to avoid doing this several times
loginStorage.clear()
// TODO Crypto DB
// TODO Remove
Thread.sleep(5000)
} }
} }