From d4d4f81c36498da80b010f8c92ec4b6b863ce9dc Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Fri, 5 May 2023 16:05:43 +1000 Subject: [PATCH] fix: remove user notifications for leaving group to prevent synced device issues, don't create thread in messages for new closed groups, includei nactive groups in the deletion queries for merging group configs --- .../conversation/v2/menus/ConversationMenuHelper.kt | 2 +- .../thoughtcrime/securesms/database/GroupDatabase.java | 4 ++-- .../java/org/thoughtcrime/securesms/database/Storage.kt | 7 ++++--- .../thoughtcrime/securesms/database/ThreadDatabase.java | 3 --- .../thoughtcrime/securesms/groups/ClosedGroupManager.kt | 4 ++-- .../securesms/groups/EditClosedGroupActivity.kt | 2 +- .../securesms/util/ConfigurationMessageUtilities.kt | 2 +- .../org/session/libsession/database/StorageProtocol.kt | 2 +- .../libsession/messaging/jobs/BatchMessageReceiveJob.kt | 2 +- .../messaging/messages/control/ConfigurationMessage.kt | 2 +- .../messaging/sending_receiving/MessageSender.kt | 2 +- .../sending_receiving/ReceivedMessageHandler.kt | 9 ++------- 12 files changed, 17 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationMenuHelper.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationMenuHelper.kt index 3de2669503..a0c753d8da 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationMenuHelper.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/menus/ConversationMenuHelper.kt @@ -321,7 +321,7 @@ object ConversationMenuHelper { } try { if (isClosedGroup) { - MessageSender.leave(groupPublicKey!!, true) + MessageSender.leave(groupPublicKey!!, notifyUser = false) } else { Toast.makeText(context, R.string.ConversationActivity_error_leaving_group, Toast.LENGTH_LONG).show() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java index 79adead57e..228bcffe36 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/GroupDatabase.java @@ -133,12 +133,12 @@ public class GroupDatabase extends Database implements LokiOpenGroupDatabaseProt return new Reader(cursor); } - public List getAllGroups() { + public List getAllGroups(boolean includeInactive) { Reader reader = getGroups(); GroupRecord record; List groups = new LinkedList<>(); while ((record = reader.getNext()) != null) { - if (record.isActive()) { groups.add(record); } + if (record.isActive() || includeInactive) { groups.add(record); } } reader.close(); return groups; 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 c5f2609e5f..a3bb4265df 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -497,7 +497,7 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co val toAddCommunities = communities.filter { it.community.fullUrl() !in existingCommunities.map { it.value.joinURL } } val existingJoinUrls = existingCommunities.values.map { it.joinURL } - val existingClosedGroups = getAllGroups().filter { it.isClosedGroup } + val existingClosedGroups = getAllGroups(includeInactive = true).filter { it.isClosedGroup } val lgcIds = lgc.map { it.sessionId } val toDeleteClosedGroups = existingClosedGroups.filter { group -> GroupUtil.doubleDecodeGroupId(group.encodedId) !in lgcIds @@ -513,6 +513,7 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co if (threadId == null) { Log.w("Loki-DBG", "Existing group had no thread to delete") } else { + Log.d("Loki-DBG", "Deleting group for thread $threadId") ClosedGroupManager.silentlyRemoveGroup(context,threadId,GroupUtil.doubleDecodeGroupId(deleteGroup.encodedId), deleteGroup.encodedId, localUserPublicKey, delete = true) } } @@ -954,8 +955,8 @@ open class Storage(context: Context, helper: SQLCipherOpenHelper, private val co OpenGroupManager.updateOpenGroup(openGroup, context) } - override fun getAllGroups(): List { - return DatabaseComponent.get(context).groupDatabase().allGroups + override fun getAllGroups(includeInactive: Boolean): List { + return DatabaseComponent.get(context).groupDatabase().getAllGroups(includeInactive) } override fun addOpenGroup(urlAsString: String): OpenGroupApi.RoomInfo? { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java index 399d33f9ea..31ffb3c189 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -535,7 +535,6 @@ public class ThreadDatabase extends Database { // edge case where we set the last seen time for a conversation before it loads messages (joining community for example) MmsSmsDatabase mmsSmsDatabase = DatabaseComponent.get(context).mmsSmsDatabase(); if (mmsSmsDatabase.getConversationCount(threadId) <= 0) return false; - Log.d("Loki-DBG", "setLastSeen "+threadId+" @ "+timestamp); SQLiteDatabase db = databaseHelper.getWritableDatabase(); @@ -559,7 +558,6 @@ public class ThreadDatabase extends Database { db.execSQL(reflectUpdates, new Object[]{threadId}); db.setTransactionSuccessful(); db.endTransaction(); - Log.d("Loki-DBG", "Updated last seen to "+timestamp); notifyConversationListListeners(); return true; } @@ -800,7 +798,6 @@ public class ThreadDatabase extends Database { public boolean markAllAsRead(long threadId, boolean isGroupRecipient, long lastSeenTime) { MmsSmsDatabase mmsSmsDatabase = DatabaseComponent.get(context).mmsSmsDatabase(); if (mmsSmsDatabase.getConversationCount(threadId) <= 0) return false; - Log.d("Loki-DBG", "markAllAsRead "+threadId+" @ "+lastSeenTime); List messages = setRead(threadId, lastSeenTime); if (isGroupRecipient) { for (MarkedMessageInfo message: messages) { 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 b1708182d4..aab09fcf36 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/ClosedGroupManager.kt @@ -16,11 +16,11 @@ object ClosedGroupManager { fun silentlyRemoveGroup(context: Context, threadId: Long, groupPublicKey: String, groupID: String, userPublicKey: String, delete: Boolean = true) { val storage = MessagingModuleConfiguration.shared.storage + // Mark the group as inactive + storage.setActive(groupID, false) storage.removeClosedGroupPublicKey(groupPublicKey) // Remove the key pairs storage.removeAllClosedGroupEncryptionKeyPairs(groupPublicKey) - // Mark the group as inactive - storage.setActive(groupID, false) storage.removeMember(groupID, Address.fromSerialized(userPublicKey)) // Notify the PN server PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey) diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/EditClosedGroupActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/groups/EditClosedGroupActivity.kt index 5dd2074ba2..9fee8adafc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/EditClosedGroupActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/EditClosedGroupActivity.kt @@ -302,7 +302,7 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity() { isLoading = true loaderContainer.fadeIn() val promise: Promise = if (!members.contains(Recipient.from(this, Address.fromSerialized(userPublicKey), false))) { - MessageSender.explicitLeave(groupPublicKey!!, true) + MessageSender.explicitLeave(groupPublicKey!!, false) } else { task { if (hasNameChanged) { 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 5fa018b4da..21cc7915ee 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/util/ConfigurationMessageUtilities.kt @@ -236,7 +236,7 @@ object ConfigurationMessageUtilities { GroupInfo.CommunityGroupInfo(baseInfo, if (isPinned) 1 else 0) } - val allLgc = storage.getAllGroups().filter { it.isClosedGroup && it.isActive }.mapNotNull { group -> + val allLgc = storage.getAllGroups(includeInactive = false).filter { it.isClosedGroup }.mapNotNull { group -> val groupAddress = Address.fromSerialized(group.encodedId) val groupPublicKey = GroupUtil.doubleDecodeGroupID(groupAddress.serialize()).toHexString() val recipient = storage.getRecipientSettings(groupAddress) ?: return@mapNotNull null diff --git a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt index 75aebab58b..b870192c67 100644 --- a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt @@ -148,7 +148,7 @@ interface StorageProtocol { fun setExpirationTimer(address: String, duration: Int) // Groups - fun getAllGroups(): List + fun getAllGroups(includeInactive: Boolean): List // Settings fun setProfileSharing(address: Address, value: Boolean) diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt index 5d4e0f8547..5bac4383f5 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt @@ -78,7 +78,7 @@ class BatchMessageReceiveJob( else { // message is control message otherwise return when(message) { is SharedConfigurationMessage -> false - is ClosedGroupControlMessage -> message.kind is ClosedGroupControlMessage.Kind.New + is ClosedGroupControlMessage -> false // message.kind is ClosedGroupControlMessage.Kind.New && !message.isSenderSelf is DataExtractionNotification -> false is MessageRequestResponse -> false is ExpirationTimerUpdate -> false diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt index 30a47ab85b..7e6db3e23d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt @@ -122,7 +122,7 @@ class ConfigurationMessage(var closedGroups: List, var openGroups: val displayName = TextSecurePreferences.getProfileName(context) ?: return null val profilePicture = TextSecurePreferences.getProfilePictureURL(context) val profileKey = ProfileKeyUtil.getProfileKey(context) - val groups = storage.getAllGroups() + val groups = storage.getAllGroups(includeInactive = false) for (group in groups) { if (group.isClosedGroup) { if (!group.members.contains(Address.fromSerialized(storage.getUserPublicKey()!!))) continue diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index ecaab000bf..b99a9aa390 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -426,7 +426,7 @@ object MessageSender { @JvmStatic fun send(message: Message, address: Address) { - val threadID = MessagingModuleConfiguration.shared.storage.getOrCreateThreadIdFor(address) + val threadID = MessagingModuleConfiguration.shared.storage.getThreadId(address) message.threadID = threadID val destination = Destination.from(address) val job = MessageSendJob(message, destination) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index a075ff42f1..0b92c833b8 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -459,7 +459,7 @@ private fun ClosedGroupControlMessage.getPublicKey(): String = kind!!.let { when private fun MessageReceiver.handleNewClosedGroup(message: ClosedGroupControlMessage) { val kind = message.kind!! as? ClosedGroupControlMessage.Kind.New ?: return val recipient = Recipient.from(MessagingModuleConfiguration.shared.context, Address.fromSerialized(message.sender!!), false) - if (!recipient.isApproved && !recipient.isLocalNumber) return + if (!recipient.isApproved && !recipient.isLocalNumber) return Log.e("Loki", "not accepting new closed group from unapproved recipient") val groupPublicKey = kind.publicKey.toByteArray().toHexString() val members = kind.members.map { it.toByteArray().toHexString() } val admins = kind.admins.map { it.toByteArray().toHexString() } @@ -758,12 +758,7 @@ private fun MessageReceiver.handleClosedGroupMemberLeft(message: ClosedGroupCont storage.setZombieMembers(groupID, zombies.plus(senderPublicKey).map { Address.fromSerialized(it) }) } // Notify the user - if (userLeft) { - val threadID = storage.getThreadId(Address.fromSerialized(groupID)) - if (threadID != null) { - storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.QUIT, name, members, admins, threadID, message.sentTimestamp!!) - } - } else { + if (!userLeft) { storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceGroup.Type.QUIT, name, members, admins, message.sentTimestamp!!) } }