Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ object SubscriptionsConstants {
const val PRIVACY_PRO_ETLD = "duckduckgo.com"
const val PRIVACY_PRO_PATH = "pro"
}

internal fun String.productIdToBillingPeriod(): String? {
return if (this == SubscriptionsConstants.MONTHLY_PLAN) {
"monthly"
} else if (this == SubscriptionsConstants.YEARLY_PLAN) {
"annual"
} else {
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* 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.
*/

package com.duckduckgo.subscriptions.impl.rmf

import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.remote.messaging.api.AttributeMatcherPlugin
import com.duckduckgo.remote.messaging.api.JsonMatchingAttribute
import com.duckduckgo.remote.messaging.api.JsonToMatchingAttributeMapper
import com.duckduckgo.remote.messaging.api.MatchingAttribute
import com.duckduckgo.subscriptions.impl.SubscriptionsManager
import com.duckduckgo.subscriptions.impl.productIdToBillingPeriod
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import javax.inject.Inject

@ContributesMultibinding(
scope = AppScope::class,
boundType = JsonToMatchingAttributeMapper::class,
)
@ContributesMultibinding(
scope = AppScope::class,
boundType = AttributeMatcherPlugin::class,
)
@SingleInstanceIn(AppScope::class)
class RMFPProBillingPeriodMatchingAttribute @Inject constructor(
private val subscriptionsManager: SubscriptionsManager,
) : JsonToMatchingAttributeMapper, AttributeMatcherPlugin {
override suspend fun evaluate(matchingAttribute: MatchingAttribute): Boolean? {
if (matchingAttribute is PProBillingPeriodMatchingAttribute) {
val productId = subscriptionsManager.getSubscription()?.productId
return productId != null && matchingAttribute.value == productId.productIdToBillingPeriod()
}
return null
}

override fun map(
key: String,
jsonMatchingAttribute: JsonMatchingAttribute,
): MatchingAttribute? {
if (key == PProBillingPeriodMatchingAttribute.KEY) {
val value = jsonMatchingAttribute.value as? String
return value.takeIf { !it.isNullOrEmpty() }?.let {
PProBillingPeriodMatchingAttribute(value = it)
}
}
return null
}
}

internal data class PProBillingPeriodMatchingAttribute(
val value: String,
) : MatchingAttribute {
companion object {
const val KEY = "privacyProBillingPeriod"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ class RMFPProDaysSinceSubscribedMatchingAttribute @Inject constructor(
else -> null
}
}

private fun Any?.toIntOrDefault(default: Int): Int = when {
this == null -> default
this is Double -> this.toInt()
this is Long -> this.toInt()
else -> this as Int
}
}

internal data class PProDaysSinceSubscribedMatchingAttribute(
Expand All @@ -108,3 +101,10 @@ internal data class PProDaysSinceSubscribedMatchingAttribute(
const val KEY = "pproDaysSinceSubscribed"
}
}

internal fun Any?.toIntOrDefault(default: Int): Int = when {
this == null -> default
this is Double -> this.toInt()
this is Long -> this.toInt()
else -> this as Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* 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.
*/

package com.duckduckgo.subscriptions.impl.rmf

import com.duckduckgo.common.utils.CurrentTimeProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.remote.messaging.api.AttributeMatcherPlugin
import com.duckduckgo.remote.messaging.api.JsonMatchingAttribute
import com.duckduckgo.remote.messaging.api.JsonToMatchingAttributeMapper
import com.duckduckgo.remote.messaging.api.MatchingAttribute
import com.duckduckgo.subscriptions.impl.SubscriptionsManager
import com.duckduckgo.subscriptions.impl.repository.isExpired
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import java.util.concurrent.TimeUnit
import javax.inject.Inject

@ContributesMultibinding(
scope = AppScope::class,
boundType = JsonToMatchingAttributeMapper::class,
)
@ContributesMultibinding(
scope = AppScope::class,
boundType = AttributeMatcherPlugin::class,
)
@SingleInstanceIn(AppScope::class)
class RMFPProDaysUntilExpiryRenewalMatchingAttribute @Inject constructor(
private val subscriptionsManager: SubscriptionsManager,
private val currentTimeProvider: CurrentTimeProvider,
) : JsonToMatchingAttributeMapper, AttributeMatcherPlugin {
override suspend fun evaluate(matchingAttribute: MatchingAttribute): Boolean? {
return when (matchingAttribute) {
is PProDaysUntilExpiryRenewalMatchingAttribute -> {
val subscription = subscriptionsManager.getSubscription()
return if (subscription == null || subscription.status.isExpired() ||
matchingAttribute == PProDaysUntilExpiryRenewalMatchingAttribute()
) {
false
} else {
val daysUntilRenewalOrExpiry = daysUntilRenewalOrExpiry(subscription.expiresOrRenewsAt)
if (!matchingAttribute.value.isDefaultValue()) {
matchingAttribute.value == daysUntilRenewalOrExpiry
} else {
(matchingAttribute.min.isDefaultValue() || daysUntilRenewalOrExpiry >= matchingAttribute.min) &&
(matchingAttribute.max.isDefaultValue() || daysUntilRenewalOrExpiry <= matchingAttribute.max)
}
}
}

else -> null
}
}

private fun Int.isDefaultValue(): Boolean {
return this == -1
}

private fun daysUntilRenewalOrExpiry(expiresOrRenewsAt: Long): Int {
return TimeUnit.MILLISECONDS.toDays(expiresOrRenewsAt - currentTimeProvider.currentTimeMillis()).toInt()
}

override fun map(
key: String,
jsonMatchingAttribute: JsonMatchingAttribute,
): MatchingAttribute? {
return when (key) {
PProDaysUntilExpiryRenewalMatchingAttribute.KEY -> {
try {
PProDaysUntilExpiryRenewalMatchingAttribute(
min = jsonMatchingAttribute.min.toIntOrDefault(-1),
max = jsonMatchingAttribute.max.toIntOrDefault(-1),
value = jsonMatchingAttribute.value.toIntOrDefault(-1),
)
} catch (e: Exception) {
null
}
}

else -> null
}
}
}

internal data class PProDaysUntilExpiryRenewalMatchingAttribute(
val min: Int = -1,
val max: Int = -1,
val value: Int = -1,
) : MatchingAttribute {
companion object {
const val KEY = "pproDaysUntilExpiryOrRenewal"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* 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.
*/

package com.duckduckgo.subscriptions.impl.rmf

import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.remote.messaging.api.AttributeMatcherPlugin
import com.duckduckgo.remote.messaging.api.JsonMatchingAttribute
import com.duckduckgo.remote.messaging.api.JsonToMatchingAttributeMapper
import com.duckduckgo.remote.messaging.api.MatchingAttribute
import com.duckduckgo.subscriptions.api.SubscriptionStatus
import com.duckduckgo.subscriptions.api.SubscriptionStatus.AUTO_RENEWABLE
import com.duckduckgo.subscriptions.api.SubscriptionStatus.EXPIRED
import com.duckduckgo.subscriptions.api.SubscriptionStatus.GRACE_PERIOD
import com.duckduckgo.subscriptions.api.SubscriptionStatus.INACTIVE
import com.duckduckgo.subscriptions.api.SubscriptionStatus.NOT_AUTO_RENEWABLE
import com.duckduckgo.subscriptions.impl.SubscriptionsManager
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import javax.inject.Inject

@ContributesMultibinding(
scope = AppScope::class,
boundType = JsonToMatchingAttributeMapper::class,
)
@ContributesMultibinding(
scope = AppScope::class,
boundType = AttributeMatcherPlugin::class,
)
@SingleInstanceIn(AppScope::class)
class RMFPProSubscriptionStatusMatchingAttribute @Inject constructor(
private val subscriptionsManager: SubscriptionsManager,
) : JsonToMatchingAttributeMapper, AttributeMatcherPlugin {
override suspend fun evaluate(matchingAttribute: MatchingAttribute): Boolean? {
if (matchingAttribute is PProSubscriptionStatusMatchingAttribute) {
val status = subscriptionsManager.getSubscription()?.status
return status != null && status.matchesRmfValue(matchingAttribute.value)
}
return null
}

private fun SubscriptionStatus.matchesRmfValue(value: List<String>): Boolean {
value.forEach {
val shouldMatch = when (it) {
STATUS_ACTIVE -> this == AUTO_RENEWABLE || this == NOT_AUTO_RENEWABLE || this == GRACE_PERIOD
STATUS_EXPIRING -> this == NOT_AUTO_RENEWABLE
STATUS_EXPIRED -> this == EXPIRED || this == INACTIVE
else -> false
}

if (shouldMatch) return true
}

return false
}

override fun map(
key: String,
jsonMatchingAttribute: JsonMatchingAttribute,
): MatchingAttribute? {
if (key == PProSubscriptionStatusMatchingAttribute.KEY) {
val value = jsonMatchingAttribute.value as? List<String>
return value.takeUnless { it.isNullOrEmpty() }?.let {
PProSubscriptionStatusMatchingAttribute(value = it)
}
}
return null
}

companion object {
private const val STATUS_ACTIVE = "active"
private const val STATUS_EXPIRING = "expiring"
private const val STATUS_EXPIRED = "expired"
}
}

internal data class PProSubscriptionStatusMatchingAttribute(
val value: List<String>,
) : MatchingAttribute {
companion object {
const val KEY = "pproSubscriptionStatus"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.duckduckgo.common.utils.CurrentTimeProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN
import com.duckduckgo.subscriptions.impl.SubscriptionsManager
import com.duckduckgo.subscriptions.impl.productIdToBillingPeriod
import com.duckduckgo.survey.api.SurveyParameterPlugin
import com.squareup.anvil.annotations.ContributesMultibinding
import java.util.concurrent.TimeUnit
Expand All @@ -42,13 +43,7 @@ class PproBillingParameterPlugin @Inject constructor(

override suspend fun evaluate(): String {
val productId = subscriptionsManager.getSubscription()?.productId
return productId?.let {
if (it.isMonthly()) {
"Monthly"
} else {
"Yearly"
}
}?.lowercase() ?: ""
return productId?.productIdToBillingPeriod() ?: ""
}

private fun String.isMonthly(): Boolean = this == MONTHLY_PLAN
Expand Down
Loading