From 1c1ce1424b07763dfebc6157c73a5d0b63fee348 Mon Sep 17 00:00:00 2001 From: jubb Date: Tue, 23 Mar 2021 11:39:14 +1100 Subject: [PATCH] fix: replace elvis on optional proto object with protobuf has flag check --- .../control/DataExtractionNotification.kt | 6 +- .../libsignal/service/loki/api/Poller.kt | 95 ------------------- 2 files changed, 2 insertions(+), 99 deletions(-) delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/Poller.kt diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/DataExtractionNotification.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/DataExtractionNotification.kt index 1888f66a49..0526fbec0f 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/DataExtractionNotification.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/DataExtractionNotification.kt @@ -1,10 +1,7 @@ package org.session.libsession.messaging.messages.control -import com.google.protobuf.ByteString -import org.session.libsignal.libsignal.ecc.ECKeyPair import org.session.libsignal.service.internal.push.SignalServiceProtos import org.session.libsignal.utilities.logging.Log -import java.lang.Exception class DataExtractionNotification(): ControlMessage() { var kind: Kind? = null @@ -25,7 +22,8 @@ class DataExtractionNotification(): ControlMessage() { const val TAG = "DataExtractionNotification" fun fromProto(proto: SignalServiceProtos.Content): DataExtractionNotification? { - val dataExtractionNotification = proto.dataExtractionNotification ?: return null + if (!proto.hasDataExtractionNotification()) return null + val dataExtractionNotification = proto.dataExtractionNotification!! val kind: Kind = when(dataExtractionNotification.type) { SignalServiceProtos.DataExtractionNotification.Type.SCREENSHOT -> Kind.Screenshot() SignalServiceProtos.DataExtractionNotification.Type.MEDIA_SAVED -> { diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/Poller.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/Poller.kt deleted file mode 100644 index 38faea7340..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/Poller.kt +++ /dev/null @@ -1,95 +0,0 @@ -package org.session.libsignal.service.loki.api - -import nl.komponents.kovenant.* -import nl.komponents.kovenant.functional.bind -import org.session.libsignal.utilities.logging.Log -import org.session.libsignal.service.internal.push.SignalServiceProtos -import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol -import java.security.SecureRandom -import java.util.* - -private class PromiseCanceledException : Exception("Promise canceled.") - -class Poller(public var userPublicKey: String, private val database: LokiAPIDatabaseProtocol, private val onMessagesReceived: (List) -> Unit) { - private var hasStarted: Boolean = false - private val usedSnodes: MutableSet = mutableSetOf() - public var isCaughtUp = false - - // region Settings - companion object { - private val retryInterval: Long = 1 * 1000 - } - // endregion - - // region Public API - fun startIfNeeded() { - if (hasStarted) { return } - Log.d("Loki", "Started polling.") - hasStarted = true - setUpPolling() - } - - fun stopIfNeeded() { - Log.d("Loki", "Stopped polling.") - hasStarted = false - usedSnodes.clear() - } - // endregion - - // region Private API - private fun setUpPolling() { - if (!hasStarted) { return; } - val thread = Thread.currentThread() - SwarmAPI.shared.getSwarm(userPublicKey).bind(SnodeAPI.messagePollingContext) { - usedSnodes.clear() - val deferred = deferred(SnodeAPI.messagePollingContext) - pollNextSnode(deferred) - deferred.promise - }.always { - Timer().schedule(object : TimerTask() { - - override fun run() { - thread.run { setUpPolling() } - } - }, retryInterval) - } - } - - private fun pollNextSnode(deferred: Deferred) { - val swarm = database.getSwarm(userPublicKey) ?: setOf() - val unusedSnodes = swarm.subtract(usedSnodes) - if (unusedSnodes.isNotEmpty()) { - val index = SecureRandom().nextInt(unusedSnodes.size) - val nextSnode = unusedSnodes.elementAt(index) - usedSnodes.add(nextSnode) - Log.d("Loki", "Polling $nextSnode.") - poll(nextSnode, deferred).fail { exception -> - if (exception is PromiseCanceledException) { - Log.d("Loki", "Polling $nextSnode canceled.") - } else { - Log.d("Loki", "Polling $nextSnode failed; dropping it and switching to next snode.") - SwarmAPI.shared.dropSnodeFromSwarmIfNeeded(nextSnode, userPublicKey) - pollNextSnode(deferred) - } - } - } else { - isCaughtUp = true - deferred.resolve() - } - } - - private fun poll(snode: Snode, deferred: Deferred): Promise { - if (!hasStarted) { return Promise.ofFail(PromiseCanceledException()) } - return SnodeAPI.shared.getRawMessages(snode, userPublicKey).bind(SnodeAPI.messagePollingContext) { rawResponse -> - isCaughtUp = true - if (deferred.promise.isDone()) { - task { Unit } // The long polling connection has been canceled; don't recurse - } else { - val messages = SnodeAPI.shared.parseRawMessagesResponse(rawResponse, snode, userPublicKey) - onMessagesReceived(messages) - poll(snode, deferred) - } - } - } - // endregion -}