2020-11-09 00:58:47 +01:00
|
|
|
import SessionUtilitiesKit
|
2020-11-07 23:00:10 +01:00
|
|
|
|
2020-11-10 05:48:47 +01:00
|
|
|
@objc(SNMessageSendJob)
|
2020-11-07 23:00:10 +01:00
|
|
|
public final class MessageSendJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
|
2020-11-24 04:10:32 +01:00
|
|
|
public let message: Message
|
2020-11-24 10:09:23 +01:00
|
|
|
public let destination: Message.Destination
|
|
|
|
public var delegate: JobDelegate?
|
2020-11-17 06:23:13 +01:00
|
|
|
public var id: String?
|
2020-11-08 03:12:38 +01:00
|
|
|
public var failureCount: UInt = 0
|
2020-11-07 23:00:10 +01:00
|
|
|
|
|
|
|
// MARK: Settings
|
2020-11-18 04:27:30 +01:00
|
|
|
public class var collection: String { return "MessageSendJobCollection" }
|
2020-11-23 05:08:01 +01:00
|
|
|
public static let maxFailureCount: UInt = 10
|
2020-11-07 23:00:10 +01:00
|
|
|
|
|
|
|
// MARK: Initialization
|
2020-11-10 05:48:47 +01:00
|
|
|
@objc public convenience init(message: Message, publicKey: String) { self.init(message: message, destination: .contact(publicKey: publicKey)) }
|
|
|
|
@objc public convenience init(message: Message, groupPublicKey: String) { self.init(message: message, destination: .closedGroup(groupPublicKey: groupPublicKey)) }
|
|
|
|
|
2020-11-12 06:02:21 +01:00
|
|
|
public init(message: Message, destination: Message.Destination) {
|
2020-11-07 23:00:10 +01:00
|
|
|
self.message = message
|
|
|
|
self.destination = destination
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Coding
|
|
|
|
public init?(coder: NSCoder) {
|
|
|
|
guard let message = coder.decodeObject(forKey: "message") as! Message?,
|
2020-11-17 06:23:13 +01:00
|
|
|
var rawDestination = coder.decodeObject(forKey: "destination") as! String?,
|
|
|
|
let id = coder.decodeObject(forKey: "id") as! String? else { return nil }
|
2020-11-07 23:00:10 +01:00
|
|
|
self.message = message
|
2020-11-08 06:31:48 +01:00
|
|
|
if rawDestination.removePrefix("contact(") {
|
|
|
|
guard rawDestination.removeSuffix(")") else { return nil }
|
|
|
|
let publicKey = rawDestination
|
|
|
|
destination = .contact(publicKey: publicKey)
|
2020-11-29 23:28:32 +01:00
|
|
|
} else if rawDestination.removePrefix("closedGroup(") {
|
2020-11-08 06:31:48 +01:00
|
|
|
guard rawDestination.removeSuffix(")") else { return nil }
|
|
|
|
let groupPublicKey = rawDestination
|
|
|
|
destination = .closedGroup(groupPublicKey: groupPublicKey)
|
2020-11-29 23:28:32 +01:00
|
|
|
} else if rawDestination.removePrefix("openGroup(") {
|
2020-11-08 06:31:48 +01:00
|
|
|
guard rawDestination.removeSuffix(")") else { return nil }
|
|
|
|
let components = rawDestination.split(separator: ",").map { String($0).trimmingCharacters(in: .whitespacesAndNewlines) }
|
|
|
|
guard components.count == 2, let channel = UInt64(components[0]) else { return nil }
|
|
|
|
let server = components[1]
|
|
|
|
destination = .openGroup(channel: channel, server: server)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-17 06:23:13 +01:00
|
|
|
self.id = id
|
2020-11-07 23:00:10 +01:00
|
|
|
self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
public func encode(with coder: NSCoder) {
|
|
|
|
coder.encode(message, forKey: "message")
|
2020-11-08 06:31:48 +01:00
|
|
|
switch destination {
|
|
|
|
case .contact(let publicKey): coder.encode("contact(\(publicKey))", forKey: "destination")
|
|
|
|
case .closedGroup(let groupPublicKey): coder.encode("closedGroup(\(groupPublicKey))", forKey: "destination")
|
2020-11-30 06:10:58 +01:00
|
|
|
case .openGroup(let channel, let server): coder.encode("openGroup(\(channel), \(server))", forKey: "destination")
|
2020-11-08 06:31:48 +01:00
|
|
|
}
|
2020-11-24 10:09:23 +01:00
|
|
|
coder.encode(id, forKey: "id")
|
2020-11-07 23:00:10 +01:00
|
|
|
coder.encode(failureCount, forKey: "failureCount")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Running
|
|
|
|
public func execute() {
|
2020-12-02 06:25:16 +01:00
|
|
|
let storage = SNMessagingKitConfiguration.shared.storage
|
2020-11-23 05:08:01 +01:00
|
|
|
if let message = message as? VisibleMessage {
|
2020-11-27 00:29:27 +01:00
|
|
|
guard TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) != nil else { return } // The message has been deleted
|
2020-11-23 05:08:01 +01:00
|
|
|
let attachments = message.attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0) }
|
|
|
|
let attachmentsToUpload = attachments.filter { !$0.isUploaded }
|
|
|
|
attachmentsToUpload.forEach { attachment in
|
|
|
|
if storage.getAttachmentUploadJob(for: attachment.uniqueId!) != nil {
|
|
|
|
// Wait for it to finish
|
|
|
|
} else {
|
2020-11-26 23:07:24 +01:00
|
|
|
let job = AttachmentUploadJob(attachmentID: attachment.uniqueId!, threadID: message.threadID!, message: message, messageSendJobID: id!)
|
2020-11-23 05:08:01 +01:00
|
|
|
storage.withAsync({ transaction in
|
|
|
|
JobQueue.shared.add(job, using: transaction)
|
|
|
|
}, completion: { })
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 23:07:24 +01:00
|
|
|
if !attachmentsToUpload.isEmpty { return } // Wait for all attachments to upload before continuing
|
2020-11-23 05:08:01 +01:00
|
|
|
}
|
|
|
|
storage.withAsync({ transaction in // Intentionally capture self
|
2020-11-24 10:09:23 +01:00
|
|
|
MessageSender.send(self.message, to: self.destination, using: transaction).done(on: DispatchQueue.global(qos: .userInitiated)) {
|
|
|
|
self.handleSuccess()
|
|
|
|
}.catch(on: DispatchQueue.global(qos: .userInitiated)) { error in
|
|
|
|
SNLog("Couldn't send message due to error: \(error).")
|
|
|
|
if let error = error as? MessageSender.Error, !error.isRetryable {
|
|
|
|
self.handlePermanentFailure(error: error)
|
|
|
|
} else {
|
|
|
|
self.handleFailure(error: error)
|
2020-11-07 23:00:10 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 06:23:13 +01:00
|
|
|
}, completion: { })
|
2020-11-07 23:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private func handleSuccess() {
|
2020-11-08 03:12:38 +01:00
|
|
|
delegate?.handleJobSucceeded(self)
|
2020-11-07 23:00:10 +01:00
|
|
|
}
|
2020-11-23 05:46:53 +01:00
|
|
|
|
|
|
|
private func handlePermanentFailure(error: Error) {
|
|
|
|
delegate?.handleJobFailedPermanently(self, with: error)
|
|
|
|
}
|
2020-11-07 23:00:10 +01:00
|
|
|
|
|
|
|
private func handleFailure(error: Error) {
|
2020-11-27 00:29:27 +01:00
|
|
|
SNLog("Failed to send \(type(of: message)).")
|
|
|
|
if let message = message as? VisibleMessage {
|
|
|
|
guard TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) != nil else { return } // The message has been deleted
|
|
|
|
}
|
2020-11-08 03:12:38 +01:00
|
|
|
delegate?.handleJobFailed(self, with: error)
|
2020-11-07 23:00:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 06:31:48 +01:00
|
|
|
// MARK: Convenience
|
|
|
|
private extension String {
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
mutating func removePrefix<T : StringProtocol>(_ prefix: T) -> Bool {
|
|
|
|
guard hasPrefix(prefix) else { return false }
|
|
|
|
removeFirst(prefix.count)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
mutating func removeSuffix<T : StringProtocol>(_ suffix: T) -> Bool {
|
|
|
|
guard hasSuffix(suffix) else { return false }
|
|
|
|
removeLast(suffix.count)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|