Avoid crash when not connected to the network.

This commit is contained in:
Alex Hart
2025-07-08 15:51:32 -03:00
parent 6d58e89c18
commit e6ca41c0f9
4 changed files with 48 additions and 47 deletions

View File

@@ -1615,29 +1615,44 @@ object BackupRepository {
}
suspend fun getBackupsType(tier: MessageBackupTier): MessageBackupsType? {
return when (tier) {
val result = when (tier) {
MessageBackupTier.FREE -> getFreeType()
MessageBackupTier.PAID -> getPaidType()
}
return if (result is NetworkResult.Success) {
result.result
} else {
null
}
}
@WorkerThread
fun getBackupLevelConfiguration(): SubscriptionsConfiguration.BackupLevelConfiguration? {
val config = getSubscriptionsConfiguration()
return config.backupConfiguration.backupLevelConfigurationMap[SubscriptionsConfiguration.BACKUPS_LEVEL]
fun getBackupLevelConfiguration(): NetworkResult<SubscriptionsConfiguration.BackupLevelConfiguration> {
return AppDependencies.donationsApi
.getDonationsConfiguration(Locale.getDefault())
.then {
val config = it.backupConfiguration.backupLevelConfigurationMap[SubscriptionsConfiguration.BACKUPS_LEVEL]
if (config != null) {
NetworkResult.Success(config)
} else {
NetworkResult.StatusCodeError(NonSuccessfulResponseCodeException(404))
}
}
}
@WorkerThread
private fun getFreeType(): MessageBackupsType.Free {
val config = getSubscriptionsConfiguration()
return MessageBackupsType.Free(
mediaRetentionDays = config.backupConfiguration.freeTierMediaDays
)
private fun getFreeType(): NetworkResult<MessageBackupsType.Free> {
return AppDependencies.donationsApi
.getDonationsConfiguration(Locale.getDefault())
.map {
MessageBackupsType.Free(
mediaRetentionDays = it.backupConfiguration.freeTierMediaDays
)
}
}
suspend fun getPaidType(): MessageBackupsType.Paid? {
suspend fun getPaidType(): NetworkResult<MessageBackupsType.Paid> {
val productPrice: FiatMoney? = if (SignalStore.backup.backupTierInternalOverride == MessageBackupTier.PAID) {
Log.d(TAG, "Accessing price via mock subscription.")
RecurringInAppPaymentRepository.getActiveSubscriptionSync(InAppPaymentSubscriberRecord.Type.BACKUP).getOrNull()?.activeSubscription?.let {
@@ -1650,37 +1665,17 @@ object BackupRepository {
if (productPrice == null) {
Log.w(TAG, "No pricing available. Exiting.")
return null
return NetworkResult.StatusCodeError(NonSuccessfulResponseCodeException(404))
}
val backupLevelConfiguration = getBackupLevelConfiguration() ?: return null
return MessageBackupsType.Paid(
pricePerMonth = productPrice,
storageAllowanceBytes = backupLevelConfiguration.storageAllowanceBytes,
mediaTtl = backupLevelConfiguration.mediaTtlDays.days
)
}
@WorkerThread
private fun getSubscriptionsConfiguration(): SubscriptionsConfiguration {
val serviceResponse = AppDependencies
.donationsService
.getDonationsConfiguration(Locale.getDefault())
if (serviceResponse.result.isEmpty) {
if (serviceResponse.applicationError.isPresent) {
throw serviceResponse.applicationError.get()
return getBackupLevelConfiguration()
.map {
MessageBackupsType.Paid(
pricePerMonth = productPrice,
storageAllowanceBytes = it.storageAllowanceBytes,
mediaTtl = it.mediaTtlDays.days
)
}
if (serviceResponse.executionError.isPresent) {
throw serviceResponse.executionError.get()
}
error("Unhandled error occurred while downloading configuration.")
}
return serviceResponse.result.get()
}
/**

View File

@@ -194,7 +194,12 @@ object BackupStateRepository {
private suspend fun getFreeBackupState(): BackupState {
val type = withContext(Dispatchers.IO) {
BackupRepository.getBackupsType(MessageBackupTier.FREE) as MessageBackupsType.Free
BackupRepository.getBackupsType(MessageBackupTier.FREE) as MessageBackupsType.Free?
}
if (type == null) {
Log.w(TAG, "Failed to load FREE type. Possible network error.")
return BackupState.Error
}
val backupState = if (SignalStore.backup.areBackupsEnabled) {
@@ -213,7 +218,7 @@ object BackupStateRepository {
* @return A paid type, or null if we were unable to get the backup level configuration.
*/
private fun buildPaidTypeFromSubscription(subscription: ActiveSubscription.Subscription): MessageBackupsType.Paid? {
val config = BackupRepository.getBackupLevelConfiguration() ?: return null
val config = BackupRepository.getBackupLevelConfiguration().successOrThrow()
val price = FiatMoney.fromSignalNetworkAmount(subscription.amount, Currency.getInstance(subscription.currency))
return MessageBackupsType.Paid(
@@ -229,7 +234,7 @@ object BackupStateRepository {
* @return A paid type, or null if we were unable to get the backup level configuration.
*/
private fun buildPaidTypeFromInAppPayment(inAppPayment: InAppPaymentTable.InAppPayment): MessageBackupsType.Paid? {
val config = BackupRepository.getBackupLevelConfiguration() ?: return null
val config = BackupRepository.getBackupLevelConfiguration().successOrThrow()
val price = inAppPayment.data.amount!!.toFiatMoney()
return MessageBackupsType.Paid(
@@ -246,7 +251,7 @@ object BackupStateRepository {
* @return A paid type, or null if we were unable to get the backup level configuration.
*/
private fun buildPaidTypeWithoutPricing(): MessageBackupsType? {
val config = BackupRepository.getBackupLevelConfiguration() ?: return null
val config = BackupRepository.getBackupLevelConfiguration().successOrThrow()
return MessageBackupsType.Paid(
pricePerMonth = FiatMoney(BigDecimal.ZERO, Currency.getInstance(Locale.getDefault())),

View File

@@ -176,7 +176,7 @@ class CopyAttachmentToArchiveJob private constructor(private val attachmentId: A
private fun getServerQuota(): ByteSize? {
return runBlocking {
BackupRepository.getPaidType()?.storageAllowanceBytes?.bytes
BackupRepository.getPaidType().successOrThrow()?.storageAllowanceBytes?.bytes
}
}

View File

@@ -27,6 +27,7 @@ import org.thoughtcrime.securesms.keyvalue.Completed
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.keyvalue.Skipped
import org.thoughtcrime.securesms.registrationv3.data.QuickRegistrationRepository
import org.whispersystems.signalservice.api.NetworkResult
import org.whispersystems.signalservice.api.provisioning.RestoreMethod
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
@@ -84,9 +85,9 @@ class RemoteRestoreViewModel(isOnlyRestoreOption: Boolean) : ViewModel() {
viewModelScope.launch(Dispatchers.IO) {
val config = BackupRepository.getBackupLevelConfiguration()
if (config != null) {
if (config is NetworkResult.Success) {
store.update {
it.copy(backupMediaTTL = config.mediaTtlDays.days)
it.copy(backupMediaTTL = config.result.mediaTtlDays.days)
}
}
}