Skip to content

Commit e65c4e5

Browse files
Handle clearing flags after a given duration
1 parent c977e88 commit e65c4e5

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
11
package org.wordpress.android.fluxc.network.rest.wpapi.applicationpasswords
22

33
import android.content.Context
4+
import androidx.annotation.VisibleForTesting
45
import androidx.core.content.edit
56
import org.wordpress.android.fluxc.model.SiteModel
7+
import org.wordpress.android.fluxc.utils.AppLogWrapper
68
import org.wordpress.android.fluxc.utils.PreferenceUtils
9+
import org.wordpress.android.util.AppLog
710
import javax.inject.Inject
11+
import javax.inject.Singleton
12+
import kotlin.time.Duration.Companion.days
813

9-
class JetpackApplicationPasswordsSupport @Inject constructor(context: Context) {
14+
@Singleton
15+
class JetpackApplicationPasswordsSupport @Inject constructor(
16+
context: Context,
17+
private val appLog: AppLogWrapper
18+
) {
1019
private val fluxCPreferences by lazy { PreferenceUtils.getFluxCPreferences(context) }
1120

1221
fun supportsAppPasswords(siteModel: SiteModel): Boolean {
13-
return siteModel.isApplicationPasswordsSupported &&
14-
fluxCPreferences.getStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, null)
15-
?.contains(siteModel.siteId.toString()) != true
22+
val siteFlag = fluxCPreferences.getStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, null)
23+
?.firstOrNull { it.startsWith("${siteModel.siteId}:") }
24+
25+
if (siteFlag != null) {
26+
val flagTimestamp = siteFlag.substringAfter(":").toLongOrNull() ?: 0L
27+
if (System.currentTimeMillis() - flagTimestamp < FLAG_REFRESH_DURATION_DAYS.days.inWholeMilliseconds) {
28+
return false
29+
} else {
30+
appLog.d(
31+
AppLog.T.API,
32+
"Clearing Jetpack Application Passwords unsupported flag for site ${siteModel.siteId} after refresh period"
33+
)
34+
clearFlag(siteModel)
35+
}
36+
}
37+
38+
return siteModel.isApplicationPasswordsSupported
1639
}
1740

1841
fun flagAsUnsupported(siteModel: SiteModel) {
1942
val unsupportedSites = fluxCPreferences.getStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, null) ?: setOf()
43+
if (unsupportedSites.any { it.startsWith("${siteModel.siteId}:") }) {
44+
// Already flagged
45+
return
46+
}
47+
fluxCPreferences.edit {
48+
putStringSet(
49+
UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES,
50+
unsupportedSites + "${siteModel.siteId}:${System.currentTimeMillis()}"
51+
)
52+
}
53+
}
54+
55+
private fun clearFlag(siteModel: SiteModel) {
56+
val unsupportedSites = fluxCPreferences.getStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, null) ?: setOf()
57+
val updatedSites = unsupportedSites.filterNot { it.startsWith("${siteModel.siteId}:") }.toSet()
2058
fluxCPreferences.edit {
21-
putStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, unsupportedSites + siteModel.siteId.toString())
59+
putStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, updatedSites)
2260
}
2361
}
2462

2563
companion object {
26-
private const val UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES = "unsupported_jetpack_app_passwords_sites"
64+
@VisibleForTesting
65+
internal const val FLAG_REFRESH_DURATION_DAYS = 7L
66+
67+
@VisibleForTesting
68+
internal const val UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES = "unsupported_jetpack_app_passwords_sites"
2769
}
2870
}

0 commit comments

Comments
 (0)