Skip to content

Commit

Permalink
Adds Privacy Pro Feature Flags (#717)
Browse files Browse the repository at this point in the history
* Privacy Pro Flags

* Fix lint issues

* Add isLaunchedOverride and isLaunchedOverrideStripe

* Add SubscriptionFeatureAvailability

* Add isLaunchedOverride flag

* Fix duplicate

* Make init public

* Protocol made public

* Override disallowed purchase for internal users

* Empty options

* Return empty options

* Empty options

* Add SubscriptionFeatureAvailabilityTests

* Fix wrong flags checked for override

* Swiftlint fixes

---------

Co-authored-by: Michal Smaga <[email protected]>
  • Loading branch information
afterxleep and miasma13 authored Mar 19, 2024
1 parent 6d5ffe5 commit 0c73586
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ let package = Package(
.target(
name: "Subscription",
dependencies: [
"BrowserServicesKit",
"Common",
],
swiftSettings: [
Expand Down Expand Up @@ -508,6 +509,13 @@ let package = Package(
],
plugins: [swiftlintPlugin]
),
.testTarget(
name: "SubscriptionTests",
dependencies: [
"Subscription",
],
plugins: [swiftlintPlugin]
),
],
cxxLanguageStandard: .cxx11
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum PrivacyFeature: String {
case sync
case privacyDashboard
case history
case privacyPro
}

/// An abstraction to be implemented by any "subfeature" of a given `PrivacyConfiguration` feature.
Expand Down Expand Up @@ -112,3 +113,14 @@ public enum AutoconsentSubfeature: String, PrivacySubfeature {

case onByDefault
}

public enum PrivacyProSubfeature: String, Equatable, PrivacySubfeature {
public var parent: PrivacyFeature { .privacyPro }

case isLaunched
case isLaunchedStripe
case allowPurchase
case allowPurchaseStripe
case isLaunchedOverride
case isLaunchedOverrideStripe
}
4 changes: 4 additions & 0 deletions Sources/Subscription/Flows/PurchaseFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public struct SubscriptionOptions: Encodable {
let platform: String
let options: [SubscriptionOption]
let features: [SubscriptionFeature]
public static var empty: SubscriptionOptions {
let features = SubscriptionFeatureName.allCases.map { SubscriptionFeature(name: $0.rawValue) }
return SubscriptionOptions(platform: "macos", options: [], features: features)
}
}

public struct SubscriptionOption: Encodable {
Expand Down
76 changes: 76 additions & 0 deletions Sources/Subscription/SubscriptionFeatureAvailability.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// SubscriptionFeatureAvailability.swift
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import BrowserServicesKit

public protocol SubscriptionFeatureAvailability {
var isFeatureAvailable: Bool { get }
var isSubscriptionPurchaseAllowed: Bool { get }
}

public final class DefaultSubscriptionFeatureAvailability: SubscriptionFeatureAvailability {

private let privacyConfigurationManager: PrivacyConfigurationManaging
private let purchasePlatform: SubscriptionPurchaseEnvironment.Environment

public init(privacyConfigurationManager: PrivacyConfigurationManaging, purchasePlatform: SubscriptionPurchaseEnvironment.Environment) {
self.privacyConfigurationManager = privacyConfigurationManager
self.purchasePlatform = purchasePlatform
}

public var isFeatureAvailable: Bool {
isInternalUser || isSubscriptionLaunched || isSubscriptionLaunchedOverride
}

public var isSubscriptionPurchaseAllowed: Bool {
let isPurchaseAllowed: Bool

switch purchasePlatform {
case .appStore:
isPurchaseAllowed = privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.allowPurchase)
case .stripe:
isPurchaseAllowed = privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.allowPurchaseStripe)
}

return isPurchaseAllowed || isInternalUser
}

// MARK: - Conditions

private var isInternalUser: Bool {
privacyConfigurationManager.internalUserDecider.isInternalUser
}

private var isSubscriptionLaunched: Bool {
switch purchasePlatform {
case .appStore:
privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.isLaunched)
case .stripe:
privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.isLaunchedStripe)
}
}

private var isSubscriptionLaunchedOverride: Bool {
switch purchasePlatform {
case .appStore:
privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.isLaunchedOverride)
case .stripe:
privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.isLaunchedOverrideStripe)
}
}
}
Loading

0 comments on commit 0c73586

Please sign in to comment.