|
|
@ -54,11 +54,6 @@ public enum MessageReceiver {
|
|
|
|
// Parse the envelope
|
|
|
|
// Parse the envelope
|
|
|
|
let envelope = try SNProtoEnvelope.parseData(data)
|
|
|
|
let envelope = try SNProtoEnvelope.parseData(data)
|
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
|
|
|
// If the message failed to process the first time around we retry it later (if the error is retryable). In this case the timestamp
|
|
|
|
|
|
|
|
// will already be in the database but we don't want to treat the message as a duplicate. The isRetry flag is a simple workaround
|
|
|
|
|
|
|
|
// for this issue.
|
|
|
|
|
|
|
|
guard !Set(storage.getReceivedMessageTimestamps(using: transaction)).contains(envelope.timestamp) || isRetry else { throw Error.duplicateMessage }
|
|
|
|
|
|
|
|
storage.addReceivedMessageTimestamp(envelope.timestamp, using: transaction)
|
|
|
|
|
|
|
|
// Decrypt the contents
|
|
|
|
// Decrypt the contents
|
|
|
|
guard let ciphertext = envelope.content else { throw Error.noData }
|
|
|
|
guard let ciphertext = envelope.content else { throw Error.noData }
|
|
|
|
var plaintext: Data!
|
|
|
|
var plaintext: Data!
|
|
|
@ -159,6 +154,19 @@ public enum MessageReceiver {
|
|
|
|
guard isValid else {
|
|
|
|
guard isValid else {
|
|
|
|
throw Error.invalidMessage
|
|
|
|
throw Error.invalidMessage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the message failed to process the first time around we retry it later (if the error is retryable). In this case the timestamp
|
|
|
|
|
|
|
|
// will already be in the database but we don't want to treat the message as a duplicate. The isRetry flag is a simple workaround
|
|
|
|
|
|
|
|
// for this issue.
|
|
|
|
|
|
|
|
if let message = message as? ClosedGroupControlMessage, case .new = message.kind {
|
|
|
|
|
|
|
|
// Allow duplicates in this case to avoid the following situation:
|
|
|
|
|
|
|
|
// • The app performed a background poll or received a push notification
|
|
|
|
|
|
|
|
// • This method was invoked and the received message timestamps table was updated
|
|
|
|
|
|
|
|
// • Processing wasn't finished
|
|
|
|
|
|
|
|
// • The user doesn't see the new closed group
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
guard !Set(storage.getReceivedMessageTimestamps(using: transaction)).contains(envelope.timestamp) || isRetry else { throw Error.duplicateMessage }
|
|
|
|
|
|
|
|
storage.addReceivedMessageTimestamp(envelope.timestamp, using: transaction)
|
|
|
|
|
|
|
|
}
|
|
|
|
// Return
|
|
|
|
// Return
|
|
|
|
return (message, proto)
|
|
|
|
return (message, proto)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|