Small optimization

This commit is contained in:
Benoit Marty 2021-07-29 15:39:03 +02:00
parent 00911a7f7e
commit 5eb794f8af

View File

@ -283,21 +283,20 @@ internal class RealmCryptoStore @Inject constructor(
// Remove the user // Remove the user
UserEntity.delete(realm, userId) UserEntity.delete(realm, userId)
} else { } else {
UserEntity.getOrCreate(realm, userId) val userEntity = UserEntity.getOrCreate(realm, userId)
.let { u ->
// Add the devices // Add the devices
val currentKnownDevices = u.devices.toList() val currentDevices = userEntity.devices.toList()
val new = devices.map { entry -> entry.value.toEntity() } val newDevices = devices.values.map { cryptoDeviceInfo -> cryptoDeviceInfo.toEntity() }
new.forEach { entity -> newDevices.forEach { deviceInfoEntity ->
// Maintain first time seen // Maintain first time seen
val existing = currentKnownDevices.firstOrNull { it.deviceId == entity.deviceId && it.identityKey == entity.identityKey } val existing = currentDevices
entity.firstTimeSeenLocalTs = existing?.firstTimeSeenLocalTs ?: System.currentTimeMillis() .firstOrNull { it.deviceId == deviceInfoEntity.deviceId && it.identityKey == deviceInfoEntity.identityKey }
realm.insertOrUpdate(entity) deviceInfoEntity.firstTimeSeenLocalTs = existing?.firstTimeSeenLocalTs ?: System.currentTimeMillis()
realm.insertOrUpdate(deviceInfoEntity)
} }
// Ensure all other devices are deleted // Ensure all other devices are deleted
u.devices.clearWith { it.deleteOnCascade() } userEntity.devices.clearWith { it.deleteOnCascade() }
u.devices.addAll(new) userEntity.devices.addAll(newDevices)
}
} }
} }
} }