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 currentDevices = userEntity.devices.toList()
val currentKnownDevices = u.devices.toList() val newDevices = devices.values.map { cryptoDeviceInfo -> cryptoDeviceInfo.toEntity() }
val new = devices.map { entry -> entry.value.toEntity() } newDevices.forEach { deviceInfoEntity ->
new.forEach { entity -> // Maintain first time seen
// Maintain first time seen val existing = currentDevices
val existing = currentKnownDevices.firstOrNull { it.deviceId == entity.deviceId && it.identityKey == entity.identityKey } .firstOrNull { it.deviceId == deviceInfoEntity.deviceId && it.identityKey == deviceInfoEntity.identityKey }
entity.firstTimeSeenLocalTs = existing?.firstTimeSeenLocalTs ?: System.currentTimeMillis() deviceInfoEntity.firstTimeSeenLocalTs = existing?.firstTimeSeenLocalTs ?: System.currentTimeMillis()
realm.insertOrUpdate(entity) 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)
}
} }
} }
} }