From dd92484b4e8a27df1081106be38633643cdaa0ab Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 20 May 2021 15:10:42 +1000 Subject: [PATCH] Create ClosedGroupPollerV2 --- .../pollers/ClosedGroupPollerV2.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPollerV2.kt diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPollerV2.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPollerV2.kt new file mode 100644 index 0000000000..8dfcf78ffc --- /dev/null +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/ClosedGroupPollerV2.kt @@ -0,0 +1,66 @@ +package org.session.libsession.messaging.sending_receiving.pollers + +import nl.komponents.kovenant.Promise +import org.session.libsession.messaging.MessagingModuleConfiguration + +class ClosedGroupPollerV2 { + private var isPolling = mutableMapOf() + + private fun isPolling(groupPublicKey: String): Boolean { + return isPolling[groupPublicKey] ?: false + } + + companion object { + private val minPollInterval = 4 * 1000 + private val maxPollInterval = 2 * 60 * 1000 + + val shared = ClosedGroupPollerV2() + } + + class InsufficientSnodesException() : Exception("No snodes left to poll.") + class PollingCanceledException() : Exception("Polling canceled.") + + fun start() { + val storage = MessagingModuleConfiguration.shared.storage + val allGroupPublicKeys = storage.getAllClosedGroupPublicKeys() + allGroupPublicKeys.forEach { startPolling(it) } + } + + fun startPolling(groupPublicKey: String) { + if (isPolling(groupPublicKey)) { return } + setUpPolling(groupPublicKey) + isPolling[groupPublicKey] = true + } + + fun stop() { + val storage = MessagingModuleConfiguration.shared.storage + val allGroupPublicKeys = storage.getAllClosedGroupPublicKeys() + allGroupPublicKeys.forEach { stopPolling(it) } + } + + fun stopPolling(groupPublicKey: String) { + // TODO: Invalidate future + isPolling[groupPublicKey] = false + } + + private fun setUpPolling(groupPublicKey: String) { + poll(groupPublicKey).success { + pollRecursively(groupPublicKey) + }.fail { + // The error is logged in poll(_:) + pollRecursively(groupPublicKey) + } + } + + private fun pollRecursively(groupPublicKey: String) { + if (!isPolling(groupPublicKey)) { return } + // Get the received date of the last message in the thread. If we don't have any messages yet, pick some + // reasonable fake time interval to use instead. + + } + + private fun poll(groupPublicKey: String): Promise { + return Promise.ofFail(InsufficientSnodesException()) + } + +}