From 75248b5dc759486c32995d7f7b858d9a1fbf3f81 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 18 Jul 2018 22:22:27 -0600 Subject: [PATCH] Stub out feedback operation // FREEBIE --- .../OWSContactDiscoveryOperation.swift | 137 ++++++++++++------ 1 file changed, 92 insertions(+), 45 deletions(-) diff --git a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift index 58c741d35..6b45e0deb 100644 --- a/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift +++ b/SignalServiceKit/src/Contacts/OWSContactDiscoveryOperation.swift @@ -4,20 +4,11 @@ import Foundation -extension Array { - func chunked(by chunkSize: Int) -> [[Element]] { - return stride(from: 0, to: self.count, by: chunkSize).map { - Array(self[$0.. Error? { - return super.checkForPreconditionError() - } - - // Called at most one time. - override func didSucceed() { - super.didSucceed() - } - - // Called at most one time, once retry is no longer possible. - override func didFail(error: Error) { - super.didFail(error: error) - } } -class OWSContactDiscoveryBatchOperation: OWSOperation { +class LegacyContactDiscoveryBatchOperation: OWSOperation { private let recipientIdsToLookup: [String] var registeredRecipientIds: Set @@ -131,6 +106,7 @@ class OWSContactDiscoveryBatchOperation: OWSOperation { } // 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)") @@ -156,11 +132,6 @@ class OWSContactDiscoveryBatchOperation: OWSOperation { } }, failure: { (task, error) in - if (!IsNSErrorNetworkFailure(error)) { - // FIXME not accessible in swift for some reason. -// OWSProdError(OWSAnalyticsEvents.contactsErrorContactsIntersectionFailed) - } - guard let response = task.response as? HTTPURLResponse else { let responseError: NSError = OWSErrorMakeUnableToProcessServerResponseError() as NSError responseError.isRetryable = true @@ -179,18 +150,94 @@ class OWSContactDiscoveryBatchOperation: OWSOperation { // MARK: Optional Overrides - // Called one time only - override func checkForPreconditionError() -> Error? { - return super.checkForPreconditionError() - } - // Called at most one time. override func didSucceed() { - super.didSucceed() + // Compare against new CDS service + let newCDSBatchOperation = CDSBatchOperation(recipientIdsToLookup: self.recipientIdsToLookup) + let cdsFeedbackOperation = CDSFeedbackOperation(legacyRegisteredRecipientIds: self.registeredRecipientIds) + cdsFeedbackOperation.addDependency(newCDSBatchOperation) + + CDSFeedbackOperation.operationQueue.addOperations([newCDSBatchOperation, cdsFeedbackOperation], waitUntilFinished: false) + } + +} + +class CDSFeedbackOperation: OWSOperation { + + static let operationQueue = OperationQueue() + + let legacyRegisteredRecipientIds: Set + + required init(legacyRegisteredRecipientIds: Set) { + self.legacyRegisteredRecipientIds = legacyRegisteredRecipientIds + + super.init() + + Logger.debug("\(logTag) in \(#function)") + } + + // MARK: Mandatory overrides + + // Called every retry, this is where the bulk of the operation's work should go. + override func run() { + guard let cdsOperation = dependencies.first as? CDSBatchOperation else { + let error = OWSErrorMakeAssertionError("\(self.logTag) in \(#function) cdsOperation was unexpectedly nil") + self.reportError(error) + return + } + + let cdsRegisteredRecipientIds = cdsOperation.registeredRecipientIds + + if cdsRegisteredRecipientIds == legacyRegisteredRecipientIds { + Logger.debug("\(logTag) in \(#function) TODO: PUT /v1/directory/feedback/ok") + } else { + Logger.debug("\(logTag) in \(#function) TODO: PUT /v1/directory/feedback/mismatch") + } + + self.reportSuccess() } - // Called at most one time, once retry is no longer possible. override func didFail(error: Error) { - super.didFail(error: error) + // dependency failed. + // Depending on error, PUT one of: + // /v1/directory/feedback/server-error: + // /v1/directory/feedback/client-error: + // /v1/directory/feedback/attestation-error: + // /v1/directory/feedback/unexpected-error: + Logger.debug("\(logTag) in \(#function) TODO: PUT /v1/directory/feedback/*-error") + } +} + +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 { + Array(self[$0..