diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index 74319eaabe..9cd6641cd0 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -366,7 +366,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF is Conversation.LegacyGroup -> conversation.groupId.let { getOrCreateThreadIdFor("", it,null) } - is Conversation.Community -> conversation.baseUrl.let { + is Conversation.Community -> conversation.baseCommunityInfo.baseUrl.let { getOrCreateThreadIdFor("",null, it) } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt b/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt index c98f512b87..2e3a3ca252 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt @@ -141,7 +141,8 @@ object ConfigurationMessageUtilities { blocked = settings.isBlocked, approved = settings.isApproved, approvedMe = settings.hasApprovedMe(), - profilePicture = userPic ?: UserPic.DEFAULT + profilePicture = userPic ?: UserPic.DEFAULT, + priority = 0 // TODO: read this in from a pinned priority ) contactConfig.set(contactInfo) } diff --git a/libsession-util/src/main/cpp/conversation.h b/libsession-util/src/main/cpp/conversation.h index 76f191e790..db18b85276 100644 --- a/libsession-util/src/main/cpp/conversation.h +++ b/libsession-util/src/main/cpp/conversation.h @@ -23,14 +23,12 @@ inline jobject serialize_one_to_one(JNIEnv *env, session::config::convo::one_to_ inline jobject serialize_open_group(JNIEnv *env, session::config::convo::community community) { jclass clazz = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$Community"); - jmethodID constructor = env->GetMethodID(clazz, "", "(Ljava/lang/String;Ljava/lang/String;[BJZ)V"); - auto base_url = env->NewStringUTF(community.base_url().data()); - auto room = env->NewStringUTF(community.room().data()); - auto pubkey_ustring = community.pubkey(); - auto pubkey_jarray = util::bytes_from_ustring(env, session::ustring_view {pubkey_ustring.data(), pubkey_ustring.size()}); + auto base_community = util::serialize_base_community(env, community); + jmethodID constructor = env->GetMethodID(clazz, "", + "(Lnetwork/loki/messenger/libsession_util/util/BaseCommunityInfo;JZ)V"); auto last_read = community.last_read; auto unread = community.unread; - jobject serialized = env->NewObject(clazz, constructor, base_url, room, pubkey_jarray, last_read, unread); + jobject serialized = env->NewObject(clazz, constructor, base_community, last_read, unread); return serialized; } @@ -72,25 +70,23 @@ inline session::config::convo::one_to_one* deserialize_one_to_one(JNIEnv *env, j inline session::config::convo::community* deserialize_open_group(JNIEnv *env, jobject info) { auto clazz = env->FindClass("network/loki/messenger/libsession_util/util/Conversation$Community"); - auto url_getter = env->GetFieldID(clazz, "baseUrl", "Ljava/lang/String;"); - auto room_getter = env->GetFieldID(clazz, "room", "Ljava/lang/String;"); - auto pub_key_getter = env->GetFieldID(clazz, "pubKey", "[B"); + + auto base_community_getter = env->GetFieldID(clazz, "baseCommunityInfo", "Lnetwork/loki/messenger/libsession_util/util/BaseCommunityInfo;"); + auto last_read_getter = env->GetFieldID(clazz, "lastRead", "J"); auto unread_getter = env->GetFieldID(clazz, "unread", "Z"); - jstring base_url = static_cast(env->GetObjectField(info, url_getter)); - jstring room = static_cast(env->GetObjectField(info, room_getter)); - jbyteArray pub_key = (jbyteArray)env->GetObjectField(info, pub_key_getter); - auto base_bytes = env->GetStringUTFChars(base_url, nullptr); - auto base_string = std::string {base_bytes}; - auto room_bytes = env->GetStringUTFChars(room, nullptr); - auto room_string = std::string {room_bytes}; - auto pub_key_ustring = util::ustring_from_bytes(env, pub_key); - auto deserialized = new session::config::convo::community(base_string, room_string,pub_key_ustring); + auto base_community_info = env->GetObjectField(info, base_community_getter); + + auto base_community_deserialized = util::deserialize_base_community(env, base_community_info); + + auto deserialized = new session::config::convo::community{ + base_community_deserialized.base_url(), + base_community_deserialized.room(), + base_community_deserialized.pubkey() + }; deserialized->last_read = env->GetLongField(info, last_read_getter); deserialized->unread = env->GetBooleanField(info, unread_getter); - env->ReleaseStringUTFChars(base_url, base_bytes); - env->ReleaseStringUTFChars(room, room_bytes); return deserialized; } diff --git a/libsession-util/src/main/cpp/user_groups.cpp b/libsession-util/src/main/cpp/user_groups.cpp index 728ca27fbc..9772301cf1 100644 --- a/libsession-util/src/main/cpp/user_groups.cpp +++ b/libsession-util/src/main/cpp/user_groups.cpp @@ -57,7 +57,7 @@ Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getCommunityInfo(J jobject community_info = nullptr; if (community) { - serialize_legacy_group_info() + community_info = serialize_community_info(env, *community); } // TODO: implement getCommunityInfo() @@ -89,7 +89,7 @@ Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getOrConstructLega extern "C" JNIEXPORT void JNICALL -Java_network_loki_messenger_libsession_1util_UserGroupsConfig_set__Lnetwork_loki_messenger_libsession_1util_util_GroupInfo_CommunityInfo_2( +Java_network_loki_messenger_libsession_1util_UserGroupsConfig_set__Lnetwork_loki_messenger_libsession_1util_util_GroupInfo_CommunityGroupInfo_2( JNIEnv *env, jobject thiz, jobject community_info) { // TODO: implement set() } diff --git a/libsession-util/src/main/cpp/user_groups.h b/libsession-util/src/main/cpp/user_groups.h index 49c47da536..b22fe1a077 100644 --- a/libsession-util/src/main/cpp/user_groups.h +++ b/libsession-util/src/main/cpp/user_groups.h @@ -21,23 +21,37 @@ val encPubKey: String, val encSecKey: String, val priority: Int */ -inline session::config::legacy_group_info deserialize_legacy_group_info(JNIEnv *env, jobject info) { +inline session::config::legacy_group_info* deserialize_legacy_group_info(JNIEnv *env, jobject info) { auto clazz = env->FindClass("network/loki/messenger/libsession_util/util/GroupInfo$LegacyGroupInfo"); auto id_field = env->GetFieldID(clazz, "sessionId", "Ljava/lang/String;"); auto name_field = env->GetFieldID(clazz, "name", "Ljava/lang/String;"); auto members_field = env->GetFieldID(clazz, "members", "Ljava/util/Map;"); auto hidden_field = env->GetFieldID(clazz, "hidden", "Z"); - auto enc_pub_key_field = env->GetFieldID(clazz, "encPubKey", "Ljava/lang/String;"); - auto enc_sec_key_field = env->GetFieldID(clazz, "encSecKey", "Ljava/lang/String;"); + auto enc_pub_key_field = env->GetFieldID(clazz, "encPubKey", "[B"); + auto enc_sec_key_field = env->GetFieldID(clazz, "encSecKey", "[B"); auto priority_field = env->GetFieldID(clazz, "priority", "I"); jstring id = static_cast(env->GetObjectField(info, id_field)); jstring name = static_cast(env->GetObjectField(info, name_field)); jobject members_map = env->GetObjectField(info, members_field); bool hidden = env->GetBooleanField(info, hidden_field); - jstring enc_pub_key = static_cast(env->GetObjectField(info, enc_pub_key_field)); - jstring enc_sec_key = static_cast(env->GetObjectField(info, enc_sec_key_field)); + jbyteArray enc_pub_key = static_cast(env->GetObjectField(info, enc_pub_key_field)); + jbyteArray enc_sec_key = static_cast(env->GetObjectField(info, enc_sec_key_field)); int priority = env->GetIntField(info, priority_field); + auto id_bytes = env->GetStringUTFChars(id, nullptr); + auto name_bytes = env->GetStringUTFChars(name, nullptr); + auto enc_pub_key_bytes = util::ustring_from_bytes(env, enc_pub_key); + auto enc_sec_key_bytes = util::ustring_from_bytes(env, enc_sec_key); + + auto info_deserialized = new session::config::legacy_group_info(id_bytes); + + info_deserialized->priority = priority; + // TODO: iterate over map and insert as admins + info_deserialized->enc_pubkey = enc_pub_key_bytes; + info_deserialized->enc_seckey = enc_sec_key_bytes; + env->ReleaseStringUTFChars(id, id_bytes); + env->ReleaseStringUTFChars(name, name_bytes); + return info_deserialized; } inline std::map deserialize_members(JNIEnv *env, jobject members_map) { @@ -54,9 +68,10 @@ inline jobject serialize_members(JNIEnv *env, std::map member inline jobject serialize_community_info(JNIEnv *env, session::config::community_info info) { auto priority = info.priority; - auto open_group = session::config::community::parse_full_url(info.full_url()); - auto serialized_community = serialize_open_group(env, (session::config::community) info); - return nullptr; + auto serialized_community = util::serialize_base_community(env, info); + auto clazz = env->FindClass("network/loki/messenger/libsession_util/util/GroupInfo$CommunityGroupInfo"); + + return serialized_community; } #endif //SESSION_ANDROID_USER_GROUPS_H diff --git a/libsession-util/src/main/cpp/util.cpp b/libsession-util/src/main/cpp/util.cpp index 8a7e429439..8ef48381cb 100644 --- a/libsession-util/src/main/cpp/util.cpp +++ b/libsession-util/src/main/cpp/util.cpp @@ -37,6 +37,20 @@ namespace util { return {pic, key}; } + jobject serialize_base_community(JNIEnv *env, const session::config::community& community) { + jclass base_community_clazz = env->FindClass("network/loki/messenger/libsession_util/util/BaseCommunityInfo"); + jmethodID base_community_constructor = env->GetMethodID(base_community_clazz, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + auto base_url = env->NewStringUTF(community.base_url().data()); + auto room = env->NewStringUTF(community.room().data()); + auto pubkey_jstring = env->NewStringUTF(community.pubkey_hex().data()); + jobject ret = env->NewObject(base_community_clazz, base_community_constructor, base_url, room, pubkey_jstring); + return ret; + } + + session::config::community deserialize_base_community(JNIEnv *env, jobject base_community) { + + } + } extern "C" diff --git a/libsession-util/src/main/cpp/util.h b/libsession-util/src/main/cpp/util.h index 1aa843fd5a..78556629bc 100644 --- a/libsession-util/src/main/cpp/util.h +++ b/libsession-util/src/main/cpp/util.h @@ -6,12 +6,15 @@ #include #include "session/types.hpp" #include "session/config/profile_pic.hpp" +#include "session/config/user_groups.hpp" namespace util { jbyteArray bytes_from_ustring(JNIEnv* env, session::ustring_view from_str); session::ustring ustring_from_bytes(JNIEnv* env, jbyteArray byteArray); jobject serialize_user_pic(JNIEnv *env, session::config::profile_pic pic); std::pair deserialize_user_pic(JNIEnv *env, jobject user_pic); + jobject serialize_base_community(JNIEnv *env, const session::config::community& base_community); + session::config::community deserialize_base_community(JNIEnv *env, jobject base_community); } #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 dcb63fd259..9b503df392 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 @@ -154,11 +154,11 @@ class UserGroupsConfig(pointer: Long): ConfigBase(pointer) { external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): UserGroupsConfig } - external fun getCommunityInfo(baseUrl: String, room: String): GroupInfo.CommunityInfo? + external fun getCommunityInfo(baseUrl: String, room: String): GroupInfo.CommunityGroupInfo? external fun getLegacyGroupInfo(sessionId: String): GroupInfo.LegacyGroupInfo? - external fun getOrConstructCommunityInfo(baseUrl: String, room: String, pubKeyHex: String): GroupInfo.CommunityInfo + external fun getOrConstructCommunityInfo(baseUrl: String, room: String, pubKeyHex: String): GroupInfo.CommunityGroupInfo external fun getOrConstructLegacyGroupInfo(sessionId: String): GroupInfo.LegacyGroupInfo - external fun set(communityInfo: GroupInfo.CommunityInfo) + external fun set(communityInfo: GroupInfo.CommunityGroupInfo) external fun set(legacyGroupInfo: GroupInfo.LegacyGroupInfo) } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/BaseCommunity.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/BaseCommunity.kt new file mode 100644 index 0000000000..aa2bbc0620 --- /dev/null +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/BaseCommunity.kt @@ -0,0 +1,3 @@ +package network.loki.messenger.libsession_util.util + +data class BaseCommunityInfo(val baseUrl: String, val room: String, val pubKeyHex: String) \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Contact.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Contact.kt index 580488a4ec..d842f6f97f 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Contact.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Contact.kt @@ -8,5 +8,5 @@ data class Contact( var approvedMe: Boolean = false, var blocked: Boolean = false, var profilePicture: UserPic = UserPic.DEFAULT, - var priority: Int + var priority: Int = 0 ) \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Conversation.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Conversation.kt index 6dae95ac3e..8473c6e8b5 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Conversation.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Conversation.kt @@ -1,7 +1,5 @@ package network.loki.messenger.libsession_util.util -import org.session.libsignal.utilities.Hex - sealed class Conversation { abstract var lastRead: Long @@ -14,43 +12,16 @@ sealed class Conversation { ): Conversation() data class Community( - val baseUrl: String, - val room: String, // lowercase - val pubKey: ByteArray, + val baseCommunityInfo: BaseCommunityInfo, override var lastRead: Long, override var unread: Boolean ) : Conversation() { - - val pubKeyHex: String - get() = Hex.toStringCondensed(pubKey) companion object { init { System.loadLibrary("session_util") } external fun parseFullUrl(fullUrl: String): Triple? } - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as Community - - if (baseUrl != other.baseUrl) return false - if (room != other.room) return false - if (!pubKey.contentEquals(other.pubKey)) return false - if (lastRead != other.lastRead) return false - - return true - } - - override fun hashCode(): Int { - var result = baseUrl.hashCode() - result = 31 * result + room.hashCode() - result = 31 * result + pubKey.contentHashCode() - result = 31 * result + lastRead.hashCode() - return result - } } data class LegacyGroup( diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/GroupInfo.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/GroupInfo.kt index 23b2618b00..d7bd80773d 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/GroupInfo.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/GroupInfo.kt @@ -2,15 +2,15 @@ package network.loki.messenger.libsession_util.util sealed class GroupInfo { - data class CommunityInfo(val community: Conversation.Community, val priority: Int) : GroupInfo() + data class CommunityGroupInfo(val community: BaseCommunityInfo, val priority: Int) : GroupInfo() data class LegacyGroupInfo( val sessionId: String, val name: String, val members: Map, val hidden: Boolean, - val encPubKey: String, - val encSecKey: String, + val encPubKey: ByteArray, + val encSecKey: ByteArray, val priority: Int ): GroupInfo() { companion object {