Fix IllegalStateException in UpdateTrustWorker

Also split into 2 methods
This commit is contained in:
Benoit Marty 2021-03-17 12:42:28 +01:00
parent e89e0724a0
commit 616d3c1988

View File

@ -97,22 +97,17 @@ internal class UpdateTrustWorker(context: Context,
// Unfortunately we don't have much info on what did exactly changed (is it the cross signing keys of that user, // Unfortunately we don't have much info on what did exactly changed (is it the cross signing keys of that user,
// or a new device?) So we check all again :/ // or a new device?) So we check all again :/
Timber.d("## CrossSigning - Updating trust for users: ${userList.logLimit()}") Timber.d("## CrossSigning - Updating trust for users: ${userList.logLimit()}")
updateTrust(userList)
Realm.getInstance(cryptoRealmConfiguration).use { cryptoRealm ->
Realm.getInstance(sessionRealmConfiguration).use {
updateTrust(userList, cryptoRealm)
}
}
} }
cleanup(params) cleanup(params)
return Result.success() return Result.success()
} }
private suspend fun updateTrust(userListParam: List<String>, private suspend fun updateTrust(userListParam: List<String>) {
cRealm: Realm) {
var userList = userListParam var userList = userListParam
var myCrossSigningInfo: MXCrossSigningInfo? = null var myCrossSigningInfo: MXCrossSigningInfo? = null
// First we check that the users MSK are trusted by mine // First we check that the users MSK are trusted by mine
// After that we check the trust chain for each devices of each users // After that we check the trust chain for each devices of each users
awaitTransaction(cryptoRealmConfiguration) { cryptoRealm -> awaitTransaction(cryptoRealmConfiguration) { cryptoRealm ->
@ -203,38 +198,43 @@ internal class UpdateTrustWorker(context: Context,
// So Cross Signing keys trust is updated, device trust is updated // So Cross Signing keys trust is updated, device trust is updated
// We can now update room shields? in the session DB? // We can now update room shields? in the session DB?
updateTrustStep2(userList, myCrossSigningInfo)
}
private suspend fun updateTrustStep2(userList: List<String>, myCrossSigningInfo: MXCrossSigningInfo?) {
Timber.d("## CrossSigning - Updating shields for impacted rooms...") Timber.d("## CrossSigning - Updating shields for impacted rooms...")
awaitTransaction(sessionRealmConfiguration) { sessionRealm -> awaitTransaction(sessionRealmConfiguration) { sessionRealm ->
sessionRealm.where(RoomMemberSummaryEntity::class.java) Realm.getInstance(cryptoRealmConfiguration).use { cryptoRealm ->
.`in`(RoomMemberSummaryEntityFields.USER_ID, userList.toTypedArray()) sessionRealm.where(RoomMemberSummaryEntity::class.java)
.distinct(RoomMemberSummaryEntityFields.ROOM_ID) .`in`(RoomMemberSummaryEntityFields.USER_ID, userList.toTypedArray())
.findAll() .distinct(RoomMemberSummaryEntityFields.ROOM_ID)
.map { it.roomId } .findAll()
.also { Timber.d("## CrossSigning - ... impacted rooms ${it.logLimit()}") } .map { it.roomId }
.forEach { roomId -> .also { Timber.d("## CrossSigning - ... impacted rooms ${it.logLimit()}") }
RoomSummaryEntity.where(sessionRealm, roomId) .forEach { roomId ->
.equalTo(RoomSummaryEntityFields.IS_ENCRYPTED, true) RoomSummaryEntity.where(sessionRealm, roomId)
.findFirst() .equalTo(RoomSummaryEntityFields.IS_ENCRYPTED, true)
?.let { roomSummary -> .findFirst()
Timber.d("## CrossSigning - Check shield state for room $roomId") ?.let { roomSummary ->
val allActiveRoomMembers = RoomMemberHelper(sessionRealm, roomId).getActiveRoomMemberIds() Timber.d("## CrossSigning - Check shield state for room $roomId")
try { val allActiveRoomMembers = RoomMemberHelper(sessionRealm, roomId).getActiveRoomMemberIds()
val updatedTrust = computeRoomShield( try {
myCrossSigningInfo, val updatedTrust = computeRoomShield(
cRealm, myCrossSigningInfo,
allActiveRoomMembers, cryptoRealm,
roomSummary allActiveRoomMembers,
) roomSummary
if (roomSummary.roomEncryptionTrustLevel != updatedTrust) { )
Timber.d("## CrossSigning - Shield change detected for $roomId -> $updatedTrust") if (roomSummary.roomEncryptionTrustLevel != updatedTrust) {
roomSummary.roomEncryptionTrustLevel = updatedTrust Timber.d("## CrossSigning - Shield change detected for $roomId -> $updatedTrust")
roomSummary.roomEncryptionTrustLevel = updatedTrust
}
} catch (failure: Throwable) {
Timber.e(failure)
} }
} catch (failure: Throwable) {
Timber.e(failure)
} }
} }
} }
} }
} }