Adding new field for last seen user agent in DB with migration

This commit is contained in:
Maxime NATUREL 2022-10-04 17:51:03 +02:00
parent 3be1513e0f
commit b23520ea40
6 changed files with 53 additions and 12 deletions

View file

@ -624,14 +624,7 @@ internal class RealmCryptoStore @Inject constructor(
}
override fun saveMyDevicesInfo(info: List<DeviceInfo>) {
val entities = info.map {
MyDeviceLastSeenInfoEntity(
lastSeenTs = it.lastSeenTs,
lastSeenIp = it.lastSeenIp,
displayName = it.displayName,
deviceId = it.deviceId
)
}
val entities = info.map { myDeviceLastSeenInfoEntityMapper.map(it) }
doRealmTransactionAsync(realmConfiguration) { realm ->
realm.where<MyDeviceLastSeenInfoEntity>().findAll().deleteAllFromRealm()
entities.forEach {

View file

@ -36,6 +36,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo017
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo018
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo019
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo020
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
import org.matrix.android.sdk.internal.util.time.Clock
import javax.inject.Inject
@ -50,7 +51,7 @@ internal class RealmCryptoStoreMigration @Inject constructor(
private val clock: Clock,
) : MatrixRealmMigration(
dbName = "Crypto",
schemaVersion = 19L,
schemaVersion = 20L,
) {
/**
* Forces all RealmCryptoStoreMigration instances to be equal.
@ -79,5 +80,6 @@ internal class RealmCryptoStoreMigration @Inject constructor(
if (oldVersion < 17) MigrateCryptoTo017(realm).perform()
if (oldVersion < 18) MigrateCryptoTo018(realm).perform()
if (oldVersion < 19) MigrateCryptoTo019(realm).perform()
if (oldVersion < 20) MigrateCryptoTo020(realm).perform()
}
}

View file

@ -22,12 +22,24 @@ import javax.inject.Inject
internal class MyDeviceLastSeenInfoEntityMapper @Inject constructor() {
// TODO add unit tests
fun map(entity: MyDeviceLastSeenInfoEntity): DeviceInfo {
return DeviceInfo(
deviceId = entity.deviceId,
lastSeenIp = entity.lastSeenIp,
lastSeenTs = entity.lastSeenTs,
displayName = entity.displayName
displayName = entity.displayName,
unstableLastSeenUserAgent = entity.lastSeenUserAgent,
)
}
fun map(deviceInfo: DeviceInfo): MyDeviceLastSeenInfoEntity {
return MyDeviceLastSeenInfoEntity(
deviceId = deviceInfo.deviceId,
lastSeenIp = deviceInfo.lastSeenIp,
lastSeenTs = deviceInfo.lastSeenTs,
displayName = deviceInfo.displayName,
lastSeenUserAgent = deviceInfo.getBestLastSeenUserAgent(),
)
}
}

View file

@ -30,7 +30,7 @@ import org.matrix.android.sdk.internal.util.database.RealmMigrator
* mark existing keys as safe.
* This migration can take long depending on the account
*/
internal class MigrateCryptoTo019(realm: DynamicRealm) : RealmMigrator(realm, 18) {
internal class MigrateCryptoTo019(realm: DynamicRealm) : RealmMigrator(realm, 19) {
override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("CrossSigningInfoEntity")

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.crypto.store.db.migration
import io.realm.DynamicRealm
import org.matrix.android.sdk.internal.crypto.store.db.model.MyDeviceLastSeenInfoEntityFields
import org.matrix.android.sdk.internal.util.database.RealmMigrator
/**
* This migration adds a new field into MyDeviceLastSeenInfoEntity corresponding to the last seen user agent.
*/
internal class MigrateCryptoTo020(realm: DynamicRealm) : RealmMigrator(realm, 20) {
override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("MyDeviceLastSeenInfoEntity")
?.addField(MyDeviceLastSeenInfoEntityFields.LAST_SEEN_USER_AGENT, String::class.java)
}
}

View file

@ -27,7 +27,9 @@ internal open class MyDeviceLastSeenInfoEntity(
/** The last time this device has been seen. */
var lastSeenTs: Long? = null,
/** The last ip address. */
var lastSeenIp: String? = null
var lastSeenIp: String? = null,
/** The last user agent. */
var lastSeenUserAgent: String? = null,
) : RealmObject() {
companion object