Skip to content

Commit

Permalink
expose oldest keydate when calling iWasExposed
Browse files Browse the repository at this point in the history
  • Loading branch information
stmitt committed Apr 20, 2021
1 parent 0765799 commit 26485ba
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
9 changes: 7 additions & 2 deletions Sources/DP3TSDK/DP3TSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class DP3TSDK {
func iWasExposed(onset: Date,
authentication: ExposeeAuthMethod,
isFakeRequest: Bool = false,
callback: @escaping (Result<Void, DP3TTracingError>) -> Void) {
callback: @escaping (Result<DP3TTracing.IWasExposedResult, DP3TTracingError>) -> Void) {
log.trace()
if !isFakeRequest,
case .infected = state.infectionStatus {
Expand Down Expand Up @@ -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)
Expand All @@ -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)))
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/DP3TSDK/DP3TTracing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -145,7 +150,7 @@ public enum DP3TTracing {
public static func iWasExposed(onset: Date,
authentication: ExposeeAuthMethod,
isFakeRequest: Bool = false,
callback: @escaping (Result<Void, DP3TTracingError>) -> Void) {
callback: @escaping (Result<IWasExposedResult, DP3TTracingError>) -> Void) {
instancePrecondition()
instance.iWasExposed(onset: onset,
authentication: authentication,
Expand Down
6 changes: 6 additions & 0 deletions Sources/DP3TSDK/Models/ExposeeListModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions Tests/DP3TSDKTests/DP3TSDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 26485ba

Please sign in to comment.