From 1f8481d287ea94722ad28002b4f47a3deb478320 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Fri, 16 Aug 2024 15:52:09 -0300 Subject: [PATCH] Add check for KEEP_ALIVE job state to allow for re-submission. --- .../securesms/database/InAppPaymentTable.kt | 13 ------------- .../securesms/jobs/InAppPaymentKeepAliveJob.kt | 18 ++++++++++++------ .../jobs/InAppPaymentRecurringContextJob.kt | 2 +- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/InAppPaymentTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/InAppPaymentTable.kt index 9f03330124..ff5af2085f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/InAppPaymentTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/InAppPaymentTable.kt @@ -286,19 +286,6 @@ class InAppPaymentTable(context: Context, databaseHelper: SignalDatabase) : Data .run() } - /** - * Returns whether there are any pending donations in the database. - */ - fun hasPending(type: InAppPaymentType): Boolean { - return readableDatabase.exists(TABLE_NAME) - .where( - "$STATE = ? AND $TYPE = ?", - State.serialize(State.PENDING), - InAppPaymentType.serialize(type) - ) - .run() - } - /** * Retrieves from the database the latest payment of the given type that is either in the PENDING or WAITING_FOR_AUTHORIZATION state. */ diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentKeepAliveJob.kt b/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentKeepAliveJob.kt index b08c200709..c16ef02d13 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentKeepAliveJob.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentKeepAliveJob.kt @@ -47,6 +47,7 @@ class InAppPaymentKeepAliveJob private constructor( private val TIMEOUT = 3.days + const val KEEP_ALIVE = "keep-alive" private const val DATA_TYPE = "type" fun create(type: InAppPaymentSubscriberRecord.Type): Job { @@ -131,11 +132,6 @@ class InAppPaymentKeepAliveJob private constructor( } } - if (SignalDatabase.inAppPayments.hasPending(type.inAppPaymentType)) { - info(type, "Already trying to redeem $type. Exiting.") - return - } - val activeInAppPayment = getActiveInAppPayment(subscriber, subscription) if (activeInAppPayment == null) { warn(type, "Failed to generate active in-app payment. Exiting") @@ -144,7 +140,6 @@ class InAppPaymentKeepAliveJob private constructor( if (activeInAppPayment.state == InAppPaymentTable.State.END) { warn(type, "Active in-app payment is in the END state. Cannot proceed.") - warn(type, "Active in-app payment error state: ${activeInAppPayment.data.error}") warn(type, "Active in-app payment cancel state: ${activeInAppPayment.data.cancellation}") return } @@ -275,6 +270,17 @@ class InAppPaymentKeepAliveJob private constructor( MultiDeviceSubscriptionSyncRequestJob.enqueue() SignalDatabase.inAppPayments.getById(inAppPaymentId) + } else if (current.state == InAppPaymentTable.State.PENDING && current.data.error?.data_ == KEEP_ALIVE) { + info(type, "Found failed keep-alive. Retrying.") + SignalDatabase.inAppPayments.update( + current.copy( + data = current.data.copy( + error = null + ) + ) + ) + + SignalDatabase.inAppPayments.getById(current.id) } else { current } diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentRecurringContextJob.kt b/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentRecurringContextJob.kt index 1c715cfd46..28b5bf81e1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentRecurringContextJob.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/InAppPaymentRecurringContextJob.kt @@ -247,7 +247,7 @@ class InAppPaymentRecurringContextJob private constructor( data = inAppPayment.data.copy( error = InAppPaymentData.Error( type = InAppPaymentData.Error.Type.PAYMENT_PROCESSING, - data_ = "keep-alive" + data_ = InAppPaymentKeepAliveJob.KEEP_ALIVE ) ) )