diff --git a/Session/Conversations/Views & Modals/DownloadAttachmentModal.swift b/Session/Conversations/Views & Modals/DownloadAttachmentModal.swift index 1e0bf7362..3c7fd293d 100644 --- a/Session/Conversations/Views & Modals/DownloadAttachmentModal.swift +++ b/Session/Conversations/Views & Modals/DownloadAttachmentModal.swift @@ -72,7 +72,7 @@ final class DownloadAttachmentModal : Modal { Storage.shared.setContact(contact, using: transaction) message.touch(with: transaction) }, completion: { - Storage.shared.resumeAttachmentDownloadJobsIfNeeded(for: message.uniqueId!) + Storage.shared.resumeAttachmentDownloadJobsIfNeeded(for: message.uniqueThreadId) }) presentingViewController?.dismiss(animated: true, completion: nil) } diff --git a/SessionMessagingKit/Database/Storage+Jobs.swift b/SessionMessagingKit/Database/Storage+Jobs.swift index 9f1362bd5..fe3f31615 100644 --- a/SessionMessagingKit/Database/Storage+Jobs.swift +++ b/SessionMessagingKit/Database/Storage+Jobs.swift @@ -73,19 +73,19 @@ extension Storage { return result.first } - public func getAttachmentDownloadJobs(for tsMessageID: String) -> [AttachmentDownloadJob] { + public func getAttachmentDownloadJobs(for threadID: String) -> [AttachmentDownloadJob] { var result: [AttachmentDownloadJob] = [] Storage.read { transaction in transaction.enumerateRows(inCollection: AttachmentDownloadJob.collection) { _, object, _, _ in - guard let job = object as? AttachmentDownloadJob, job.tsMessageID == tsMessageID else { return } + guard let job = object as? AttachmentDownloadJob, job.threadID == threadID else { return } result.append(job) } } return result } - public func resumeAttachmentDownloadJobsIfNeeded(for tsMessageID: String) { - let jobs = getAttachmentDownloadJobs(for: tsMessageID) + public func resumeAttachmentDownloadJobsIfNeeded(for threadID: String) { + let jobs = getAttachmentDownloadJobs(for: threadID) jobs.forEach { job in job.delegate = JobQueue.shared job.isDeferred = false diff --git a/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift b/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift index 8953ed17b..7b667193f 100644 --- a/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift +++ b/SessionMessagingKit/Jobs/AttachmentDownloadJob.swift @@ -5,6 +5,7 @@ import SignalCoreKit public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility public let attachmentID: String public let tsMessageID: String + public let threadID: String public var delegate: JobDelegate? public var id: String? public var failureCount: UInt = 0 @@ -27,18 +28,21 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject public static let maxFailureCount: UInt = 20 // MARK: Initialization - public init(attachmentID: String, tsMessageID: String) { + public init(attachmentID: String, tsMessageID: String, threadID: String) { self.attachmentID = attachmentID self.tsMessageID = tsMessageID + self.threadID = threadID } // MARK: Coding public init?(coder: NSCoder) { guard let attachmentID = coder.decodeObject(forKey: "attachmentID") as! String?, let tsMessageID = coder.decodeObject(forKey: "tsIncomingMessageID") as! String?, + let threadID = coder.decodeObject(forKey: "threadID") as! String?, let id = coder.decodeObject(forKey: "id") as! String? else { return nil } self.attachmentID = attachmentID self.tsMessageID = tsMessageID + self.threadID = threadID self.id = id self.failureCount = coder.decodeObject(forKey: "failureCount") as! UInt? ?? 0 self.isDeferred = coder.decodeBool(forKey: "isDeferred") @@ -47,6 +51,7 @@ public final class AttachmentDownloadJob : NSObject, Job, NSCoding { // NSObject public func encode(with coder: NSCoder) { coder.encode(attachmentID, forKey: "attachmentID") coder.encode(tsMessageID, forKey: "tsIncomingMessageID") + coder.encode(threadID, forKey: "threadID") coder.encode(id, forKey: "id") coder.encode(failureCount, forKey: "failureCount") coder.encode(isDeferred, forKey: "isDeferred") diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 69b1a7fae..c1a8e3341 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -281,7 +281,7 @@ extension MessageReceiver { let isContactTrusted = Storage.shared.getContact(with: message.sender!)?.isTrusted ?? false let isGroup = message.groupPublicKey != nil || openGroupID != nil attachmentsToDownload.forEach { attachmentID in - let downloadJob = AttachmentDownloadJob(attachmentID: attachmentID, tsMessageID: tsMessageID) + let downloadJob = AttachmentDownloadJob(attachmentID: attachmentID, tsMessageID: tsMessageID, threadID: threadID) downloadJob.isDeferred = !isContactTrusted && !isGroup if isMainAppAndActive { JobQueue.shared.add(downloadJob, using: transaction)