diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ConfigDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/ConfigDatabase.kt index fd06f46cf4..0b7ae9a275 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ConfigDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ConfigDatabase.kt @@ -18,7 +18,7 @@ class ConfigDatabase(context: Context, helper: SQLCipherOpenHelper): Database(co const val CREATE_CONFIG_TABLE_COMMAND = "CREATE TABLE $TABLE_NAME ($VARIANT TEXT NOT NULL, $PUBKEY TEXT NOT NULL, $DATA BLOB, $COMBINED_MESSAGE_HASHES TEXT, PRIMARY KEY($VARIANT, $PUBKEY));" - private const val VARIANT_WHERE = "$VARIANT = ?" + private const val VARIANT_AND_PUBKEY_WHERE = "$VARIANT = ? AND $PUBKEY = ?" } diff --git a/libsession-util/libsession-util b/libsession-util/libsession-util index 4930086a34..c76d7e06f6 160000 --- a/libsession-util/libsession-util +++ b/libsession-util/libsession-util @@ -1 +1 @@ -Subproject commit 4930086a348d175cff4a41e804c84f23d048951e +Subproject commit c76d7e06f6c1c258fe930bcd8d567b3bd4067262 diff --git a/libsession-util/src/main/cpp/config_base.cpp b/libsession-util/src/main/cpp/config_base.cpp index 2d994dbcbb..7416767537 100644 --- a/libsession-util/src/main/cpp/config_base.cpp +++ b/libsession-util/src/main/cpp/config_base.cpp @@ -23,10 +23,12 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_needsDump(JNIEnv *env, j JNIEXPORT jobject JNICALL Java_network_loki_messenger_libsession_1util_ConfigBase_push(JNIEnv *env, jobject thiz) { auto config = ptrToConfigBase(env, thiz); - auto pair = config->push(); - session::ustring to_push_str = pair.first; + auto push_tuple = config->push(); + auto to_push_str = std::get<1>(push_tuple); + auto to_delete = std::get<2>(push_tuple); + jbyteArray returnByteArray = util::bytes_from_ustring(env, to_push_str); - jlong seqNo = pair.second; + jlong seqNo = std::get<0>(push_tuple); jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/ConfigWithSeqNo"); jmethodID methodId = env->GetMethodID(returnObjectClass, "", "([BJ)V"); jobject returnObject = env->NewObject(returnObjectClass, methodId, returnByteArray, seqNo); @@ -56,34 +58,38 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_encryptionDomain(JNIEnv JNIEXPORT void JNICALL Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *env, jobject thiz, - jlong seq_no) { + jlong seq_no, + jstring new_hash_jstring) { auto conf = ptrToConfigBase(env, thiz); - conf->confirm_pushed(seq_no); + auto new_hash = env->GetStringUTFChars(new_hash_jstring, nullptr); + conf->confirm_pushed(seq_no, new_hash); + env->ReleaseStringUTFChars(new_hash_jstring, new_hash); } #pragma clang diagnostic push #pragma ide diagnostic ignored "bugprone-reserved-identifier" JNIEXPORT jint JNICALL -Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3_3B(JNIEnv *env, jobject thiz, +Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3Lkotlin_Pair_2(JNIEnv *env, jobject thiz, jobjectArray to_merge) { auto conf = ptrToConfigBase(env, thiz); size_t number = env->GetArrayLength(to_merge); - std::vector configs = {}; + std::vector> configs = {}; for (int i = 0; i < number; i++) { - auto jArr = (jbyteArray) env->GetObjectArrayElement(to_merge, i); - auto bytes = util::ustring_from_bytes(env, jArr); - configs.push_back(bytes); + auto jElement = (jobject) env->GetObjectArrayElement(to_merge, i); + auto pair = extractHashAndData(env, jElement); + configs.push_back(pair); } return conf->merge(configs); } JNIEXPORT jint JNICALL -Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3B(JNIEnv *env, jobject thiz, - jbyteArray to_merge) { +Java_network_loki_messenger_libsession_1util_ConfigBase_merge__Lkotlin_Pair_2(JNIEnv *env, jobject thiz, + jobject to_merge) { auto conf = ptrToConfigBase(env, thiz); - std::vector configs = {util::ustring_from_bytes(env, to_merge)}; + std::vector> configs = {extractHashAndData(env, to_merge)}; return conf->merge(configs); } + #pragma clang diagnostic pop } extern "C" @@ -100,6 +106,7 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_00024Companion_kindFor(J 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"); + auto group_list_class = env->FindClass("network/loki/messenger/libsession_util/UserGroupsConfig"); switch (config_namespace) { case (int)session::config::Namespace::UserProfile: return user_class; @@ -107,7 +114,23 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_00024Companion_kindFor(J return contact_class; case (int)session::config::Namespace::ConvoInfoVolatile: return convo_volatile_class; + case (int)session::config::Namespace::UserGroups: + return group_list_class; default: return nullptr; } +} +extern "C" +JNIEXPORT void JNICALL +Java_network_loki_messenger_libsession_1util_ConfigBase_removeObsoleteHashes(JNIEnv *env, + jobject thiz, + jobjectArray to_remove) { + auto conf = ptrToConfigBase(env, thiz); + size_t number = env->GetArrayLength(to_remove); + for (int i = 0; i < number; i++) { + auto jElement = (jstring) env->GetObjectArrayElement(to_remove, i); + auto element_as_string = env->GetStringUTFChars(jElement, nullptr); + conf->confirm_removed(element_as_string); + env->ReleaseStringUTFChars(jElement, element_as_string); + } } \ No newline at end of file diff --git a/libsession-util/src/main/cpp/config_base.h b/libsession-util/src/main/cpp/config_base.h index 9ce520b198..836fb04ef5 100644 --- a/libsession-util/src/main/cpp/config_base.h +++ b/libsession-util/src/main/cpp/config_base.h @@ -2,6 +2,7 @@ #define SESSION_ANDROID_CONFIG_BASE_H #include "session/config/base.hpp" +#include "util.h" #include #include @@ -11,4 +12,17 @@ inline session::config::ConfigBase* ptrToConfigBase(JNIEnv *env, jobject obj) { return (session::config::ConfigBase*) env->GetLongField(obj, pointerField); } +inline std::pair extractHashAndData(JNIEnv *env, jobject kotlin_pair) { + jclass pair = env->FindClass("kotlin/Pair"); + jfieldID first = env->GetFieldID(pair, "first", "Ljava/lang/Object;"); + jfieldID second = env->GetFieldID(pair, "second", "Ljava/lang/Object;"); + jstring hash_as_jstring = static_cast(env->GetObjectField(kotlin_pair, first)); + jbyteArray data_as_jbytes = static_cast(env->GetObjectField(kotlin_pair, second)); + auto hash_as_string = env->GetStringUTFChars(hash_as_jstring, nullptr); + auto data_as_ustring = util::ustring_from_bytes(env, data_as_jbytes); + auto ret_pair = std::pair{hash_as_string, data_as_ustring}; + env->ReleaseStringUTFChars(hash_as_jstring, hash_as_string); + return ret_pair; +} + #endif \ 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 c30ef88526..b91aacd9dd 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 @@ -31,13 +31,14 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) { external fun push(): ConfigWithSeqNo external fun dump(): ByteArray external fun encryptionDomain(): String - external fun confirmPushed(seqNo: Long) - external fun merge(toMerge: Array): Int + external fun confirmPushed(seqNo: Long, newHash: String) + external fun merge(toMerge: Array>): Int + external fun removeObsoleteHashes(toRemove: Array) external fun configNamespace(): Int // Singular merge - external fun merge(toMerge: ByteArray): Int + external fun merge(toMerge: Pair): Int external fun free() diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/ConfigurationSyncJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/ConfigurationSyncJob.kt index 03dcbd8c05..6571da0a2d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/ConfigurationSyncJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/ConfigurationSyncJob.kt @@ -136,13 +136,12 @@ data class ConfigurationSyncJob(val destination: Destination): Job { // confirm pushed seqno val thisSeqNo = toPushMessage.seqNo - config.confirmPushed(thisSeqNo) + config.confirmPushed(thisSeqNo, insertHash) + config. // wipe any of the existing hashes which we deleted (they may or may not be in this namespace) if (configFactory.removeHashesFor(config, deletedHashes.toSet())) { Log.d(TAG, "Successfully removed the deleted hashes from ${config.javaClass.simpleName}") } - // store the new hash in list of hashes to track against - configFactory.appendHash(config, insertHash) // dump and write config after successful if (config.needsDump()) { // usually this will be true? configFactory.persist(config)