Async everything!

pull/42/head
Mikunj 6 years ago
parent 457faae5a5
commit 8eedff81eb

@ -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,46 +182,42 @@ 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 }
// Get the set of primary device pubKeys FROM the secondary devices in uniqueDevices
val newDisplayNameUpdatees = uniqueDevices.mapNotNull {
// This will return null if current device is primary
// So if it's non-null then we know the device is a secondary device
val primaryDevice = LokiStorageAPI.shared.getPrimaryDevicePublicKey(it).get()
primaryDevice
}.toSet()
// Fetch the display names of the primary devices
displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees)
// Process messages in the background
messages.forEach { message ->
if (ourDevices.contains(message.hexEncodedPublicKey)) {
processOutgoingMessage(message)
} else {
processIncomingMessage(message)
}
}
} }
}
Promise.of(messages)
}.successBackground {
// Get the set of primary device pubKeys FROM the secondary devices in uniqueDevices
val newDisplayNameUpdatees = uniqueDevices.mapNotNull {
// This will return null if current device is primary
// So if it's non-null then we know the device is a secondary device
val primaryDevice = LokiStorageAPI.shared.getPrimaryDevicePublicKey(it).get()
primaryDevice
}.toSet()
// We need to fetch device mappings for all the devices we don't have // Fetch the display names of the primary devices
if (devicesToUpdate.isEmpty()) { displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees)
proceed() }.success { messages ->
} else { // Process messages in the background
// Fetch the device mappings first messages.forEach { message ->
try { AsyncTask.execute {
LokiStorageAPI.shared.getDeviceMappings(devicesToUpdate.toSet()).get() if (ourDevices.contains(message.hexEncodedPublicKey)) {
} finally { processOutgoingMessage(message)
proceed() } else {
processIncomingMessage(message)
} }
} }
} }
}.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}.")

Loading…
Cancel
Save