|
|
|
@ -1,23 +1,22 @@
|
|
|
|
|
package org.session.libsession.messaging.sending_receiving.pollers
|
|
|
|
|
|
|
|
|
|
import nl.komponents.kovenant.Promise
|
|
|
|
|
import nl.komponents.kovenant.deferred
|
|
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
|
|
|
|
import org.session.libsession.messaging.jobs.JobQueue
|
|
|
|
|
import org.session.libsession.messaging.jobs.MessageReceiveJob
|
|
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupAPIV2
|
|
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupMessageV2
|
|
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupV2
|
|
|
|
|
import org.session.libsession.messaging.threads.Address
|
|
|
|
|
import org.session.libsession.utilities.GroupUtil
|
|
|
|
|
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
|
|
|
|
import org.session.libsignal.service.loki.database.LokiMessageDatabaseProtocol
|
|
|
|
|
import org.session.libsignal.utilities.logging.Log
|
|
|
|
|
import org.session.libsignal.utilities.successBackground
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService
|
|
|
|
|
import java.util.concurrent.ScheduledFuture
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
|
|
class OpenGroupV2Poller(private val openGroup: OpenGroupV2, private val executorService: ScheduledExecutorService? = null) {
|
|
|
|
|
class OpenGroupV2Poller(private val openGroups: List<OpenGroupV2>, private val executorService: ScheduledExecutorService? = null) {
|
|
|
|
|
|
|
|
|
|
private var hasStarted = false
|
|
|
|
|
@Volatile private var isPollOngoing = false
|
|
|
|
@ -25,22 +24,29 @@ class OpenGroupV2Poller(private val openGroup: OpenGroupV2, private val executor
|
|
|
|
|
|
|
|
|
|
private val cancellableFutures = mutableListOf<ScheduledFuture<out Any>>()
|
|
|
|
|
|
|
|
|
|
// use this as a receive time-based window to calculate re-poll interval
|
|
|
|
|
private val receivedQueue = ArrayDeque<Long>(50)
|
|
|
|
|
|
|
|
|
|
private fun calculatePollInterval(): Long {
|
|
|
|
|
// sample last default poll time * 2
|
|
|
|
|
while (receivedQueue.size > 50) {
|
|
|
|
|
receivedQueue.removeLast()
|
|
|
|
|
}
|
|
|
|
|
val sampleWindow = System.currentTimeMillis() - pollForNewMessagesInterval * 2
|
|
|
|
|
val numberInSample = receivedQueue.toList().filter { it > sampleWindow }.size.coerceAtLeast(1)
|
|
|
|
|
return ((2 + (50 / numberInSample / 20)*5) * 1000).toLong()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// region Settings
|
|
|
|
|
companion object {
|
|
|
|
|
private val pollForNewMessagesInterval: Long = 10 * 1000
|
|
|
|
|
private val pollForDeletedMessagesInterval: Long = 60 * 1000
|
|
|
|
|
private val pollForModeratorsInterval: Long = 10 * 60 * 1000
|
|
|
|
|
}
|
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
|
fun startIfNeeded() {
|
|
|
|
|
if (hasStarted || executorService == null) return
|
|
|
|
|
cancellableFutures += listOf(
|
|
|
|
|
executorService.scheduleAtFixedRate(::pollForNewMessages,0, pollForNewMessagesInterval, TimeUnit.MILLISECONDS),
|
|
|
|
|
executorService.scheduleAtFixedRate(::pollForDeletedMessages,0, pollForDeletedMessagesInterval, TimeUnit.MILLISECONDS),
|
|
|
|
|
executorService.scheduleAtFixedRate(::pollForModerators,0, pollForModeratorsInterval, TimeUnit.MILLISECONDS),
|
|
|
|
|
)
|
|
|
|
|
cancellableFutures += executorService.schedule(::compactPoll, 0, TimeUnit.MILLISECONDS)
|
|
|
|
|
hasStarted = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -54,75 +60,75 @@ class OpenGroupV2Poller(private val openGroup: OpenGroupV2, private val executor
|
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
// region Polling
|
|
|
|
|
fun pollForNewMessages(): Promise<Unit, Exception> {
|
|
|
|
|
return pollForNewMessages(false)
|
|
|
|
|
|
|
|
|
|
private fun compactPoll(): Promise<Any, Exception> {
|
|
|
|
|
return compactPoll(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun pollForNewMessages(isBackgroundPoll: Boolean): Promise<Unit, Exception> {
|
|
|
|
|
if (isPollOngoing) { return Promise.of(Unit) }
|
|
|
|
|
fun compactPoll(isBackgroundPoll: Boolean): Promise<Any, Exception> {
|
|
|
|
|
if (isPollOngoing) return Promise.of(Unit)
|
|
|
|
|
isPollOngoing = true
|
|
|
|
|
val deferred = deferred<Unit, Exception>()
|
|
|
|
|
// Kovenant propagates a context to chained promises, so OpenGroupAPI.sharedContext should be used for all of the below
|
|
|
|
|
OpenGroupAPIV2.getMessages(openGroup.room, openGroup.server).successBackground { messages ->
|
|
|
|
|
// Process messages in the background
|
|
|
|
|
messages.forEach { message ->
|
|
|
|
|
try {
|
|
|
|
|
val senderPublicKey = message.sender!!
|
|
|
|
|
// Main message
|
|
|
|
|
val dataMessageProto = message.toProto()
|
|
|
|
|
// Content
|
|
|
|
|
val content = SignalServiceProtos.Content.newBuilder()
|
|
|
|
|
content.dataMessage = dataMessageProto
|
|
|
|
|
// Envelope
|
|
|
|
|
val builder = SignalServiceProtos.Envelope.newBuilder()
|
|
|
|
|
builder.type = SignalServiceProtos.Envelope.Type.SESSION_MESSAGE
|
|
|
|
|
builder.source = senderPublicKey
|
|
|
|
|
builder.sourceDevice = 1
|
|
|
|
|
builder.content = content.build().toByteString()
|
|
|
|
|
builder.timestamp = message.sentTimestamp
|
|
|
|
|
val envelope = builder.build()
|
|
|
|
|
val job = MessageReceiveJob(envelope.toByteArray(), isBackgroundPoll, message.serverID, openGroup.id)
|
|
|
|
|
Log.d("Loki", "Scheduling Job $job")
|
|
|
|
|
if (isBackgroundPoll) {
|
|
|
|
|
job.executeAsync().always { deferred.resolve(Unit) }
|
|
|
|
|
// The promise is just used to keep track of when we're done
|
|
|
|
|
} else {
|
|
|
|
|
JobQueue.shared.add(job)
|
|
|
|
|
}
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
Log.e("Loki", "Exception parsing message", e)
|
|
|
|
|
}
|
|
|
|
|
val server = openGroups.first().server // assume all the same server
|
|
|
|
|
val rooms = openGroups.map { it.room }
|
|
|
|
|
return OpenGroupAPIV2.getCompactPoll(rooms = rooms, server).successBackground { results ->
|
|
|
|
|
results.forEach { (room, results) ->
|
|
|
|
|
val serverRoomId = "$server.$room"
|
|
|
|
|
handleDeletedMessages(serverRoomId,results.deletions)
|
|
|
|
|
handleNewMessages(serverRoomId, results.messages, isBackgroundPoll)
|
|
|
|
|
}
|
|
|
|
|
isCaughtUp = true
|
|
|
|
|
isPollOngoing = false
|
|
|
|
|
deferred.resolve(Unit)
|
|
|
|
|
}.fail {
|
|
|
|
|
Log.e("Loki", "Failed to get messages for group chat with room: ${openGroup.room} on server: ${openGroup.server}.", it)
|
|
|
|
|
}.always {
|
|
|
|
|
isPollOngoing = false
|
|
|
|
|
if (!isBackgroundPoll) {
|
|
|
|
|
val delay = calculatePollInterval()
|
|
|
|
|
Log.d("Loki", "polling in ${delay}ms")
|
|
|
|
|
executorService?.schedule(this@OpenGroupV2Poller::compactPoll, delay, TimeUnit.MILLISECONDS)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return deferred.promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun pollForDeletedMessages() {
|
|
|
|
|
val messagingModule = MessagingModuleConfiguration.shared
|
|
|
|
|
val address = GroupUtil.getEncodedOpenGroupID(openGroup.id.toByteArray())
|
|
|
|
|
val threadId = messagingModule.storage.getThreadIdFor(Address.fromSerialized(address)) ?: return
|
|
|
|
|
|
|
|
|
|
OpenGroupAPIV2.getDeletedMessages(openGroup.room, openGroup.server).success { deletedMessageServerIDs ->
|
|
|
|
|
|
|
|
|
|
val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { serverId ->
|
|
|
|
|
messagingModule.messageDataProvider.getMessageID(serverId.deletedMessageId, threadId)
|
|
|
|
|
}
|
|
|
|
|
deletedMessageIDs.forEach { (messageId, isSms) ->
|
|
|
|
|
MessagingModuleConfiguration.shared.messageDataProvider.deleteMessage(messageId, isSms)
|
|
|
|
|
private fun handleNewMessages(serverRoomId: String, newMessages: List<OpenGroupMessageV2>, isBackgroundPoll: Boolean) {
|
|
|
|
|
newMessages.forEach { message ->
|
|
|
|
|
try {
|
|
|
|
|
val senderPublicKey = message.sender!!
|
|
|
|
|
// Main message
|
|
|
|
|
val dataMessageProto = message.toProto()
|
|
|
|
|
// Content
|
|
|
|
|
val content = SignalServiceProtos.Content.newBuilder()
|
|
|
|
|
content.dataMessage = dataMessageProto
|
|
|
|
|
// Envelope
|
|
|
|
|
val builder = SignalServiceProtos.Envelope.newBuilder()
|
|
|
|
|
builder.type = SignalServiceProtos.Envelope.Type.SESSION_MESSAGE
|
|
|
|
|
builder.source = senderPublicKey
|
|
|
|
|
builder.sourceDevice = 1
|
|
|
|
|
builder.content = content.build().toByteString()
|
|
|
|
|
builder.timestamp = message.sentTimestamp
|
|
|
|
|
val envelope = builder.build()
|
|
|
|
|
val job = MessageReceiveJob(envelope.toByteArray(), isBackgroundPoll, message.serverID, serverRoomId)
|
|
|
|
|
Log.d("Loki", "Scheduling Job $job")
|
|
|
|
|
if (isBackgroundPoll) {
|
|
|
|
|
job.executeAsync()
|
|
|
|
|
// The promise is just used to keep track of when we're done
|
|
|
|
|
} else {
|
|
|
|
|
JobQueue.shared.add(job)
|
|
|
|
|
}
|
|
|
|
|
receivedQueue.addFirst(message.sentTimestamp)
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
Log.e("Loki", "Exception parsing message", e)
|
|
|
|
|
}
|
|
|
|
|
}.fail {
|
|
|
|
|
Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${openGroup.room} on server: ${openGroup.server}.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun pollForModerators() {
|
|
|
|
|
OpenGroupAPIV2.getModerators(openGroup.room, openGroup.server)
|
|
|
|
|
private fun handleDeletedMessages(serverRoomId: String, deletedMessageServerIDs: List<Long>) {
|
|
|
|
|
val messagingModule = MessagingModuleConfiguration.shared
|
|
|
|
|
val address = GroupUtil.getEncodedOpenGroupID(serverRoomId.toByteArray())
|
|
|
|
|
val threadId = messagingModule.storage.getThreadIdFor(Address.fromSerialized(address)) ?: return
|
|
|
|
|
|
|
|
|
|
val deletedMessageIDs = deletedMessageServerIDs.mapNotNull { serverId ->
|
|
|
|
|
messagingModule.messageDataProvider.getMessageID(serverId, threadId)
|
|
|
|
|
}
|
|
|
|
|
deletedMessageIDs.forEach { (messageId, isSms) ->
|
|
|
|
|
MessagingModuleConfiguration.shared.messageDataProvider.deleteMessage(messageId, isSms)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// endregion
|
|
|
|
|
}
|