|
1 | 1 | package org.wordpress.android.fluxc.network.rest.wpapi.applicationpasswords |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import androidx.annotation.VisibleForTesting |
4 | 5 | import androidx.core.content.edit |
5 | 6 | import org.wordpress.android.fluxc.model.SiteModel |
| 7 | +import org.wordpress.android.fluxc.utils.AppLogWrapper |
6 | 8 | import org.wordpress.android.fluxc.utils.PreferenceUtils |
| 9 | +import org.wordpress.android.util.AppLog |
7 | 10 | import javax.inject.Inject |
| 11 | +import javax.inject.Singleton |
| 12 | +import kotlin.time.Duration.Companion.days |
8 | 13 |
|
9 | | -class JetpackApplicationPasswordsSupport @Inject constructor(context: Context) { |
| 14 | +@Singleton |
| 15 | +class JetpackApplicationPasswordsSupport @Inject constructor( |
| 16 | + context: Context, |
| 17 | + private val appLog: AppLogWrapper |
| 18 | +) { |
10 | 19 | private val fluxCPreferences by lazy { PreferenceUtils.getFluxCPreferences(context) } |
11 | 20 |
|
12 | 21 | 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 |
16 | 39 | } |
17 | 40 |
|
18 | 41 | fun flagAsUnsupported(siteModel: SiteModel) { |
19 | 42 | 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() |
20 | 58 | fluxCPreferences.edit { |
21 | | - putStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, unsupportedSites + siteModel.siteId.toString()) |
| 59 | + putStringSet(UNSUPPORTED_JETPACK_APP_PASSWORDS_SITES, updatedSites) |
22 | 60 | } |
23 | 61 | } |
24 | 62 |
|
25 | 63 | 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" |
27 | 69 | } |
28 | 70 | } |
0 commit comments