From 6193fe466885de089ad42dd227a42a1a878e9e29 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Tue, 4 Apr 2023 17:43:21 +1000 Subject: [PATCH] feat: update shared library to use priority only, fix compile errors, fix group member sync problem --- .../securesms/database/Storage.kt | 32 ++++++++++--------- .../securesms/groups/ClosedGroupManager.kt | 5 ++- .../util/ConfigurationMessageUtilities.kt | 11 ++++--- libsession-util/libsession-util | 2 +- libsession-util/src/main/cpp/contacts.h | 10 ++---- libsession-util/src/main/cpp/user_groups.h | 13 ++++---- libsession-util/src/main/cpp/user_profile.cpp | 13 -------- .../loki/messenger/libsession_util/Config.kt | 6 ++-- .../messenger/libsession_util/util/Contact.kt | 1 - .../libsession_util/util/GroupInfo.kt | 5 +-- 10 files changed, 41 insertions(+), 57 deletions(-) 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 d5e36df78a..19c3dd3bdd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -117,10 +117,12 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF // don't update our own address into the contacts DB if (getUserPublicKey() != address.serialize()) { val contacts = configFactory.contacts ?: return - contacts.upsertContact(address.serialize()) + contacts.upsertContact(address.serialize()) { + priority = ConfigBase.PRIORITY_VISIBLE + } } else { val userProfile = configFactory.user ?: return - userProfile.setNtsHidden(false) + userProfile.setNtsPriority(ConfigBase.PRIORITY_VISIBLE) } val newVolatileParams = volatile.getOrConstructOneToOne(address.serialize()).copy( lastRead = SnodeAPI.nowWithOffset @@ -146,11 +148,11 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF if (getUserPublicKey() != address.serialize()) { val contacts = configFactory.contacts ?: return contacts.upsertContact(address.serialize()) { - // hidden = true TODO: maybe this? + priority = ConfigBase.PRIORITY_HIDDEN } } else { val userProfile = configFactory.user ?: return - userProfile.setNtsHidden(true) + userProfile.setNtsPriority(ConfigBase.PRIORITY_HIDDEN) } } ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(context) @@ -414,7 +416,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF profileManager.setProfileKey(context, recipient, userPic.key) setUserProfilePictureURL(userPic.url) } - if (userProfile.getNtsHidden()) { + if (userProfile.getNtsPriority() == ConfigBase.PRIORITY_HIDDEN) { // delete nts thread if needed val ourThread = getThreadId(recipient) ?: return deleteConversation(ourThread) @@ -524,8 +526,8 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF val existingThread = existingGroup?.let { getThreadId(existingGroup.encodedId) } if (existingGroup != null) { Log.d("Loki-DBG", "Existing closed group, don't add") - if (group.hidden && existingThread != null) { - threadDb.setThreadArchived(existingThread) + if (group.priority == ConfigBase.PRIORITY_HIDDEN && existingThread != null) { + threadDb.deleteConversation(existingThread) } else if (existingThread == null) { Log.w("Loki-DBG", "Existing group had no thread to hide") } @@ -766,7 +768,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF sessionId = groupPublicKey, name = name, members = members, - hidden = false, + priority = ConfigBase.PRIORITY_VISIBLE, encPubKey = encryptionKeyPair.publicKey.serialize(), encSecKey = encryptionKeyPair.privateKey.serialize(), disappearingTimer = 0L @@ -801,7 +803,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF members = membersMap, encPubKey = latestKeyPair.publicKey.serialize(), encSecKey = latestKeyPair.privateKey.serialize(), - priority = if (isPinned(threadID)) 1 else 0, + priority = if (isPinned(threadID)) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE, disappearingTimer = recipientSettings.expireMessages.toLong() ) userGroups.set(groupInfo) @@ -1070,7 +1072,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF profileManager.setUnidentifiedAccessMode(context, recipient, Recipient.UnidentifiedAccessMode.UNKNOWN) profileManager.setProfilePictureURL(context, recipient, url) } - if (contact.hidden) { + if (contact.priority == ConfigBase.PRIORITY_HIDDEN) { getThreadId(fromSerialized(contact.id))?.let { conversationThreadId -> deleteConversation(conversationThreadId) } @@ -1146,25 +1148,25 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF val threadRecipient = getRecipientForThread(threadID) ?: return if (threadRecipient.isLocalNumber) { val user = configFactory.user ?: return - user.setNtsPriority(if (isPinned) 1 else 0) + user.setNtsPriority(if (isPinned) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE) } else if (threadRecipient.isContactRecipient) { val contacts = configFactory.contacts ?: return contacts.upsertContact(threadRecipient.address.serialize()) { - priority = if (isPinned) 1 else 0 + priority = if (isPinned) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE } } else if (threadRecipient.isGroupRecipient) { val groups = configFactory.userGroups ?: return if (threadRecipient.isClosedGroupRecipient) { val sessionId = GroupUtil.doubleDecodeGroupId(threadRecipient.address.serialize()) val newGroupInfo = groups.getOrConstructLegacyGroupInfo(sessionId).copy ( - priority = if (isPinned) 1 else 0 + priority = if (isPinned) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE ) groups.set(newGroupInfo) } else if (threadRecipient.isOpenGroupRecipient) { val openGroup = getOpenGroup(threadID) ?: return val (baseUrl, room, pubKeyHex) = BaseCommunityInfo.parseFullUrl(openGroup.joinURL) ?: return val newGroupInfo = groups.getOrConstructCommunityInfo(baseUrl, room, Hex.toStringCondensed(pubKeyHex)).copy ( - priority = if (isPinned) 1 else 0 + priority = if (isPinned) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE ) groups.set(newGroupInfo) } @@ -1187,7 +1189,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper, private val configF // TODO: handle contact val contacts = configFactory.contacts ?: return contacts.upsertContact(recipient.address.serialize()) { - this.hidden = true + this.priority = ConfigBase.PRIORITY_HIDDEN } ConfigurationMessageUtilities.forceSyncConfigurationNowIfNeeded(context) } else if (recipient.isClosedGroupRecipient) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt b/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt index b11c3ebc91..b1708182d4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt @@ -1,7 +1,7 @@ package org.thoughtcrime.securesms.groups import android.content.Context -import network.loki.messenger.libsession_util.util.GroupInfo +import network.loki.messenger.libsession_util.ConfigBase import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2 @@ -9,7 +9,6 @@ import org.session.libsession.utilities.Address import org.session.libsession.utilities.GroupRecord import org.session.libsession.utilities.GroupUtil import org.session.libsession.utilities.recipients.Recipient -import org.session.libsignal.utilities.toHexString import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.dependencies.ConfigFactory @@ -54,7 +53,7 @@ object ClosedGroupManager { members = latestMemberMap, name = group.title, disappearingTimer = groupRecipientSettings.expireMessages.toLong(), - priority = if (storage.isPinned(threadId)) 1 else 0, + priority = if (storage.isPinned(threadId)) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE, encSecKey = latestKeyPair.privateKey.serialize(), encPubKey = latestKeyPair.publicKey.serialize() ) 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 418a9e00f2..bc0210bf49 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt @@ -27,7 +27,7 @@ import org.session.libsignal.utilities.Hex import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.toHexString import org.thoughtcrime.securesms.dependencies.DatabaseComponent -import java.util.* +import java.util.Timer object ConfigurationMessageUtilities { @@ -123,7 +123,11 @@ object ConfigurationMessageUtilities { profile.setPic(UserPic(picUrl, picKey)) } val ownThreadId = storage.getThreadId(Address.fromSerialized(ownPublicKey)) - profile.setNtsHidden(ownThreadId != null) + profile.setNtsPriority( + if (ownThreadId != null) + if (storage.isPinned(ownThreadId)) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE + else ConfigBase.PRIORITY_HIDDEN + ) if (ownThreadId != null) { // have NTS thread val ntsPinned = storage.isPinned(ownThreadId) @@ -248,8 +252,7 @@ object ConfigurationMessageUtilities { sessionId = sessionId, name = group.title, members = admins + members, - hidden = threadId == null, - priority = if (isPinned) 1 else 0, + priority = if (isPinned) ConfigBase.PRIORITY_PINNED else ConfigBase.PRIORITY_VISIBLE, encPubKey = encryptionKeyPair.publicKey.serialize(), encSecKey = encryptionKeyPair.privateKey.serialize(), disappearingTimer = recipient.expireMessages.toLong() diff --git a/libsession-util/libsession-util b/libsession-util/libsession-util index 5241878bb2..3817b543f5 160000 --- a/libsession-util/libsession-util +++ b/libsession-util/libsession-util @@ -1 +1 @@ -Subproject commit 5241878bb20a9232b011f709adb87d1e6e7496b4 +Subproject commit 3817b543f5f862b58afd50c285fbccc26bc82ee0 diff --git a/libsession-util/src/main/cpp/contacts.h b/libsession-util/src/main/cpp/contacts.h index 97f9d96af7..75b06fc087 100644 --- a/libsession-util/src/main/cpp/contacts.h +++ b/libsession-util/src/main/cpp/contacts.h @@ -13,18 +13,17 @@ inline session::config::Contacts *ptrToContacts(JNIEnv *env, jobject obj) { inline jobject serialize_contact(JNIEnv *env, session::config::contact_info info) { jclass contactClass = env->FindClass("network/loki/messenger/libsession_util/util/Contact"); - jmethodID constructor = env->GetMethodID(contactClass, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZZLnetwork/loki/messenger/libsession_util/util/UserPic;ILnetwork/loki/messenger/libsession_util/util/ExpiryMode;)V"); + jmethodID constructor = env->GetMethodID(contactClass, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZLnetwork/loki/messenger/libsession_util/util/UserPic;ILnetwork/loki/messenger/libsession_util/util/ExpiryMode;)V"); jstring id = env->NewStringUTF(info.session_id.data()); jstring name = env->NewStringUTF(info.name.data()); jstring nickname = env->NewStringUTF(info.nickname.data()); - jboolean approved, approvedMe, blocked, hidden; + jboolean approved, approvedMe, blocked; approved = info.approved; approvedMe = info.approved_me; blocked = info.blocked; - hidden = info.hidden; jobject profilePic = util::serialize_user_pic(env, info.profile_picture); jobject returnObj = env->NewObject(contactClass, constructor, id, name, nickname, approved, - approvedMe, blocked, hidden, profilePic, info.priority, + approvedMe, blocked, profilePic, info.priority, util::serialize_expiry(env, info.exp_mode, info.exp_timer)); return returnObj; } @@ -40,7 +39,6 @@ deserialize_contact(JNIEnv *env, jobject info, session::config::Contacts *conf) getApproved = env->GetFieldID(contactClass, "approved", "Z"); getApprovedMe = env->GetFieldID(contactClass, "approvedMe", "Z"); getBlocked = env->GetFieldID(contactClass, "blocked", "Z"); - getHidden = env->GetFieldID(contactClass, "hidden", "Z"); getUserPic = env->GetFieldID(contactClass, "profilePicture", "Lnetwork/loki/messenger/libsession_util/util/UserPic;"); getPriority = env->GetFieldID(contactClass, "priority", "I"); @@ -54,7 +52,6 @@ deserialize_contact(JNIEnv *env, jobject info, session::config::Contacts *conf) approved = env->GetBooleanField(info, getApproved); approvedMe = env->GetBooleanField(info, getApprovedMe); blocked = env->GetBooleanField(info, getBlocked); - hidden = env->GetBooleanField(info, getHidden); jobject user_pic = env->GetObjectField(info, getUserPic); jobject expiry_mode = env->GetObjectField(info, getExpiry); @@ -86,7 +83,6 @@ deserialize_contact(JNIEnv *env, jobject info, session::config::Contacts *conf) contact_info.approved = approved; contact_info.approved_me = approvedMe; contact_info.blocked = blocked; - contact_info.hidden = hidden; if (!url.empty() && !key.empty()) { contact_info.profile_picture = session::config::profile_pic(url, key); } else { diff --git a/libsession-util/src/main/cpp/user_groups.h b/libsession-util/src/main/cpp/user_groups.h index 7846054443..aa3f3e1410 100644 --- a/libsession-util/src/main/cpp/user_groups.h +++ b/libsession-util/src/main/cpp/user_groups.h @@ -47,7 +47,6 @@ inline session::config::legacy_group_info deserialize_legacy_group_info(JNIEnv * 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", "[B"); auto enc_sec_key_field = env->GetFieldID(clazz, "encSecKey", "[B"); auto priority_field = env->GetFieldID(clazz, "priority", "I"); @@ -55,7 +54,6 @@ inline session::config::legacy_group_info deserialize_legacy_group_info(JNIEnv * 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); 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); @@ -67,10 +65,12 @@ inline session::config::legacy_group_info deserialize_legacy_group_info(JNIEnv * auto info_deserialized = conf->get_or_construct_legacy_group(id_bytes); - info_deserialized.priority = priority; + auto current_members = info_deserialized.members(); + for (auto member = current_members.begin(); member != current_members.end(); ++member) { + info_deserialized.erase(member->first); + } deserialize_members_into(env, members_map, info_deserialized); info_deserialized.name = name_bytes; - info_deserialized.hidden = hidden; info_deserialized.enc_pubkey = enc_pub_key_bytes; info_deserialized.enc_seckey = enc_sec_key_bytes; info_deserialized.disappearing_timer = std::chrono::seconds(env->GetLongField(info, disappearing_timer_field)); @@ -116,11 +116,10 @@ inline jobject serialize_legacy_group_info(JNIEnv *env, session::config::legacy_ jbyteArray enc_pubkey = util::bytes_from_ustring(env, info.enc_pubkey); jbyteArray enc_seckey = util::bytes_from_ustring(env, info.enc_seckey); int priority = info.priority; - bool hidden = info.hidden; jclass legacy_group_class = env->FindClass("network/loki/messenger/libsession_util/util/GroupInfo$LegacyGroupInfo"); - jmethodID constructor = env->GetMethodID(legacy_group_class, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Z[B[BIJ)V"); - jobject serialized = env->NewObject(legacy_group_class, constructor, session_id, name, members, hidden, enc_pubkey, enc_seckey, priority, (jlong) info.disappearing_timer.count()); + jmethodID constructor = env->GetMethodID(legacy_group_class, "", "(Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;[B[BIJ)V"); + jobject serialized = env->NewObject(legacy_group_class, constructor, session_id, name, members, enc_pubkey, enc_seckey, priority, (jlong) info.disappearing_timer.count()); return serialized; } diff --git a/libsession-util/src/main/cpp/user_profile.cpp b/libsession-util/src/main/cpp/user_profile.cpp index 1b8426d75c..fb58d02913 100644 --- a/libsession-util/src/main/cpp/user_profile.cpp +++ b/libsession-util/src/main/cpp/user_profile.cpp @@ -84,21 +84,8 @@ Java_network_loki_messenger_libsession_1util_UserProfile_setNtsPriority(JNIEnv * profile->set_nts_priority(priority); } extern "C" -JNIEXPORT void JNICALL -Java_network_loki_messenger_libsession_1util_UserProfile_setNtsHidden(JNIEnv *env, jobject thiz, - jboolean is_hidden) { - auto profile = ptrToProfile(env, thiz); - profile->set_nts_hidden(is_hidden); -} -extern "C" JNIEXPORT jint JNICALL Java_network_loki_messenger_libsession_1util_UserProfile_getNtsPriority(JNIEnv *env, jobject thiz) { auto profile = ptrToProfile(env, thiz); return profile->get_nts_priority(); -} -extern "C" -JNIEXPORT jboolean JNICALL -Java_network_loki_messenger_libsession_1util_UserProfile_getNtsHidden(JNIEnv *env, jobject thiz) { - auto profile = ptrToProfile(env, thiz); - return profile->get_nts_hidden(); } \ 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 88d4737846..13c6a59487 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 @@ -25,6 +25,10 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) { const val isNewConfigEnabled = true + const val PRIORITY_HIDDEN = -1 + const val PRIORITY_VISIBLE = 0 + const val PRIORITY_PINNED = 1 + } external fun dirty(): Boolean @@ -96,8 +100,6 @@ class UserProfile(pointer: Long) : ConfigBase(pointer) { external fun setPic(userPic: UserPic) external fun setNtsPriority(priority: Int) external fun getNtsPriority(): Int - external fun setNtsHidden(isHidden: Boolean) - external fun getNtsHidden(): Boolean } class ConversationVolatileConfig(pointer: Long): ConfigBase(pointer) { 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 6498a6f4a4..f7f7b0a5b7 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 @@ -7,7 +7,6 @@ data class Contact( var approved: Boolean = false, var approvedMe: Boolean = false, var blocked: Boolean = false, - var hidden: Boolean = false, var profilePicture: UserPic = UserPic.DEFAULT, var priority: Int = 0, var expiryMode: ExpiryMode 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 0781bbc6d9..8201baf940 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 @@ -8,10 +8,9 @@ sealed class GroupInfo { val sessionId: String, val name: String, val members: Map, - val hidden: Boolean, val encPubKey: ByteArray, val encSecKey: ByteArray, - val priority: Int = 0, + val priority: Int, val disappearingTimer: Long ): GroupInfo() { companion object { @@ -28,7 +27,6 @@ sealed class GroupInfo { if (sessionId != other.sessionId) return false if (name != other.name) return false if (members != other.members) return false - if (hidden != other.hidden) return false if (!encPubKey.contentEquals(other.encPubKey)) return false if (!encSecKey.contentEquals(other.encSecKey)) return false if (priority != other.priority) return false @@ -40,7 +38,6 @@ sealed class GroupInfo { var result = sessionId.hashCode() result = 31 * result + name.hashCode() result = 31 * result + members.hashCode() - result = 31 * result + hidden.hashCode() result = 31 * result + encPubKey.contentHashCode() result = 31 * result + encSecKey.contentHashCode() result = 31 * result + priority