From ec736afde44e1e00ae0f93a58756e7bba90d7a64 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Thu, 24 Oct 2024 10:45:05 -0300 Subject: [PATCH] Synchronize backup checks job. --- .../jobs/BackupSubscriptionCheckJob.kt | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/BackupSubscriptionCheckJob.kt b/app/src/main/java/org/thoughtcrime/securesms/jobs/BackupSubscriptionCheckJob.kt index e37cfdfe4a..fc3679edd4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/BackupSubscriptionCheckJob.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/BackupSubscriptionCheckJob.kt @@ -12,6 +12,8 @@ import org.signal.donations.InAppPaymentType import org.thoughtcrime.securesms.backup.v2.MessageBackupTier import org.thoughtcrime.securesms.components.settings.app.subscription.InAppPaymentsRepository import org.thoughtcrime.securesms.components.settings.app.subscription.RecurringInAppPaymentRepository +import org.thoughtcrime.securesms.database.InAppPaymentTable +import org.thoughtcrime.securesms.database.SignalDatabase import org.thoughtcrime.securesms.database.model.InAppPaymentSubscriberRecord import org.thoughtcrime.securesms.dependencies.AppDependencies import org.thoughtcrime.securesms.jobmanager.CoroutineJob @@ -78,40 +80,43 @@ class BackupSubscriptionCheckJob private constructor(parameters: Parameters) : C val purchase: BillingPurchaseResult = AppDependencies.billingApi.queryPurchases() val hasActivePurchase = purchase is BillingPurchaseResult.Success && purchase.isAcknowledged && purchase.isWithinTheLastMonth() - val subscriberId = InAppPaymentsRepository.getSubscriber(InAppPaymentSubscriberRecord.Type.BACKUP) - if (subscriberId == null && hasActivePurchase) { - Log.w(TAG, "User has active Google Play Billing purchase but no subscriber id! User should cancel backup and resubscribe.") - // TODO [message-backups] Set UI flag hint here to launch sheet (designs pending) + synchronized(InAppPaymentSubscriberRecord.Type.BACKUP) { + val subscriberId = InAppPaymentsRepository.getSubscriber(InAppPaymentSubscriberRecord.Type.BACKUP) + if (subscriberId == null && hasActivePurchase) { + Log.w(TAG, "User has active Google Play Billing purchase but no subscriber id! User should cancel backup and resubscribe.") + // TODO [message-backups] Set UI flag hint here to launch sheet (designs pending) + return Result.success() + } + + val tier = SignalStore.backup.backupTier + if (subscriberId == null && tier == MessageBackupTier.PAID) { + Log.w(TAG, "User has no subscriber id but PAID backup tier. User will need to cancel and resubscribe.") + // TODO [message-backups] Set UI flag hint here to launch sheet (designs pending) + return Result.success() + } + + val inAppPayment = SignalDatabase.inAppPayments.getLatestInAppPaymentByType(InAppPaymentType.RECURRING_BACKUP) + val activeSubscription = RecurringInAppPaymentRepository.getActiveSubscriptionSync(InAppPaymentSubscriberRecord.Type.BACKUP).getOrNull() + if (activeSubscription?.isActive == true && tier != MessageBackupTier.PAID && inAppPayment?.state != InAppPaymentTable.State.PENDING) { + Log.w(TAG, "User has an active subscription but no backup tier and no pending redemption.") + // TODO [message-backups] Set UI flag hint here to launch error sheet? + return Result.success() + } + + if (activeSubscription?.isActive != true && tier == MessageBackupTier.PAID) { + Log.w(TAG, "User subscription is inactive or does not exist. User will need to cancel and resubscribe.") + // TODO [message-backups] Set UI hint? + return Result.success() + } + + if (activeSubscription?.isActive != true && hasActivePurchase) { + Log.w(TAG, "User subscription is inactive but user has a recent purchase. User will need to cancel and resubscribe.") + // TODO [message-backups] Set UI hint? + return Result.success() + } + return Result.success() } - - val tier = SignalStore.backup.backupTier - if (subscriberId == null && tier == MessageBackupTier.PAID) { - Log.w(TAG, "User has no subscriber id but PAID backup tier. User will need to cancel and resubscribe.") - // TODO [message-backups] Set UI flag hint here to launch sheet (designs pending) - return Result.success() - } - - val activeSubscription = RecurringInAppPaymentRepository.getActiveSubscriptionSync(InAppPaymentSubscriberRecord.Type.BACKUP).getOrNull() - if (activeSubscription?.isActive == true && tier != MessageBackupTier.PAID) { - Log.w(TAG, "User has an active subscription but no backup tier.") - // TODO [message-backups] Set UI flag hint here to launch error sheet? - return Result.success() - } - - if (activeSubscription?.isActive != true && tier == MessageBackupTier.PAID) { - Log.w(TAG, "User subscription is inactive or does not exist. User will need to cancel and resubscribe.") - // TODO [message-backups] Set UI hint? - return Result.success() - } - - if (activeSubscription?.isActive != true && hasActivePurchase) { - Log.w(TAG, "User subscription is inactive but user has a recent purchase. User will need to cancel and resubscribe.") - // TODO [message-backups] Set UI hint? - return Result.success() - } - - return Result.success() } override fun serialize(): ByteArray? = null