From ac1bbb3de1feaf2413d40789b7e99555eeb3c1c2 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 23 Nov 2020 15:58:48 +1100 Subject: [PATCH] Handle open group message ID --- .../Sending & Receiving/MessageReceiver.swift | 7 +++++-- .../Sending & Receiving/MessageSender.swift | 18 ++++++++++++------ .../MessageSenderDelegate.swift | 3 +++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index ee1a48c3a..bbd0dcb88 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -10,11 +10,11 @@ internal enum MessageReceiver { case noData case senderBlocked case noThread + case selfSend // Shared sender keys case invalidGroupPublicKey case noGroupPrivateKey case sharedSecretGenerationFailed - case selfSend internal var isRetryable: Bool { switch self { @@ -42,6 +42,7 @@ internal enum MessageReceiver { } internal static func parse(_ data: Data, messageServerID: UInt64?, using transaction: Any) throws -> (Message, SNProtoContent) { + let userPublicKey = Configuration.shared.storage.getUserPublicKey() // Parse the envelope let envelope = try SNProtoEnvelope.parseData(data) // Decrypt the contents @@ -57,6 +58,8 @@ internal enum MessageReceiver { } // Don't process the envelope any further if the sender is blocked guard !Configuration.shared.messageReceiverDelegate.isBlocked(sender) else { throw Error.senderBlocked } + // Ignore self sends + guard sender != userPublicKey else { throw Error.selfSend } // Parse the proto let proto: SNProtoContent do { @@ -76,7 +79,7 @@ internal enum MessageReceiver { }() if let message = message { message.sender = sender - message.recipient = Configuration.shared.storage.getUserPublicKey() + message.recipient = userPublicKey message.sentTimestamp = envelope.timestamp message.receivedTimestamp = NSDate.millisecondTimestamp() message.groupPublicKey = groupPublicKey diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 0c7b3e57b..04c3c36f1 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -38,10 +38,11 @@ public final class MessageSender : NSObject { } internal static func sendToSnodeDestination(_ destination: Message.Destination, message: Message, using transaction: Any) -> Promise { + let storage = Configuration.shared.storage if message.sentTimestamp == nil { // Visible messages will already have the sent timestamp set message.sentTimestamp = NSDate.millisecondTimestamp() } - message.sender = Configuration.shared.storage.getUserPublicKey() + message.sender = storage.getUserPublicKey() switch destination { case .contact(let publicKey): message.recipient = publicKey case .closedGroup(let groupPublicKey): message.recipient = groupPublicKey @@ -137,7 +138,6 @@ public final class MessageSender : NSObject { seal.reject(error) } let _ = promise.done(on: DispatchQueue.main) { - let storage = Configuration.shared.storage storage.withAsync({ transaction in Configuration.shared.messageSenderDelegate.handleSuccessfulMessageSend(message, using: transaction) }, completion: { }) @@ -150,7 +150,7 @@ public final class MessageSender : NSObject { }, completion: { }) } let _ = promise.catch(on: DispatchQueue.main) { _ in - Configuration.shared.storage.withAsync({ transaction in + storage.withAsync({ transaction in Configuration.shared.messageSenderDelegate.handleFailedMessageSend(message, using: transaction) }, completion: { }) if case .contact(_) = destination { @@ -161,6 +161,7 @@ public final class MessageSender : NSObject { } internal static func sendToOpenGroupDestination(_ destination: Message.Destination, message: Message, using transaction: Any) -> Promise { + let storage = Configuration.shared.storage message.sentTimestamp = NSDate.millisecondTimestamp() switch destination { case .contact(_): preconditionFailure() @@ -177,11 +178,16 @@ public final class MessageSender : NSObject { guard let message = message as? VisibleMessage, let openGroupMessage = OpenGroupMessage.from(message, for: server) else { return Promise(error: Error.invalidMessage) } let promise = OpenGroupAPI.sendMessage(openGroupMessage, to: channel, on: server) - let _ = promise.done(on: DispatchQueue.global(qos: .userInitiated)) { _ in - // TODO: Save server message ID + let _ = promise.done(on: DispatchQueue.global(qos: .userInitiated)) { openGroupMessage in + message.openGroupServerMessageID = openGroupMessage.serverID + storage.withAsync({ transaction in + Configuration.shared.messageSenderDelegate.handleSuccessfulMessageSend(message, using: transaction) + }, completion: { }) } promise.catch(on: DispatchQueue.global(qos: .userInitiated)) { _ in - // TODO: Handle failure + storage.withAsync({ transaction in + Configuration.shared.messageSenderDelegate.handleFailedMessageSend(message, using: transaction) + }, completion: { }) } return promise.map { _ in } } diff --git a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSenderDelegate.swift b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSenderDelegate.swift index aa26c37a6..c08bcbfe1 100644 --- a/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSenderDelegate.swift +++ b/SignalUtilitiesKit/Messaging/Sending & Receiving/MessageSenderDelegate.swift @@ -5,6 +5,9 @@ public final class MessageSenderDelegate : SessionMessagingKit.MessageSenderDele public func handleSuccessfulMessageSend(_ message: Message, using transaction: Any) { guard let tsMessage = TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) else { return } + if let openGroupServerMessageID = message.openGroupServerMessageID { + tsMessage.openGroupServerMessageID = openGroupServerMessageID + } tsMessage.update(withSentRecipient: message.recipient!, wasSentByUD: true, transaction: transaction as! YapDatabaseReadWriteTransaction) }