From ece564988e819f5e6cfeb4ef36896cdfebe1a372 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Wed, 10 Feb 2021 10:56:46 +1100 Subject: [PATCH] Allow self-sending of closed group control messages --- .../Control Messages/ClosedGroupControlMessage.swift | 7 +------ .../Sending & Receiving/MessageReceiver+Handling.swift | 10 +++------- .../MessageSender+ClosedGroups.swift | 1 - .../Sending & Receiving/MessageSender.swift | 8 ++++++-- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/SessionMessagingKit/Messages/Control Messages/ClosedGroupControlMessage.swift b/SessionMessagingKit/Messages/Control Messages/ClosedGroupControlMessage.swift index 4d4723047..a93518ca3 100644 --- a/SessionMessagingKit/Messages/Control Messages/ClosedGroupControlMessage.swift +++ b/SessionMessagingKit/Messages/Control Messages/ClosedGroupControlMessage.swift @@ -11,12 +11,7 @@ public final class ClosedGroupControlMessage : ControlMessage { } } - public override var isSelfSendValid: Bool { - switch kind { - case .new: return false - default: return true - } - } + public override var isSelfSendValid: Bool { true } // MARK: Kind public enum Kind : CustomStringConvertible { diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index a0dba8c78..9561777dd 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -279,6 +279,9 @@ extension MessageReceiver { } else { thread = TSGroupThread.getOrCreateThread(with: group, transaction: transaction) thread.save(with: transaction) + // Notify the user + let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) + infoMessage.save(with: transaction) } // Add the group to the user's set of public keys to poll for Storage.shared.addClosedGroupPublicKey(groupPublicKey, using: transaction) @@ -288,9 +291,6 @@ extension MessageReceiver { Storage.shared.setClosedGroupFormationTimestamp(to: messageSentTimestamp, for: groupPublicKey, using: transaction) // Notify the PN server let _ = PushNotificationAPI.performOperation(.subscribe, for: groupPublicKey, publicKey: getUserHexEncodedPublicKey()) - // Notify the user - let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) - infoMessage.save(with: transaction) } private static func handleClosedGroupEncryptionKeyPair(_ message: ClosedGroupControlMessage, using transaction: Any) { @@ -418,10 +418,6 @@ extension MessageReceiver { let members: Set = didAdminLeave ? [] : Set(group.groupMemberIds).subtracting([ message.sender! ]) // If the admin leaves the group is disbanded let userPublicKey = getUserHexEncodedPublicKey() let isCurrentUserAdmin = group.groupAdminIds.contains(userPublicKey) - // Guard against self-sends - guard message.sender != getUserHexEncodedPublicKey() else { - return SNLog("Ignoring invalid closed group update.") - } // If a regular member left: // • Distribute a new encryption key pair if we're the admin of the group // If the admin left: diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift index 57386e521..19d833a1c 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift @@ -24,7 +24,6 @@ extension MessageSender { // Send a closed group update message to all members individually var promises: [Promise] = [] for member in members { - guard member != userPublicKey else { continue } let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction) thread.save(with: transaction) let closedGroupControlMessageKind = ClosedGroupControlMessage.Kind.new(publicKey: Data(hex: groupPublicKey), name: name, diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 9bd97972f..093885aab 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -137,8 +137,12 @@ public final class MessageSender : NSObject { } // Validate the message guard message.isValid else { handleFailure(with: Error.invalidMessage, using: transaction); return promise } - // Stop here if this is a self-send (unless it's a configuration message or a sync message) - guard !isSelfSend || message is ConfigurationMessage || isSyncMessage else { + // Stop here if this is a self-send, unless it's: + // • a configuration message + // • a sync message + // • a closed group control message of type `new` + let isNewClosedGroupControlMessage = given(message as? ClosedGroupControlMessage) { if case .new = $0.kind { return true } else { return false } } ?? false + guard !isSelfSend || message is ConfigurationMessage || isSyncMessage || isNewClosedGroupControlMessage else { storage.write(with: { transaction in MessageSender.handleSuccessfulMessageSend(message, to: destination, using: transaction) seal.fulfill(())