Use new isUserTracked API

This commit is contained in:
Valere 2021-11-25 15:42:52 +01:00
parent 9118b26d2f
commit 7fd9ca03be

View file

@ -334,7 +334,6 @@ internal class OlmMachine(
suspend fun updateTrackedUsers(users: List<String>) = suspend fun updateTrackedUsers(users: List<String>) =
withContext(Dispatchers.IO) { inner.updateTrackedUsers(users) } withContext(Dispatchers.IO) { inner.updateTrackedUsers(users) }
/** /**
* Check if the given user is considered to be tracked. * Check if the given user is considered to be tracked.
* A user can be marked for tracking using the * A user can be marked for tracking using the
@ -647,14 +646,12 @@ internal class OlmMachine(
} }
@Throws @Throws
suspend fun forceKeyDownload(userIds: List<String>): MXUsersDevicesMap<CryptoDeviceInfo> { suspend fun forceKeyDownload(userIds: List<String>) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val requestId = UUID.randomUUID().toString() val requestId = UUID.randomUUID().toString()
val response = requestSender.queryKeys(Request.KeysQuery(requestId, userIds)) val response = requestSender.queryKeys(Request.KeysQuery(requestId, userIds))
markRequestAsSent(requestId, RequestType.KEYS_QUERY, response) markRequestAsSent(requestId, RequestType.KEYS_QUERY, response)
} }
// TODO notify shield listener (?)
return getUserDevicesMap(userIds)
} }
suspend fun getUserDevicesMap(userIds: List<String>): MXUsersDevicesMap<CryptoDeviceInfo> { suspend fun getUserDevicesMap(userIds: List<String>): MXUsersDevicesMap<CryptoDeviceInfo> {
@ -674,21 +671,24 @@ internal class OlmMachine(
/** /**
* If the user is untracked or forceDownload is set to true, a key query request will be made. * If the user is untracked or forceDownload is set to true, a key query request will be made.
* It will suspend until query response, and the device list will be returned. * It will suspend until query response, and the device list will be returned.
* As there is no API to know tracking status, we consider that if there are no known devices the user is un tracked.
* *
* The key query request will be retried a few time in case of shaky connection, but could fail. * The key query request will be retried a few time in case of shaky connection, but could fail.
*/ */
suspend fun ensureUserDevicesMap(userIds: List<String>, forceDownload: Boolean = false): MXUsersDevicesMap<CryptoDeviceInfo> { suspend fun ensureUserDevicesMap(userIds: List<String>, forceDownload: Boolean = false): MXUsersDevicesMap<CryptoDeviceInfo> {
val known = getUserDevicesMap(userIds) val toDownload = if (forceDownload) {
return if (known.isEmpty /* We have no api to know if was tracked..*/ || forceDownload) { userIds
updateTrackedUsers(userIds)
tryOrNull("Failed to download keys for $userIds") {
forceKeyDownload(userIds)
} ?: known
} else { } else {
known userIds.mapNotNull { userId ->
userId.takeIf { !isUserTracked(it) }
}.also {
updateTrackedUsers(it)
} }
} }
tryOrNull("Failed to download keys for $toDownload") {
forceKeyDownload(toDownload)
}
return getUserDevicesMap(userIds)
}
suspend fun getLiveUserIdentity(userId: String): LiveData<Optional<MXCrossSigningInfo>> { suspend fun getLiveUserIdentity(userId: String): LiveData<Optional<MXCrossSigningInfo>> {
val identity = this.getIdentity(userId)?.toMxCrossSigningInfo().toOptional() val identity = this.getIdentity(userId)?.toMxCrossSigningInfo().toOptional()