mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-29 14:38:45 +03:00
crypto: Connect the live CryptoDeviceInfo getter methods to the rust-sdk
This commit is contained in:
parent
ef93d9e625
commit
0b064f647a
2 changed files with 72 additions and 3 deletions
|
@ -410,15 +410,19 @@ internal class DefaultCryptoService @Inject constructor(
|
|||
}
|
||||
|
||||
override fun getCryptoDeviceInfo(userId: String): List<CryptoDeviceInfo> {
|
||||
return cryptoStore.getUserDeviceList(userId).orEmpty()
|
||||
return runBlocking {
|
||||
olmMachine!!.getUserDevices(userId)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getLiveCryptoDeviceInfo(userId: String): LiveData<List<CryptoDeviceInfo>> {
|
||||
return cryptoStore.getLiveDeviceList(userId)
|
||||
return getLiveCryptoDeviceInfo(listOf(userId))
|
||||
}
|
||||
|
||||
override fun getLiveCryptoDeviceInfo(userIds: List<String>): LiveData<List<CryptoDeviceInfo>> {
|
||||
return cryptoStore.getLiveDeviceList(userIds)
|
||||
return runBlocking {
|
||||
olmMachine!!.getLiveDevices(userIds)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.matrix.android.sdk.internal
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.LiveData
|
||||
import java.io.File
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
@ -61,6 +63,26 @@ private class CryptoProgressListener(listener: ProgressListener?) : RustProgress
|
|||
}
|
||||
}
|
||||
|
||||
internal class LiveDevice(
|
||||
userIds: List<String>,
|
||||
machine: OlmMachine
|
||||
) : MutableLiveData<List<CryptoDeviceInfo>>() {
|
||||
var userIds: List<String> = userIds
|
||||
private var machine: OlmMachine = machine
|
||||
|
||||
private val listener = { devices: List<CryptoDeviceInfo> ->
|
||||
value = devices
|
||||
}
|
||||
|
||||
override fun onActive() {
|
||||
machine.addDeviceUpdateListener(this)
|
||||
}
|
||||
|
||||
override fun onInactive() {
|
||||
machine.removeDeviceUpdateListener(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun setRustLogger() {
|
||||
setLogger(CryptoLogger() as Logger)
|
||||
}
|
||||
|
@ -108,6 +130,7 @@ class Device(inner: InnerDevice, machine: InnerMachine) {
|
|||
|
||||
internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
||||
private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString())
|
||||
private val deviceUpdateListeners = HashMap<LiveDevice, List<String>>()
|
||||
|
||||
fun userId(): String {
|
||||
return this.inner.userId()
|
||||
|
@ -136,6 +159,21 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
|||
)
|
||||
}
|
||||
|
||||
fun addDeviceUpdateListener(device: LiveDevice) {
|
||||
deviceUpdateListeners.set(device, device.userIds)
|
||||
}
|
||||
|
||||
fun removeDeviceUpdateListener(device: LiveDevice) {
|
||||
deviceUpdateListeners.remove(device)
|
||||
}
|
||||
|
||||
suspend fun updateLiveDevices() {
|
||||
for ((liveDevice, users) in deviceUpdateListeners) {
|
||||
val devices = getUserDevices(users)
|
||||
liveDevice.postValue(devices)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun outgoingRequests(): List<Request> = withContext(Dispatchers.IO) {
|
||||
inner.outgoingRequests()
|
||||
}
|
||||
|
@ -183,6 +221,10 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
|||
response_body: String
|
||||
) = withContext(Dispatchers.IO) {
|
||||
inner.markRequestAsSent(request_id, request_type, response_body)
|
||||
|
||||
if (request_type == RequestType.KEYS_QUERY) {
|
||||
updateLiveDevices()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getDevice(user_id: String, device_id: String): Device? = withContext(Dispatchers.IO) {
|
||||
|
@ -192,6 +234,29 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
|||
}
|
||||
}
|
||||
|
||||
suspend fun getUserDevices(userId: String): List<CryptoDeviceInfo> {
|
||||
return inner.getUserDevices(userId).map { Device(it, inner).toCryptoDeviceInfo() }
|
||||
}
|
||||
|
||||
suspend fun getUserDevices(userIds: List<String>): List<CryptoDeviceInfo> {
|
||||
val plainDevices: ArrayList<CryptoDeviceInfo> = arrayListOf()
|
||||
|
||||
for (user in userIds) {
|
||||
val devices = getUserDevices(user)
|
||||
plainDevices.addAll(devices)
|
||||
}
|
||||
|
||||
return plainDevices
|
||||
}
|
||||
|
||||
suspend fun getLiveDevices(userIds: List<String>): LiveData<List<CryptoDeviceInfo>> {
|
||||
val plainDevices = getUserDevices(userIds)
|
||||
val devices = LiveDevice(userIds, this)
|
||||
devices.setValue(plainDevices)
|
||||
|
||||
return devices
|
||||
}
|
||||
|
||||
@Throws(CryptoStoreErrorException::class)
|
||||
suspend fun exportKeys(passphrase: String, rounds: Int): ByteArray = withContext(Dispatchers.IO) {
|
||||
inner.exportKeys(passphrase, rounds).toByteArray()
|
||||
|
|
Loading…
Reference in a new issue