diff --git a/libsession-util/src/main/cpp/config_base.cpp b/libsession-util/src/main/cpp/config_base.cpp index db7693ab06..2d994dbcbb 100644 --- a/libsession-util/src/main/cpp/config_base.cpp +++ b/libsession-util/src/main/cpp/config_base.cpp @@ -91,4 +91,23 @@ JNIEXPORT jint JNICALL Java_network_loki_messenger_libsession_1util_ConfigBase_configNamespace(JNIEnv *env, jobject thiz) { auto conf = ptrToConfigBase(env, thiz); return (std::int16_t) conf->storage_namespace(); +} +extern "C" +JNIEXPORT jclass JNICALL +Java_network_loki_messenger_libsession_1util_ConfigBase_00024Companion_kindFor(JNIEnv *env, + jobject thiz, + jint config_namespace) { + auto user_class = env->FindClass("network/loki/messenger/libsession_util/UserProfile"); + auto contact_class = env->FindClass("network/loki/messenger/libsession_util/Contacts"); + auto convo_volatile_class = env->FindClass("network/loki/messenger/libsession_util/ConversationVolatileConfig"); + switch (config_namespace) { + case (int)session::config::Namespace::UserProfile: + return user_class; + case (int)session::config::Namespace::Contacts: + return contact_class; + case (int)session::config::Namespace::ConvoInfoVolatile: + return convo_volatile_class; + default: + return nullptr; + } } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt index 62c482b3b9..50454f6b37 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt @@ -11,6 +11,7 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) { init { System.loadLibrary("session_util") } + external fun kindFor(configNamespace: Int): Class } external fun dirty(): Boolean diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt index 6f6c3b034f..8ea2ba0fca 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt @@ -1,8 +1,13 @@ package org.session.libsession.messaging.sending_receiving.pollers +import android.util.SparseArray +import androidx.core.util.keyIterator +import androidx.core.util.valueIterator import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.runBlocking +import network.loki.messenger.libsession_util.ConfigBase +import network.loki.messenger.libsession_util.UserProfile import nl.komponents.kovenant.Deferred import nl.komponents.kovenant.Promise import nl.komponents.kovenant.deferred @@ -19,6 +24,7 @@ import org.session.libsession.snode.SnodeModule import org.session.libsession.utilities.ConfigFactoryProtocol import org.session.libsignal.utilities.JsonUtil import org.session.libsignal.utilities.Log +import org.session.libsignal.utilities.Namespace import org.session.libsignal.utilities.Snode import java.security.SecureRandom import java.util.Timer @@ -132,33 +138,22 @@ class Poller(private val configFactory: ConfigFactoryProtocol) { if (!hasStarted) { return Promise.ofFail(PromiseCanceledException()) } return task { runBlocking(Dispatchers.IO) { - val requests = listOfNotNull( - // get messages - SnodeAPI.buildAuthenticatedRetrieveBatchRequest(snode, userPublicKey), - // get user config namespace - configFactory.user?.let { currentUserConfig -> - SnodeAPI.buildAuthenticatedRetrieveBatchRequest( - snode, - userPublicKey, - currentUserConfig.configNamespace() - ) - }, - // get contact config namespace - configFactory.contacts?.let { currentContacts -> - SnodeAPI.buildAuthenticatedRetrieveBatchRequest( - snode, - userPublicKey, - currentContacts.configNamespace() - ) - }, - // get the latest convo info volatile - configFactory.convoVolatile?.let { currentConvoVolatile -> - SnodeAPI.buildAuthenticatedRetrieveBatchRequest( - snode, userPublicKey, - currentConvoVolatile.configNamespace() - ) - } - ) + val requestSparseArray = SparseArray() + // get messages + SnodeAPI.buildAuthenticatedRetrieveBatchRequest(snode, userPublicKey)!!.also { personalMessages -> + requestSparseArray[personalMessages.namespace] = personalMessages + } + // get the latest convo info volatile + listOfNotNull(configFactory.user, configFactory.contacts, configFactory.convoVolatile).mapNotNull { config -> + SnodeAPI.buildAuthenticatedRetrieveBatchRequest( + snode, userPublicKey, + config.configNamespace() + ) + }.forEach { request -> + requestSparseArray[request.namespace] = request + } + + val requests = requestSparseArray.valueIterator().asSequence().toList() SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses -> isCaughtUp = true @@ -166,7 +161,18 @@ class Poller(private val configFactory: ConfigFactoryProtocol) { return@bind Promise.ofSuccess(Unit) } else { Log.d("Loki-DBG", JsonUtil.toJson(rawResponses)) - + val requestList = (rawResponses["results"] as List) + requestSparseArray.keyIterator().withIndex().forEach { (requestIndex, key) -> + requestList.getOrNull(requestIndex)?.let { rawResponse -> + if (key == Namespace.DEFAULT) { + processPersonalMessages(snode, rawResponse) + } else { + when (ConfigBase.kindFor(key)) { + UserProfile -> + } + } + } + } poll(snode, deferred) } } diff --git a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt index 63477f43e4..b133c708d1 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt @@ -100,6 +100,8 @@ object SnodeAPI { data class SnodeBatchRequestInfo( val method: String, val params: Map, + @Transient + val namespace: Int ) // assume signatures, pubkey and namespaces are attached in parameters if required // Internal API @@ -398,7 +400,8 @@ object SnodeAPI { } return SnodeBatchRequestInfo( Snode.Method.Retrieve.rawValue, - params + params, + namespace ) } diff --git a/libsignal/src/main/java/org/session/libsignal/utilities/Namespace.kt b/libsignal/src/main/java/org/session/libsignal/utilities/Namespace.kt index 1c635d9934..fe673fca5c 100644 --- a/libsignal/src/main/java/org/session/libsignal/utilities/Namespace.kt +++ b/libsignal/src/main/java/org/session/libsignal/utilities/Namespace.kt @@ -3,5 +3,4 @@ package org.session.libsignal.utilities object Namespace { const val DEFAULT = 0 const val UNAUTHENTICATED_CLOSED_GROUP = -10 - const val CONFIGURATION = 5 } \ No newline at end of file