diff --git a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt index a76f4672ce..5783a7972f 100644 --- a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt +++ b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt @@ -464,7 +464,7 @@ class InstrumentedTests { convos.set(og) - val (toPush, seqNo) = convos.push() + val (_, seqNo) = convos.push() assertEquals(1, seqNo) @@ -473,7 +473,7 @@ class InstrumentedTests { assertTrue(convos.needsDump()) assertFalse(convos.needsPush()) - val convos2 = ConversationVolatileConfig.newInstance(keyPair.secretKey, toPush) + val convos2 = ConversationVolatileConfig.newInstance(keyPair.secretKey, convos.dump()) assertFalse(convos.needsPush()) assertFalse(convos.needsDump()) assertEquals(1, convos.push().seqNo) diff --git a/libsession-util/src/main/cpp/conversation.cpp b/libsession-util/src/main/cpp/conversation.cpp index ac00ee9dd8..79d4b5a1bc 100644 --- a/libsession-util/src/main/cpp/conversation.cpp +++ b/libsession-util/src/main/cpp/conversation.cpp @@ -119,14 +119,189 @@ Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_set(JNIE jclass legacy_closed_group = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$LegacyClosedGroup"); jclass to_store_class = env->GetObjectClass(to_store); - if (to_store_class == one_to_one) { + if (env->IsSameObject(to_store_class, one_to_one)) { // store as 1to1 convos->set(*deserialize_one_to_one(env, to_store)); - } else if (to_store_class == open_group) { + } else if (env->IsSameObject(to_store_class,open_group)) { // store as open_group convos->set(*deserialize_open_group(env, to_store)); - } else if (to_store_class == legacy_closed_group) { + } else if (env->IsSameObject(to_store_class,legacy_closed_group)) { // store as legacy_closed_group convos->set(*deserialize_legacy_closed_group(env, to_store)); } +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOneToOne(JNIEnv *env, + jobject thiz, + jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto param = env->GetStringUTFChars(pub_key_hex, nullptr); + auto internal = convos->get_1to1(param); + env->ReleaseStringUTFChars(pub_key_hex, param); + if (internal) { + return serialize_one_to_one(env, *internal); + } + return nullptr; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOrConstructOneToOne( + JNIEnv *env, jobject thiz, jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto param = env->GetStringUTFChars(pub_key_hex, nullptr); + auto internal = convos->get_or_construct_1to1(param); + env->ReleaseStringUTFChars(pub_key_hex, param); + return serialize_one_to_one(env, internal); +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_eraseOneToOne(JNIEnv *env, + jobject thiz, + jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto param = env->GetStringUTFChars(pub_key_hex, nullptr); + auto result = convos->erase_1to1(param); + env->ReleaseStringUTFChars(pub_key_hex, param); + return result; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOpenGroup__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto hex_chars = env->GetStringUTFChars(pub_key_hex, nullptr); + auto open = convos->get_open(base_url_chars, room_chars, hex_chars); + env->ReleaseStringUTFChars(base_url, base_url_chars); + env->ReleaseStringUTFChars(room, room_chars); + env->ReleaseStringUTFChars(pub_key_hex, hex_chars); + if (open) { + auto serialized = serialize_open_group(env, *open); + return serialized; + } + return nullptr; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOpenGroup__Ljava_lang_String_2Ljava_lang_String_2_3B( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jbyteArray pub_key) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto pub_key_ustring = util::ustring_from_bytes(env, pub_key); + auto open = convos->get_open(base_url_chars, room_chars, pub_key_ustring); + if (open) { + auto serialized = serialize_open_group(env, *open); + return serialized; + } + return nullptr; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOrConstructOpenGroup__Ljava_lang_String_2Ljava_lang_String_2_3B( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jbyteArray pub_key) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto pub_key_ustring = util::ustring_from_bytes(env, pub_key); + auto open = convos->get_or_construct_open(base_url_chars, room_chars, pub_key_ustring); + auto serialized = serialize_open_group(env, open); + return serialized; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOrConstructOpenGroup__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto hex_chars = env->GetStringUTFChars(pub_key_hex, nullptr); + auto open = convos->get_or_construct_open(base_url_chars, room_chars, hex_chars); + env->ReleaseStringUTFChars(base_url, base_url_chars); + env->ReleaseStringUTFChars(room, room_chars); + env->ReleaseStringUTFChars(pub_key_hex, hex_chars); + auto serialized = serialize_open_group(env, open); + return serialized; +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_eraseOpenGroup__Lnetwork_loki_messenger_libsession_1util_util_Conversation_OpenGroup_2(JNIEnv *env, + jobject thiz, + jobject open_group) { + auto convos = ptrToConvoInfo(env, thiz); + auto deserialized = deserialize_open_group(env, open_group); + return convos->erase(*deserialized); +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_eraseOpenGroup__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jstring pub_key_hex) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto hex_chars = env->GetStringUTFChars(pub_key_hex, nullptr); + auto result = convos->erase_open(base_url_chars, room_chars, hex_chars); + env->ReleaseStringUTFChars(base_url, base_url_chars); + env->ReleaseStringUTFChars(room, room_chars); + env->ReleaseStringUTFChars(pub_key_hex, hex_chars); + return result; +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_eraseOpenGroup__Ljava_lang_String_2Ljava_lang_String_2_3B( + JNIEnv *env, jobject thiz, jstring base_url, jstring room, jbyteArray pub_key) { + auto convos = ptrToConvoInfo(env, thiz); + auto base_url_chars = env->GetStringUTFChars(base_url, nullptr); + auto room_chars = env->GetStringUTFChars(room, nullptr); + auto pub_key_bytes = util::ustring_from_bytes(env, pub_key); + auto result = convos->erase_open(base_url_chars, room_chars, pub_key_bytes); + env->ReleaseStringUTFChars(base_url, base_url_chars); + env->ReleaseStringUTFChars(room, room_chars); + return result; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getLegacyClosedGroup( + JNIEnv *env, jobject thiz, jstring group_id) { + auto convos = ptrToConvoInfo(env, thiz); + auto id_chars = env->GetStringUTFChars(group_id, nullptr); + auto lgc = convos->get_legacy_closed(id_chars); + env->ReleaseStringUTFChars(group_id, id_chars); + if (lgc) { + auto serialized = serialize_legacy_group(env, *lgc); + return serialized; + } + return nullptr; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_getOrConstructLegacyClosedGroup( + JNIEnv *env, jobject thiz, jstring group_id) { + auto convos = ptrToConvoInfo(env, thiz); + auto id_chars = env->GetStringUTFChars(group_id, nullptr); + auto lgc = convos->get_or_construct_legacy_closed(id_chars); + env->ReleaseStringUTFChars(group_id, id_chars); + return serialize_legacy_group(env, lgc); +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_eraseLegacyClosedGroup( + JNIEnv *env, jobject thiz, jstring group_id) { + auto convos = ptrToConvoInfo(env, thiz); + auto id_chars = env->GetStringUTFChars(group_id, nullptr); + auto result = convos->erase_legacy_closed(id_chars); + env->ReleaseStringUTFChars(group_id, id_chars); + return result; +} +extern "C" +JNIEXPORT jboolean JNICALL +Java_network_loki_messenger_libsession_1util_ConversationVolatileConfig_erase(JNIEnv *env, + jobject thiz, + jobject conversation) { + auto convos = ptrToConvoInfo(env, thiz); + auto deserialized = deserialize_any(env, conversation); + if (deserialized == nullptr) return false; + return convos->erase(*deserialized); } \ No newline at end of file diff --git a/libsession-util/src/main/cpp/conversation.h b/libsession-util/src/main/cpp/conversation.h index 842ebbd3a3..7d35ef9545 100644 --- a/libsession-util/src/main/cpp/conversation.h +++ b/libsession-util/src/main/cpp/conversation.h @@ -11,46 +11,46 @@ inline session::config::ConvoInfoVolatile *ptrToConvoInfo(JNIEnv *env, jobject o return (session::config::ConvoInfoVolatile *) env->GetLongField(obj, pointerField); } -inline jobject serialize_one_to_one(JNIEnv *env, session::config::convo::one_to_one *one_to_one) { +inline jobject serialize_one_to_one(JNIEnv *env, session::config::convo::one_to_one one_to_one) { jclass clazz = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$OneToOne"); jmethodID constructor = env->GetMethodID(clazz, "", "(Ljava/lang/String;JZ)V"); - auto session_id = env->NewStringUTF(one_to_one->session_id.data()); - auto last_read = one_to_one->last_read; - auto unread = one_to_one->unread; + auto session_id = env->NewStringUTF(one_to_one.session_id.data()); + auto last_read = one_to_one.last_read; + auto unread = one_to_one.unread; jobject serialized = env->NewObject(clazz, constructor, session_id, last_read, unread); return serialized; } -inline jobject serialize_open_group(JNIEnv *env, session::config::convo::open_group *open_group) { +inline jobject serialize_open_group(JNIEnv *env, session::config::convo::open_group open_group) { jclass clazz = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$OpenGroup"); jmethodID constructor = env->GetMethodID(clazz, "", "(Ljava/lang/String;Ljava/lang/String;[BJZ)V"); - auto base_url = env->NewStringUTF(open_group->base_url().data()); - auto room = env->NewStringUTF(open_group->room().data()); - auto pubkey_ustring = open_group->pubkey(); + auto base_url = env->NewStringUTF(open_group.base_url().data()); + auto room = env->NewStringUTF(open_group.room().data()); + auto pubkey_ustring = open_group.pubkey(); auto pubkey_jarray = util::bytes_from_ustring(env, session::ustring_view {pubkey_ustring.data(), pubkey_ustring.size()}); - auto last_read = open_group->last_read; - auto unread = open_group->unread; + auto last_read = open_group.last_read; + auto unread = open_group.unread; jobject serialized = env->NewObject(clazz, constructor, base_url, room, pubkey_jarray, last_read, unread); return serialized; } -inline jobject serialize_legacy_group(JNIEnv *env, session::config::convo::legacy_closed_group *legacy_group) { +inline jobject serialize_legacy_group(JNIEnv *env, session::config::convo::legacy_closed_group legacy_group) { jclass clazz = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$LegacyClosedGroup"); jmethodID constructor = env->GetMethodID(clazz, "", "(Ljava/lang/String;JZ)V"); - auto group_id = env->NewStringUTF(legacy_group->id.data()); - auto last_read = legacy_group->last_read; - auto unread = legacy_group->unread; + auto group_id = env->NewStringUTF(legacy_group.id.data()); + auto last_read = legacy_group.last_read; + auto unread = legacy_group.unread; jobject serialized = env->NewObject(clazz, constructor, group_id, last_read, unread); return serialized; } inline jobject serialize_any(JNIEnv *env, session::config::convo::any any) { if (auto* dm = std::get_if(&any)) { - return serialize_one_to_one(env, dm); + return serialize_one_to_one(env, *dm); } else if (auto* og = std::get_if(&any)) { - return serialize_open_group(env, og); + return serialize_open_group(env, *og); } else if (auto* lgc = std::get_if(&any)) { - return serialize_legacy_group(env, lgc); + return serialize_legacy_group(env, *lgc); } return nullptr; } @@ -109,4 +109,19 @@ inline session::config::convo::legacy_closed_group* deserialize_legacy_closed_gr return deserialized; } +inline session::config::convo::any* deserialize_any(JNIEnv *env, jobject convo) { + auto oto_class = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$OneToOne"); + auto og_class = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$OpenGroup"); + auto lgc_class = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$LegacyClosedGroup"); + auto object_class = env->GetObjectClass(convo); + if (env->IsSameObject(object_class, oto_class)) { + return new session::config::convo::any{*deserialize_one_to_one(env, convo)}; + } else if (env->IsSameObject(object_class, og_class)) { + return new session::config::convo::any{*deserialize_open_group(env, convo)}; + } else if (env->IsSameObject(object_class, lgc_class)) { + return new session::config::convo::any{*deserialize_legacy_closed_group(env, convo)}; + } + return nullptr; +} + #endif //SESSION_ANDROID_CONVERSATION_H \ 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 f8c3feee33..376324bccc 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 @@ -78,14 +78,19 @@ class ConversationVolatileConfig(pointer: Long): ConfigBase(pointer) { external fun getOneToOne(pubKeyHex: String): Conversation.OneToOne? external fun getOrConstructOneToOne(pubKeyHex: String): Conversation.OneToOne + external fun eraseOneToOne(pubKeyHex: String): Boolean external fun getOpenGroup(baseUrl: String, room: String, pubKeyHex: String): Conversation.OpenGroup? external fun getOpenGroup(baseUrl: String, room: String, pubKey: ByteArray): Conversation.OpenGroup? - external fun getOrConstructOpenGroup(baseUrl: String, room: String, pubKey: ByteArray): Conversation.OpenGroup external fun getOrConstructOpenGroup(baseUrl: String, room: String, pubKeyHex: String): Conversation.OpenGroup + external fun getOrConstructOpenGroup(baseUrl: String, room: String, pubKey: ByteArray): Conversation.OpenGroup + external fun eraseOpenGroup(openGroup: Conversation.OpenGroup): Boolean + external fun eraseOpenGroup(baseUrl: String, room: String, pubKeyHex: String): Boolean + external fun eraseOpenGroup(baseUrl: String, room: String, pubKey: ByteArray): Boolean external fun getLegacyClosedGroup(groupId: String): Conversation.LegacyClosedGroup? external fun getOrConstructLegacyClosedGroup(groupId: String): Conversation.LegacyClosedGroup + external fun eraseLegacyClosedGroup(groupId: String): Boolean external fun erase(conversation: Conversation): Boolean external fun set(toStore: Conversation)