Handle open group message ID

This commit is contained in:
Niels Andriesse 2020-11-23 15:58:48 +11:00
parent 64cf19f461
commit ac1bbb3de1
3 changed files with 20 additions and 8 deletions

View File

@ -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

View File

@ -38,10 +38,11 @@ public final class MessageSender : NSObject {
}
internal static func sendToSnodeDestination(_ destination: Message.Destination, message: Message, using transaction: Any) -> Promise<Void> {
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<Void> {
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 }
}

View File

@ -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)
}