diff --git a/src/org/thoughtcrime/securesms/loki/redesign/messaging/LokiPublicChatPoller.kt b/src/org/thoughtcrime/securesms/loki/redesign/messaging/LokiPublicChatPoller.kt index 1d98b3e761..51f2e59c51 100644 --- a/src/org/thoughtcrime/securesms/loki/redesign/messaging/LokiPublicChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/redesign/messaging/LokiPublicChatPoller.kt @@ -23,6 +23,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptM import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.loki.api.* import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus +import org.whispersystems.signalservice.loki.utilities.successBackground import java.security.MessageDigest import java.util.* @@ -236,24 +237,22 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki } } Promise.of(messages) - }.success { + }.successBackground { val newDisplayNameUpdatees = uniqueDevices.mapNotNull { // This will return null if the current device is a master device LokiDeviceLinkUtilities.getMasterHexEncodedPublicKey(it).get() }.toSet() // Fetch the display names of the master devices displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees) - }.success { messages -> + }.successBackground { messages -> // Process messages in the background - Thread { - messages.forEach { message -> - if (userDevices.contains(message.hexEncodedPublicKey)) { - processOutgoingMessage(message) - } else { - processIncomingMessage(message) - } + messages.forEach { message -> + if (userDevices.contains(message.hexEncodedPublicKey)) { + processOutgoingMessage(message) + } else { + processIncomingMessage(message) } - }.start() + } }.fail { Log.d("Loki", "Failed to get messages for group chat with ID: ${group.channel} on server: ${group.server}.") } @@ -263,13 +262,11 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki if (displayNameUpdatees.isEmpty()) { return } val hexEncodedPublicKeys = displayNameUpdatees displayNameUpdatees = setOf() - api.getDisplayNames(hexEncodedPublicKeys, group.server).success { mapping -> - Thread { - for (pair in mapping.entries) { - val senderDisplayName = "${pair.value} (...${pair.key.takeLast(8)})" - DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, pair.key, senderDisplayName) - } - }.start() + api.getDisplayNames(hexEncodedPublicKeys, group.server).successBackground { mapping -> + for (pair in mapping.entries) { + val senderDisplayName = "${pair.value} (...${pair.key.takeLast(8)})" + DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, pair.key, senderDisplayName) + } }.fail { displayNameUpdatees = displayNameUpdatees.union(hexEncodedPublicKeys) } @@ -277,16 +274,14 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki private fun pollForDeletedMessages() { api.getDeletedMessageServerIDs(group.channel, group.server).success { deletedMessageServerIDs -> - Thread { - val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) - val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) } - val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context) - val mmsMessageDatabase = DatabaseFactory.getMmsDatabase(context) - deletedMessageIDs.forEach { - smsMessageDatabase.deleteMessage(it) - mmsMessageDatabase.delete(it) - } - }.start() + val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) + val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { lokiMessageDatabase.getMessageID(it) } + val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context) + val mmsMessageDatabase = DatabaseFactory.getMmsDatabase(context) + deletedMessageIDs.forEach { + smsMessageDatabase.deleteMessage(it) + mmsMessageDatabase.delete(it) + } }.fail { Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.channel} on server: ${group.server}.") }