Skip to content

Commit b23520e

Browse files
author
Maxime NATUREL
committed
Adding new field for last seen user agent in DB with migration
1 parent 3be1513 commit b23520e

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,7 @@ internal class RealmCryptoStore @Inject constructor(
624624
}
625625

626626
override fun saveMyDevicesInfo(info: List<DeviceInfo>) {
627-
val entities = info.map {
628-
MyDeviceLastSeenInfoEntity(
629-
lastSeenTs = it.lastSeenTs,
630-
lastSeenIp = it.lastSeenIp,
631-
displayName = it.displayName,
632-
deviceId = it.deviceId
633-
)
634-
}
627+
val entities = info.map { myDeviceLastSeenInfoEntityMapper.map(it) }
635628
doRealmTransactionAsync(realmConfiguration) { realm ->
636629
realm.where<MyDeviceLastSeenInfoEntity>().findAll().deleteAllFromRealm()
637630
entities.forEach {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo
3636
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo017
3737
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo018
3838
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo019
39+
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo020
3940
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
4041
import org.matrix.android.sdk.internal.util.time.Clock
4142
import javax.inject.Inject
@@ -50,7 +51,7 @@ internal class RealmCryptoStoreMigration @Inject constructor(
5051
private val clock: Clock,
5152
) : MatrixRealmMigration(
5253
dbName = "Crypto",
53-
schemaVersion = 19L,
54+
schemaVersion = 20L,
5455
) {
5556
/**
5657
* Forces all RealmCryptoStoreMigration instances to be equal.
@@ -79,5 +80,6 @@ internal class RealmCryptoStoreMigration @Inject constructor(
7980
if (oldVersion < 17) MigrateCryptoTo017(realm).perform()
8081
if (oldVersion < 18) MigrateCryptoTo018(realm).perform()
8182
if (oldVersion < 19) MigrateCryptoTo019(realm).perform()
83+
if (oldVersion < 20) MigrateCryptoTo020(realm).perform()
8284
}
8385
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/mapper/MyDeviceLastSeenInfoEntityMapper.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ import javax.inject.Inject
2222

2323
internal class MyDeviceLastSeenInfoEntityMapper @Inject constructor() {
2424

25+
// TODO add unit tests
2526
fun map(entity: MyDeviceLastSeenInfoEntity): DeviceInfo {
2627
return DeviceInfo(
2728
deviceId = entity.deviceId,
2829
lastSeenIp = entity.lastSeenIp,
2930
lastSeenTs = entity.lastSeenTs,
30-
displayName = entity.displayName
31+
displayName = entity.displayName,
32+
unstableLastSeenUserAgent = entity.lastSeenUserAgent,
33+
)
34+
}
35+
36+
fun map(deviceInfo: DeviceInfo): MyDeviceLastSeenInfoEntity {
37+
return MyDeviceLastSeenInfoEntity(
38+
deviceId = deviceInfo.deviceId,
39+
lastSeenIp = deviceInfo.lastSeenIp,
40+
lastSeenTs = deviceInfo.lastSeenTs,
41+
displayName = deviceInfo.displayName,
42+
lastSeenUserAgent = deviceInfo.getBestLastSeenUserAgent(),
3143
)
3244
}
3345
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo019.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import org.matrix.android.sdk.internal.util.database.RealmMigrator
3030
* mark existing keys as safe.
3131
* This migration can take long depending on the account
3232
*/
33-
internal class MigrateCryptoTo019(realm: DynamicRealm) : RealmMigrator(realm, 18) {
33+
internal class MigrateCryptoTo019(realm: DynamicRealm) : RealmMigrator(realm, 19) {
3434

3535
override fun doMigrate(realm: DynamicRealm) {
3636
realm.schema.get("CrossSigningInfoEntity")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.crypto.store.db.migration
18+
19+
import io.realm.DynamicRealm
20+
import org.matrix.android.sdk.internal.crypto.store.db.model.MyDeviceLastSeenInfoEntityFields
21+
import org.matrix.android.sdk.internal.util.database.RealmMigrator
22+
23+
/**
24+
* This migration adds a new field into MyDeviceLastSeenInfoEntity corresponding to the last seen user agent.
25+
*/
26+
internal class MigrateCryptoTo020(realm: DynamicRealm) : RealmMigrator(realm, 20) {
27+
28+
override fun doMigrate(realm: DynamicRealm) {
29+
realm.schema.get("MyDeviceLastSeenInfoEntity")
30+
?.addField(MyDeviceLastSeenInfoEntityFields.LAST_SEEN_USER_AGENT, String::class.java)
31+
}
32+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/model/MyDeviceLastSeenInfoEntity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ internal open class MyDeviceLastSeenInfoEntity(
2727
/** The last time this device has been seen. */
2828
var lastSeenTs: Long? = null,
2929
/** The last ip address. */
30-
var lastSeenIp: String? = null
30+
var lastSeenIp: String? = null,
31+
/** The last user agent. */
32+
var lastSeenUserAgent: String? = null,
3133
) : RealmObject() {
3234

3335
companion object

0 commit comments

Comments
 (0)