feat: update request responses and their appropriate processing

This commit is contained in:
0x330a 2023-02-06 17:24:55 +11:00
parent 28e5794624
commit c8d520c3ce
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
5 changed files with 58 additions and 30 deletions

View File

@ -91,4 +91,23 @@ JNIEXPORT jint JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_configNamespace(JNIEnv *env, jobject thiz) { Java_network_loki_messenger_libsession_1util_ConfigBase_configNamespace(JNIEnv *env, jobject thiz) {
auto conf = ptrToConfigBase(env, thiz); auto conf = ptrToConfigBase(env, thiz);
return (std::int16_t) conf->storage_namespace(); 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;
}
} }

View File

@ -11,6 +11,7 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
init { init {
System.loadLibrary("session_util") System.loadLibrary("session_util")
} }
external fun kindFor(configNamespace: Int): Class<ConfigBase>
} }
external fun dirty(): Boolean external fun dirty(): Boolean

View File

@ -1,8 +1,13 @@
package org.session.libsession.messaging.sending_receiving.pollers 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.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking 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.Deferred
import nl.komponents.kovenant.Promise import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.deferred import nl.komponents.kovenant.deferred
@ -19,6 +24,7 @@ import org.session.libsession.snode.SnodeModule
import org.session.libsession.utilities.ConfigFactoryProtocol import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsignal.utilities.JsonUtil import org.session.libsignal.utilities.JsonUtil
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.Namespace
import org.session.libsignal.utilities.Snode import org.session.libsignal.utilities.Snode
import java.security.SecureRandom import java.security.SecureRandom
import java.util.Timer import java.util.Timer
@ -132,33 +138,22 @@ class Poller(private val configFactory: ConfigFactoryProtocol) {
if (!hasStarted) { return Promise.ofFail(PromiseCanceledException()) } if (!hasStarted) { return Promise.ofFail(PromiseCanceledException()) }
return task { return task {
runBlocking(Dispatchers.IO) { runBlocking(Dispatchers.IO) {
val requests = listOfNotNull( val requestSparseArray = SparseArray<SnodeAPI.SnodeBatchRequestInfo>()
// get messages // get messages
SnodeAPI.buildAuthenticatedRetrieveBatchRequest(snode, userPublicKey), SnodeAPI.buildAuthenticatedRetrieveBatchRequest(snode, userPublicKey)!!.also { personalMessages ->
// get user config namespace requestSparseArray[personalMessages.namespace] = personalMessages
configFactory.user?.let { currentUserConfig -> }
SnodeAPI.buildAuthenticatedRetrieveBatchRequest( // get the latest convo info volatile
snode, listOfNotNull(configFactory.user, configFactory.contacts, configFactory.convoVolatile).mapNotNull { config ->
userPublicKey, SnodeAPI.buildAuthenticatedRetrieveBatchRequest(
currentUserConfig.configNamespace() snode, userPublicKey,
) config.configNamespace()
}, )
// get contact config namespace }.forEach { request ->
configFactory.contacts?.let { currentContacts -> requestSparseArray[request.namespace] = request
SnodeAPI.buildAuthenticatedRetrieveBatchRequest( }
snode,
userPublicKey, val requests = requestSparseArray.valueIterator().asSequence().toList()
currentContacts.configNamespace()
)
},
// get the latest convo info volatile
configFactory.convoVolatile?.let { currentConvoVolatile ->
SnodeAPI.buildAuthenticatedRetrieveBatchRequest(
snode, userPublicKey,
currentConvoVolatile.configNamespace()
)
}
)
SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses -> SnodeAPI.getRawBatchResponse(snode, userPublicKey, requests).bind { rawResponses ->
isCaughtUp = true isCaughtUp = true
@ -166,7 +161,18 @@ class Poller(private val configFactory: ConfigFactoryProtocol) {
return@bind Promise.ofSuccess(Unit) return@bind Promise.ofSuccess(Unit)
} else { } else {
Log.d("Loki-DBG", JsonUtil.toJson(rawResponses)) Log.d("Loki-DBG", JsonUtil.toJson(rawResponses))
val requestList = (rawResponses["results"] as List<RawResponse>)
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) poll(snode, deferred)
} }
} }

View File

@ -100,6 +100,8 @@ object SnodeAPI {
data class SnodeBatchRequestInfo( data class SnodeBatchRequestInfo(
val method: String, val method: String,
val params: Map<String, Any>, val params: Map<String, Any>,
@Transient
val namespace: Int
) // assume signatures, pubkey and namespaces are attached in parameters if required ) // assume signatures, pubkey and namespaces are attached in parameters if required
// Internal API // Internal API
@ -398,7 +400,8 @@ object SnodeAPI {
} }
return SnodeBatchRequestInfo( return SnodeBatchRequestInfo(
Snode.Method.Retrieve.rawValue, Snode.Method.Retrieve.rawValue,
params params,
namespace
) )
} }

View File

@ -3,5 +3,4 @@ package org.session.libsignal.utilities
object Namespace { object Namespace {
const val DEFAULT = 0 const val DEFAULT = 0
const val UNAUTHENTICATED_CLOSED_GROUP = -10 const val UNAUTHENTICATED_CLOSED_GROUP = -10
const val CONFIGURATION = 5
} }