Async everything!

This commit is contained in:
Mikunj 2019-11-18 11:55:16 +11:00
parent 457faae5a5
commit 8eedff81eb

View File

@ -1,8 +1,12 @@
package org.thoughtcrime.securesms.loki package org.thoughtcrime.securesms.loki
import android.content.Context import android.content.Context
import android.os.AsyncTask
import android.os.Handler import android.os.Handler
import android.util.Log import android.util.Log
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.then
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.thoughtcrime.securesms.jobs.PushDecryptJob
@ -178,13 +182,22 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
} }
} }
api.getMessages(group.channel, group.server).successBackground { messages -> var ourDevices = setOf<String>()
var uniqueDevices = setOf<String>()
LokiStorageAPI.shared.getAllDevicePublicKeys(userHexEncodedPublicKey).bind { devices ->
ourDevices = devices
api.getMessages(group.channel, group.server)
}.bind { messages ->
if (messages.isNotEmpty()) { if (messages.isNotEmpty()) {
val ourDevices = LokiStorageAPI.shared.getAllDevicePublicKeys(userHexEncodedPublicKey).get(setOf()) // We need to fetch device mappings for all the devices we don't have
val uniqueDevices = messages.map { it.hexEncodedPublicKey }.toSet() uniqueDevices = messages.map { it.hexEncodedPublicKey }.toSet()
val devicesToUpdate = uniqueDevices.filter { !ourDevices.contains(it) && LokiStorageAPI.shared.hasCacheExpired(it) } val devicesToUpdate = uniqueDevices.filter { !ourDevices.contains(it) && LokiStorageAPI.shared.hasCacheExpired(it) }
if (devicesToUpdate.isNotEmpty()) {
fun proceed() { return@bind LokiStorageAPI.shared.getDeviceMappings(devicesToUpdate.toSet()).then { messages }
}
}
Promise.of(messages)
}.successBackground {
// Get the set of primary device pubKeys FROM the secondary devices in uniqueDevices // Get the set of primary device pubKeys FROM the secondary devices in uniqueDevices
val newDisplayNameUpdatees = uniqueDevices.mapNotNull { val newDisplayNameUpdatees = uniqueDevices.mapNotNull {
// This will return null if current device is primary // This will return null if current device is primary
@ -195,9 +208,10 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
// Fetch the display names of the primary devices // Fetch the display names of the primary devices
displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees) displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees)
}.success { messages ->
// Process messages in the background // Process messages in the background
messages.forEach { message -> messages.forEach { message ->
AsyncTask.execute {
if (ourDevices.contains(message.hexEncodedPublicKey)) { if (ourDevices.contains(message.hexEncodedPublicKey)) {
processOutgoingMessage(message) processOutgoingMessage(message)
} else { } else {
@ -205,20 +219,6 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
} }
} }
} }
// We need to fetch device mappings for all the devices we don't have
if (devicesToUpdate.isEmpty()) {
proceed()
} else {
// Fetch the device mappings first
try {
LokiStorageAPI.shared.getDeviceMappings(devicesToUpdate.toSet()).get()
} finally {
proceed()
}
}
}
}.fail { }.fail {
Log.d("Loki", "Failed to get messages for group chat with ID: ${group.channel} on server: ${group.server}.") Log.d("Loki", "Failed to get messages for group chat with ID: ${group.channel} on server: ${group.server}.")
} }