This commit is contained in:
Niels Andriesse 2019-11-18 13:12:57 +11:00
parent 8eedff81eb
commit 638d693e11

View File

@ -22,7 +22,6 @@ import org.whispersystems.signalservice.loki.api.LokiPublicChat
import org.whispersystems.signalservice.loki.api.LokiPublicChatAPI import org.whispersystems.signalservice.loki.api.LokiPublicChatAPI
import org.whispersystems.signalservice.loki.api.LokiPublicChatMessage import org.whispersystems.signalservice.loki.api.LokiPublicChatMessage
import org.whispersystems.signalservice.loki.api.LokiStorageAPI import org.whispersystems.signalservice.loki.api.LokiStorageAPI
import org.whispersystems.signalservice.loki.utilities.get
import org.whispersystems.signalservice.loki.utilities.successBackground import org.whispersystems.signalservice.loki.utilities.successBackground
import java.util.* import java.util.*
@ -81,7 +80,7 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
private val pollForNewMessagesInterval: Long = 4 * 1000 private val pollForNewMessagesInterval: Long = 4 * 1000
private val pollForDeletedMessagesInterval: Long = 20 * 1000 private val pollForDeletedMessagesInterval: Long = 20 * 1000
private val pollForModeratorsInterval: Long = 10 * 60 * 1000 private val pollForModeratorsInterval: Long = 10 * 60 * 1000
private val pollForDisplayNamesInterval: Long = 1 * 60 * 1000 private val pollForDisplayNamesInterval: Long = 60 * 1000
} }
// endregion // endregion
@ -181,17 +180,16 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
PushDecryptJob(context).handleSynchronizeSentTextMessage(transcript) PushDecryptJob(context).handleSynchronizeSentTextMessage(transcript)
} }
} }
var userDevices = setOf<String>()
var ourDevices = setOf<String>()
var uniqueDevices = setOf<String>() var uniqueDevices = setOf<String>()
LokiStorageAPI.shared.getAllDevicePublicKeys(userHexEncodedPublicKey).bind { devices -> LokiStorageAPI.shared.getAllDevicePublicKeys(userHexEncodedPublicKey).bind { devices ->
ourDevices = devices userDevices = devices
api.getMessages(group.channel, group.server) api.getMessages(group.channel, group.server)
}.bind { messages -> }.bind { messages ->
if (messages.isNotEmpty()) { if (messages.isNotEmpty()) {
// We need to fetch device mappings for all the devices we don't have // We need to fetch device mappings for all the devices we don't have
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 { !userDevices.contains(it) && LokiStorageAPI.shared.hasCacheExpired(it) }
if (devicesToUpdate.isNotEmpty()) { if (devicesToUpdate.isNotEmpty()) {
return@bind LokiStorageAPI.shared.getDeviceMappings(devicesToUpdate.toSet()).then { messages } return@bind LokiStorageAPI.shared.getDeviceMappings(devicesToUpdate.toSet()).then { messages }
} }
@ -205,14 +203,13 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
val primaryDevice = LokiStorageAPI.shared.getPrimaryDevicePublicKey(it).get() val primaryDevice = LokiStorageAPI.shared.getPrimaryDevicePublicKey(it).get()
primaryDevice primaryDevice
}.toSet() }.toSet()
// 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 -> }.success { messages ->
// Process messages in the background // Process messages in the background
messages.forEach { message -> messages.forEach { message ->
AsyncTask.execute { AsyncTask.execute {
if (ourDevices.contains(message.hexEncodedPublicKey)) { if (userDevices.contains(message.hexEncodedPublicKey)) {
processOutgoingMessage(message) processOutgoingMessage(message)
} else { } else {
processIncomingMessage(message) processIncomingMessage(message)
@ -226,18 +223,15 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
private fun pollForDisplayNames() { private fun pollForDisplayNames() {
if (displayNameUpdatees.isEmpty()) { return } if (displayNameUpdatees.isEmpty()) { return }
val hexEncodedPublicKeys = displayNameUpdatees
val devices = displayNameUpdatees
displayNameUpdatees = setOf() displayNameUpdatees = setOf()
api.getDisplayNames(hexEncodedPublicKeys, group.server).successBackground { mapping ->
api.getDisplayNames(devices, group.server).successBackground { mapping ->
for (pair in mapping.entries) { for (pair in mapping.entries) {
val senderDisplayName = "${pair.value} (...${pair.key.takeLast(8)})" val senderDisplayName = "${pair.value} (...${pair.key.takeLast(8)})"
DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, pair.key, senderDisplayName) DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, pair.key, senderDisplayName)
} }
}.fail { }.fail {
// Retry next time displayNameUpdatees = displayNameUpdatees.union(hexEncodedPublicKeys)
displayNameUpdatees = displayNameUpdatees.union(devices)
} }
} }