diff --git a/SignalServiceKit/src/Contacts/ContactsUpdater.m b/SignalServiceKit/src/Contacts/ContactsUpdater.m index 8ac744224..5e638e94e 100644 --- a/SignalServiceKit/src/Contacts/ContactsUpdater.m +++ b/SignalServiceKit/src/Contacts/ContactsUpdater.m @@ -82,8 +82,8 @@ NS_ASSUME_NONNULL_BEGIN failure:(void (^)(NSError *error))failure { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - OWSContactDiscoveryOperation *operation = - [[OWSContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects]; + OWSLegacyContactDiscoveryOperation *operation = + [[OWSLegacyContactDiscoveryOperation alloc] initWithRecipientIdsToLookup:recipientIdsToLookup.allObjects]; NSArray *operationAndDependencies = [operation.dependencies arrayByAddingObject:operation]; [self.contactIntersectionQueue addOperations:operationAndDependencies waitUntilFinished:YES]; diff --git a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift index bbbfb08ce..e7399cb1e 100644 --- a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift +++ b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift @@ -4,10 +4,17 @@ import Foundation -@objc(OWSContactDiscoveryOperation) -class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperationDelegate { +@objc(OWSCDSOperation) +class CDSOperation: OWSOperation { let batchSize = 2048 + static let operationQueue: OperationQueue = { + let queue = OperationQueue() + queue.maxConcurrentOperationCount = 5 + + return queue + }() + let recipientIdsToLookup: [String] @objc @@ -22,8 +29,7 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)") for batchIds in recipientIdsToLookup.chunked(by: batchSize) { - let batchOperation = LegacyContactDiscoveryBatchOperation(recipientIdsToLookup: batchIds) - batchOperation.delegate = self + let batchOperation = CDSBatchOperation(recipientIdsToLookup: batchIds) self.addDependency(batchOperation) } } @@ -35,7 +41,7 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat Logger.debug("\(logTag) in \(#function)") for dependency in self.dependencies { - guard let batchOperation = dependency as? LegacyContactDiscoveryBatchOperation else { + guard let batchOperation = dependency as? CDSBatchOperation else { owsFail("\(self.logTag) in \(#function) unexpected dependency: \(dependency)") continue } @@ -46,23 +52,13 @@ class ContactDiscoveryOperation: OWSOperation, LegacyContactDiscoveryBatchOperat self.reportSuccess() } - // MARK: LegacyContactDiscoveryBatchOperationDelegate - func contactDiscoverBatchOperation(_ contactDiscoverBatchOperation: LegacyContactDiscoveryBatchOperation, didFailWithError error: Error) { - Logger.debug("\(logTag) in \(#function) canceling self and all dependencies.") - - self.dependencies.forEach { $0.cancel() } - self.cancel() - } -} - -protocol LegacyContactDiscoveryBatchOperationDelegate: class { - func contactDiscoverBatchOperation(_ contactDiscoverBatchOperation: LegacyContactDiscoveryBatchOperation, didFailWithError error: Error) } +@objc(OWSLegacyContactDiscoveryOperation) class LegacyContactDiscoveryBatchOperation: OWSOperation { + @objc var registeredRecipientIds: Set - weak var delegate: LegacyContactDiscoveryBatchOperationDelegate? private let recipientIdsToLookup: [String] private var networkManager: TSNetworkManager { @@ -71,6 +67,7 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation { // MARK: Initializers + @objc required init(recipientIdsToLookup: [String]) { self.recipientIdsToLookup = recipientIdsToLookup self.registeredRecipientIds = Set() @@ -134,16 +131,12 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation { // Called at most one time. override func didSucceed() { // Compare against new CDS service - let newCDSBatchOperation = CDSBatchOperation(recipientIdsToLookup: self.recipientIdsToLookup) + let modernCDSOperation = CDSOperation(recipientIdsToLookup: self.recipientIdsToLookup) let cdsFeedbackOperation = CDSFeedbackOperation(legacyRegisteredRecipientIds: self.registeredRecipientIds) - cdsFeedbackOperation.addDependency(newCDSBatchOperation) - - CDSBatchOperation.operationQueue.addOperations([newCDSBatchOperation, cdsFeedbackOperation], waitUntilFinished: false) - } + cdsFeedbackOperation.addDependency(modernCDSOperation) - // Called at most one time. - override func didFail(error: Error) { - self.delegate?.contactDiscoverBatchOperation(self, didFailWithError: error) + let operations = modernCDSOperation.dependencies + [modernCDSOperation, cdsFeedbackOperation] + CDSOperation.operationQueue.addOperations(operations, waitUntilFinished: false) } // MARK: Private Helpers @@ -204,13 +197,6 @@ class CDSBatchOperation: OWSOperation { private let recipientIdsToLookup: [String] private(set) var registeredRecipientIds: Set - static let operationQueue: OperationQueue = { - let queue = OperationQueue() - queue.maxConcurrentOperationCount = 1 - - return queue - }() - private var networkManager: TSNetworkManager { return TSNetworkManager.shared() } @@ -411,7 +397,6 @@ class CDSBatchOperation: OWSOperation { additionalAuthenticatedData: nil, authTag: authTag, key: remoteAttestation.keys.serverKey) else { - throw ContactDiscoveryError.parseError(description: "decryption failed") } @@ -462,7 +447,7 @@ class CDSFeedbackOperation: OWSOperation { return } - guard let cdsOperation = dependencies.first as? CDSBatchOperation else { + guard let cdsOperation = dependencies.first as? CDSOperation else { let error = OWSErrorMakeAssertionError("\(self.logTag) in \(#function) cdsOperation was unexpectedly nil") self.reportError(error) return