From 26485ba698b48c986a46e5e1ceac5f3182ec4fdd Mon Sep 17 00:00:00 2001 From: Stefan Mitterrutzner Date: Tue, 20 Apr 2021 10:29:26 +0200 Subject: [PATCH] expose oldest keydate when calling iWasExposed --- Sources/DP3TSDK/DP3TSDK.swift | 9 +++++++-- Sources/DP3TSDK/DP3TTracing.swift | 7 ++++++- Sources/DP3TSDK/Models/ExposeeListModel.swift | 6 ++++++ Tests/DP3TSDKTests/DP3TSDKTests.swift | 16 +++++++++++++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Sources/DP3TSDK/DP3TSDK.swift b/Sources/DP3TSDK/DP3TSDK.swift index e31a6b8e..ce033fa2 100644 --- a/Sources/DP3TSDK/DP3TSDK.swift +++ b/Sources/DP3TSDK/DP3TSDK.swift @@ -246,7 +246,7 @@ class DP3TSDK { func iWasExposed(onset: Date, authentication: ExposeeAuthMethod, isFakeRequest: Bool = false, - callback: @escaping (Result) -> Void) { + callback: @escaping (Result) -> Void) { log.trace() if !isFakeRequest, case .infected = state.infectionStatus { @@ -299,6 +299,11 @@ class DP3TSDK { withFederationGateway = nil } + let oldestNonFakeKeyDate = mutableKeys + .filter { $0.fake == 0 } + .map(\.date) + .min() + let model = ExposeeListModel(gaenKeys: mutableKeys, withFederationGateway: withFederationGateway, fake: isFakeRequest) @@ -313,7 +318,7 @@ class DP3TSDK { self.tracer.setEnabled(false, completionHandler: nil) } - callback(.success(())) + callback(.success((.init(oldestKeyDate: oldestNonFakeKeyDate)))) case let .failure(error): callback(.failure(.networkingError(error: error))) } diff --git a/Sources/DP3TSDK/DP3TTracing.swift b/Sources/DP3TSDK/DP3TTracing.swift index c8f0c5bf..56591f41 100644 --- a/Sources/DP3TSDK/DP3TTracing.swift +++ b/Sources/DP3TSDK/DP3TTracing.swift @@ -135,6 +135,11 @@ public enum DP3TTracing { return instance.status } + + public struct IWasExposedResult { + public let oldestKeyDate: Date? + } + /// tell the SDK that the user was exposed /// - Parameters: /// - onset: Start date of the exposure @@ -145,7 +150,7 @@ public enum DP3TTracing { public static func iWasExposed(onset: Date, authentication: ExposeeAuthMethod, isFakeRequest: Bool = false, - callback: @escaping (Result) -> Void) { + callback: @escaping (Result) -> Void) { instancePrecondition() instance.iWasExposed(onset: onset, authentication: authentication, diff --git a/Sources/DP3TSDK/Models/ExposeeListModel.swift b/Sources/DP3TSDK/Models/ExposeeListModel.swift index 221b1bc3..509c483f 100644 --- a/Sources/DP3TSDK/Models/ExposeeListModel.swift +++ b/Sources/DP3TSDK/Models/ExposeeListModel.swift @@ -18,6 +18,12 @@ struct CodableDiagnosisKey: Codable, Equatable { let fake: UInt8 } +extension CodableDiagnosisKey { + var date: Date { + Date(timeIntervalSince1970: TimeInterval(rollingStartNumber) * TimeInterval.minute * 10) + } +} + /// Model of the exposed person struct ExposeeListModel: Encodable { /// Diagnosis keys diff --git a/Tests/DP3TSDKTests/DP3TSDKTests.swift b/Tests/DP3TSDKTests/DP3TSDKTests.swift index d1db2804..b91292b0 100644 --- a/Tests/DP3TSDKTests/DP3TSDKTests.swift +++ b/Tests/DP3TSDKTests/DP3TSDKTests.swift @@ -96,8 +96,18 @@ class DP3TSDKTests: XCTestCase { XCTAssertEqual(sdk.status.infectionStatus, .healthy) let exp = expectation(description: "infected") - keyProvider.keys = [ .init(keyData: Data(count: 16), rollingPeriod: 144, rollingStartNumber: DayDate().period, transmissionRiskLevel: 0, fake: 0) ] + let oldestDate = Date(timeIntervalSinceNow: -.day * 5) + keyProvider.keys = [ + .init(keyData: Data(count: 16), rollingPeriod: 144, rollingStartNumber: DayDate(date: oldestDate.addingTimeInterval(.day)).period, transmissionRiskLevel: 0, fake: 0), + .init(keyData: Data(count: 16), rollingPeriod: 144, rollingStartNumber: DayDate(date: oldestDate).period, transmissionRiskLevel: 0, fake: 0), + .init(keyData: Data(count: 16), rollingPeriod: 144, rollingStartNumber: DayDate(date: oldestDate.addingTimeInterval(.day * 2)).period, transmissionRiskLevel: 0, fake: 0), + ] sdk.iWasExposed(onset: .init(timeIntervalSinceNow: -.day), authentication: .none) { (result) in + if case let Result.success(wrapper) = result { + XCTAssertEqual(wrapper.oldestKeyDate, DayDate(date: oldestDate).dayMin) + } else { + XCTFail() + } exp.fulfill() } wait(for: [exp], timeout: 0.1) @@ -108,8 +118,8 @@ class DP3TSDKTests: XCTestCase { let rollingStartNumbers = Set(model!.gaenKeys.map(\.rollingStartNumber)) XCTAssertEqual(rollingStartNumbers.count, model!.gaenKeys.count) var runningDate: Date? - for key in model!.gaenKeys { - let date = Date(timeIntervalSince1970: Double(key.rollingStartNumber) * 10 * .minute) + for key in model!.gaenKeys.sorted(by: { $0.date > $1.date }) { + let date = key.date guard runningDate != nil else { runningDate = date continue