From 422f53c86a8360a0a4575086056c18c98720c99f Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Thu, 1 Jun 2023 08:57:29 +1000 Subject: [PATCH] Updated logic to delete legacy groups when kicked/left --- .../MessageSenderClosedGroupHandler.kt | 2 +- .../sending_receiving/ReceivedMessageHandler.kt | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderClosedGroupHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderClosedGroupHandler.kt index 2c4088c78e..5cfebd5277 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderClosedGroupHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSenderClosedGroupHandler.kt @@ -234,7 +234,7 @@ fun MessageSender.leave(groupPublicKey: String, notifyUser: Boolean = true): Pro storage.insertOutgoingInfoMessage(context, groupID, infoType, name, updatedMembers, admins, threadID, sentTime) } // Remove the group private key and unsubscribe from PNs - MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey) + MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey, true) deferred.resolve(Unit) }.fail { storage.setActive(groupID, true) 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 10e514a702..215a3e936f 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 @@ -692,7 +692,8 @@ private fun MessageReceiver.handleClosedGroupMembersRemoved(message: ClosedGroup val wasCurrentUserRemoved = userPublicKey in removedMembers // Admin should send a MEMBERS_LEFT message but handled here just in case if (didAdminLeave || wasCurrentUserRemoved) { - disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey) + disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey, true) + return } else { storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) }) // Update zombie members @@ -746,7 +747,11 @@ private fun MessageReceiver.handleClosedGroupMemberLeft(message: ClosedGroupCont val updatedMemberList = members - senderPublicKey val userLeft = (userPublicKey == senderPublicKey) if (didAdminLeave || userLeft) { - disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey) + disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey, delete = userLeft) + + if (userLeft) { + return + } } else { storage.updateMembers(groupID, updatedMemberList.map { Address.fromSerialized(it) }) // Update zombie members @@ -774,7 +779,7 @@ private fun isValidGroupUpdate(group: GroupRecord, sentTimestamp: Long, senderPu return true } -fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, groupID: String, userPublicKey: String) { +fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, groupID: String, userPublicKey: String, delete: Boolean) { val storage = MessagingModuleConfiguration.shared.storage storage.removeClosedGroupPublicKey(groupPublicKey) // Remove the key pairs @@ -786,5 +791,11 @@ fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, grou PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey) // Stop polling ClosedGroupPollerV2.shared.stopPolling(groupPublicKey) + + if (delete) { + val threadId = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID)) + storage.cancelPendingMessageSendJobs(threadId) + storage.deleteConversation(threadId) + } } // endregion