diff --git a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift index 6b45e0deb3..d0137738f2 100644 --- a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift +++ b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift @@ -7,7 +7,6 @@ import Foundation @objc(OWSContactDiscoveryOperation) class ContactDiscoveryOperation: OWSOperation { - // TODO verify proper batch size let batchSize = 2048 let recipientIdsToLookup: [String] @@ -49,9 +48,15 @@ class ContactDiscoveryOperation: OWSOperation { class LegacyContactDiscoveryBatchOperation: OWSOperation { - private let recipientIdsToLookup: [String] var registeredRecipientIds: Set + private let recipientIdsToLookup: [String] + private var networkManager: TSNetworkManager { + return TSNetworkManager.shared() + } + + // MARK: Initializers + required init(recipientIdsToLookup: [String]) { self.recipientIdsToLookup = recipientIdsToLookup self.registeredRecipientIds = Set() @@ -61,51 +66,7 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation { Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)") } - private var networkManager: TSNetworkManager { - return TSNetworkManager.shared() - } - - private func parse(response: Any?, phoneNumbersByHashes: [String: String]) throws -> Set { - - guard let responseDict = response as? [String: AnyObject] else { - let responseError: NSError = OWSErrorMakeUnableToProcessServerResponseError() as NSError - responseError.isRetryable = true - - throw responseError - } - - guard let contactDicts = responseDict["contacts"] as? [[String: AnyObject]] else { - let responseError: NSError = OWSErrorMakeUnableToProcessServerResponseError() as NSError - responseError.isRetryable = true - - throw responseError - } - - var registeredRecipientIds: Set = Set() - - for contactDict in contactDicts { - guard let hash = contactDict["token"] as? String, hash.count > 0 else { - owsFail("\(self.logTag) in \(#function) hash was unexpectedly nil") - continue - } - - guard let recipientId = phoneNumbersByHashes[hash], recipientId.count > 0 else { - owsFail("\(self.logTag) in \(#function) recipientId was unexpectedly nil") - continue - } - - guard recipientIdsToLookup.contains(recipientId) else { - owsFail("\(self.logTag) in \(#function) unexpected recipientId") - continue - } - - registeredRecipientIds.insert(recipientId) - } - - return registeredRecipientIds - } - - // MARK: Mandatory overrides + // MARK: OWSOperation Overrides // Called every retry, this is where the bulk of the operation's work should go. override func run() { @@ -148,8 +109,6 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation { }) } - // MARK: Optional Overrides - // Called at most one time. override func didSucceed() { // Compare against new CDS service @@ -160,13 +119,85 @@ class LegacyContactDiscoveryBatchOperation: OWSOperation { CDSFeedbackOperation.operationQueue.addOperations([newCDSBatchOperation, cdsFeedbackOperation], waitUntilFinished: false) } + // MARK: Private Helpers + + private func parse(response: Any?, phoneNumbersByHashes: [String: String]) throws -> Set { + + guard let responseDict = response as? [String: AnyObject] else { + let responseError: NSError = OWSErrorMakeUnableToProcessServerResponseError() as NSError + responseError.isRetryable = true + + throw responseError + } + + guard let contactDicts = responseDict["contacts"] as? [[String: AnyObject]] else { + let responseError: NSError = OWSErrorMakeUnableToProcessServerResponseError() as NSError + responseError.isRetryable = true + + throw responseError + } + + var registeredRecipientIds: Set = Set() + + for contactDict in contactDicts { + guard let hash = contactDict["token"] as? String, hash.count > 0 else { + owsFail("\(self.logTag) in \(#function) hash was unexpectedly nil") + continue + } + + guard let recipientId = phoneNumbersByHashes[hash], recipientId.count > 0 else { + owsFail("\(self.logTag) in \(#function) recipientId was unexpectedly nil") + continue + } + + guard recipientIdsToLookup.contains(recipientId) else { + owsFail("\(self.logTag) in \(#function) unexpected recipientId") + continue + } + + registeredRecipientIds.insert(recipientId) + } + + return registeredRecipientIds + } + +} + +class CDSBatchOperation: OWSOperation { + + private let recipientIdsToLookup: [String] + var registeredRecipientIds: Set + + // MARK: Initializers + + required init(recipientIdsToLookup: [String]) { + self.recipientIdsToLookup = recipientIdsToLookup + self.registeredRecipientIds = Set() + + super.init() + + Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)") + } + + // MARK: OWSOperationOverrides + + // Called every retry, this is where the bulk of the operation's work should go. + override func run() { + Logger.debug("\(logTag) in \(#function)") + + Logger.debug("\(logTag) in \(#function) FAKING intersection (TODO)") + self.registeredRecipientIds = Set(self.recipientIdsToLookup) + self.reportSuccess() + } } class CDSFeedbackOperation: OWSOperation { static let operationQueue = OperationQueue() - let legacyRegisteredRecipientIds: Set + private let legacyRegisteredRecipientIds: Set + + // MARK: Initializers required init(legacyRegisteredRecipientIds: Set) { self.legacyRegisteredRecipientIds = legacyRegisteredRecipientIds @@ -176,7 +207,7 @@ class CDSFeedbackOperation: OWSOperation { Logger.debug("\(logTag) in \(#function)") } - // MARK: Mandatory overrides + // MARK: OWSOperation Overrides // Called every retry, this is where the bulk of the operation's work should go. override func run() { @@ -208,32 +239,6 @@ class CDSFeedbackOperation: OWSOperation { } } -class CDSBatchOperation: OWSOperation { - - private let recipientIdsToLookup: [String] - var registeredRecipientIds: Set - - required init(recipientIdsToLookup: [String]) { - self.recipientIdsToLookup = recipientIdsToLookup - self.registeredRecipientIds = Set() - - super.init() - - Logger.debug("\(logTag) in \(#function) with recipientIdsToLookup: \(recipientIdsToLookup.count)") - } - - // MARK: Mandatory overrides - - // Called every retry, this is where the bulk of the operation's work should go. - override func run() { - Logger.debug("\(logTag) in \(#function)") - - Logger.debug("\(logTag) in \(#function) FAKING intersection (TODO)") - self.registeredRecipientIds = Set(self.recipientIdsToLookup) - self.reportSuccess() - } -} - extension Array { func chunked(by chunkSize: Int) -> [[Element]] { return stride(from: 0, to: self.count, by: chunkSize).map {