AppExpiry should read app build time from AppVersion

This commit is contained in:
Evan Hahn 2023-04-19 09:42:20 -05:00 committed by GitHub
parent 95bcaf3dab
commit f06e6c4fe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 26 deletions

View File

@ -136,8 +136,7 @@ public class AppExpiry: NSObject {
if let newExpirationDate = newExpirationDate {
// Ignore any expiration date that is later than when the app expires by default.
guard newExpirationDate < Self.defaultExpirationDate else { return }
guard newExpirationDate < AppVersion.shared.defaultExpirationDate else { return }
updateExpirationState(ExpirationState(mode: .atDate, expirationDate: newExpirationDate))
} else {
updateExpirationState(ExpirationState(mode: .default))
@ -151,7 +150,7 @@ public class AppExpiry: NSObject {
let state = expirationState.get()
switch state.mode {
case .default:
return Self.defaultExpirationDate
return AppVersion.shared.defaultExpirationDate
case .atDate:
guard let expirationDate = state.expirationDate else {
owsFailDebug("Missing expiration date, expiring immediately")
@ -167,27 +166,10 @@ public class AppExpiry: NSObject {
public var isExpired: Bool {
return expirationDate < Date()
}
// MARK: - Build Time
private static let defaultExpirationDate: Date = {
guard let buildTime = loadBuildTime() else {
owsAssert(OWSIsTestableBuild(), "Production builds should always expire.")
Logger.debug("No build timestamp, assuming app never expires.")
return .distantFuture
}
// By default, we expire 90 days after the app was compiled.
return buildTime.addingTimeInterval(90 * kDayInterval)
}()
private static func loadBuildTime() -> Date? {
guard
let buildDetails = Bundle.main.app.object(forInfoDictionaryKey: "BuildDetails") as? [String: Any],
let buildTimestamp = buildDetails["Timestamp"] as? TimeInterval
else {
return nil
}
return Date(timeIntervalSince1970: buildTimestamp)
}
}
// MARK: - Build time
fileprivate extension AppVersion {
var defaultExpirationDate: Date { buildDate.addingTimeInterval(90 * kDayInterval) }
}

View File

@ -95,6 +95,8 @@ public class AppVersion {
}
}
public let buildDate: Date
// MARK: - Setup
private init(bundle: Bundle, userDefaults: UserDefaults) {
@ -102,6 +104,18 @@ public class AppVersion {
self.currentAppBuildVersion = bundle.string(forInfoDictionaryKey: "CFBundleVersion")
self.currentAppVersion4 = bundle.string(forInfoDictionaryKey: "OWSBundleVersion4")
if
let rawBuildDetails = bundle.app.object(forInfoDictionaryKey: "BuildDetails"),
let buildDetails = rawBuildDetails as? [String: Any],
let buildTimestamp = buildDetails["Timestamp"] as? TimeInterval {
self.buildDate = Date(timeIntervalSince1970: buildTimestamp)
} else {
#if !TESTABLE_BUILD
owsFailBeta("Expected a build date to be defined. Assuming build date is right now")
#endif
self.buildDate = Date()
}
self.userDefaults = userDefaults
}