|
|
|
@ -3,6 +3,8 @@ import SessionUtilitiesKit
|
|
|
|
|
public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
|
|
|
|
|
public let attachmentID: String
|
|
|
|
|
public let threadID: String
|
|
|
|
|
public let message: Message
|
|
|
|
|
public let messageSendJobID: String
|
|
|
|
|
public var delegate: JobDelegate?
|
|
|
|
|
public var id: String?
|
|
|
|
|
public var failureCount: UInt = 0
|
|
|
|
@ -22,18 +24,24 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N
|
|
|
|
|
public static let maxFailureCount: UInt = 20
|
|
|
|
|
|
|
|
|
|
// MARK: Initialization
|
|
|
|
|
public init(attachmentID: String, threadID: String) {
|
|
|
|
|
public init(attachmentID: String, threadID: String, message: Message, messageSendJobID: String) {
|
|
|
|
|
self.attachmentID = attachmentID
|
|
|
|
|
self.threadID = threadID
|
|
|
|
|
self.message = message
|
|
|
|
|
self.messageSendJobID = messageSendJobID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Coding
|
|
|
|
|
public init?(coder: NSCoder) {
|
|
|
|
|
guard let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String?,
|
|
|
|
|
let threadID = coder.decodeObject(forKey: "threadID") as! String?,
|
|
|
|
|
let message = coder.decodeObject(forKey: "message") as! Message?,
|
|
|
|
|
let messageSendJobID = coder.decodeObject(forKey: "messageSendJobID") as! String?,
|
|
|
|
|
let id = coder.decodeObject(forKey: "id") as! String? else { return nil }
|
|
|
|
|
self.attachmentID = attachmentID
|
|
|
|
|
self.threadID = threadID
|
|
|
|
|
self.message = message
|
|
|
|
|
self.messageSendJobID = messageSendJobID
|
|
|
|
|
self.id = id
|
|
|
|
|
self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0
|
|
|
|
|
}
|
|
|
|
@ -41,6 +49,8 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N
|
|
|
|
|
public func encode(with coder: NSCoder) {
|
|
|
|
|
coder.encode(attachmentID, forKey: "attachmentID")
|
|
|
|
|
coder.encode(threadID, forKey: "threadID")
|
|
|
|
|
coder.encode(message, forKey: "message")
|
|
|
|
|
coder.encode(messageSendJobID, forKey: "messageSendJobID")
|
|
|
|
|
coder.encode(id, forKey: "id")
|
|
|
|
|
coder.encode(failureCount, forKey: "failureCount")
|
|
|
|
|
}
|
|
|
|
@ -69,14 +79,30 @@ public final class AttachmentUploadJob : NSObject, Job, NSCoding { // NSObject/N
|
|
|
|
|
|
|
|
|
|
private func handleSuccess() {
|
|
|
|
|
delegate?.handleJobSucceeded(self)
|
|
|
|
|
Configuration.shared.storage.resumeMessageSendJobIfNeeded(messageSendJobID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func handlePermanentFailure(error: Swift.Error) {
|
|
|
|
|
delegate?.handleJobFailedPermanently(self, with: error)
|
|
|
|
|
failAssociatedMessageSendJob(with: error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func handleFailure(error: Swift.Error) {
|
|
|
|
|
delegate?.handleJobFailed(self, with: error)
|
|
|
|
|
if failureCount + 1 == AttachmentUploadJob.maxFailureCount {
|
|
|
|
|
failAssociatedMessageSendJob(with: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func failAssociatedMessageSendJob(with error: Swift.Error) {
|
|
|
|
|
let storage = Configuration.shared.storage
|
|
|
|
|
let messageSendJob = storage.getMessageSendJob(for: messageSendJobID)
|
|
|
|
|
storage.withAsync({ transaction in // Intentionally capture self
|
|
|
|
|
MessageSender.handleFailedMessageSend(self.message, with: error, using: transaction)
|
|
|
|
|
if let messageSendJob = messageSendJob {
|
|
|
|
|
storage.markJobAsFailed(messageSendJob, using: transaction)
|
|
|
|
|
}
|
|
|
|
|
}, completion: { })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|