Correctly handle multiple untrusted attachments

This commit is contained in:
Niels Andriesse 2021-04-08 16:24:54 +10:00
parent d3412be955
commit 209882fb3e
4 changed files with 12 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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