Fix crash loop when writing invalid currency .

This commit is contained in:
Alex Hart
2024-06-13 18:03:20 -03:00
committed by GitHub
parent 71979b34db
commit cb171092cf
16 changed files with 63 additions and 40 deletions

View File

@@ -25,6 +25,7 @@ import org.thoughtcrime.securesms.database.model.InAppPaymentSubscriberRecord
import org.thoughtcrime.securesms.database.model.databaseprotos.InAppPaymentData
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.whispersystems.signalservice.api.subscriptions.SubscriberId
import java.util.Currency
/**
* A table matching up SubscriptionIds to currency codes and type
@@ -77,7 +78,7 @@ class InAppPaymentSubscriberTable(
* This is a destructive, mutating operation. For setting specific values, prefer the alternative setters available on this table class.
*/
fun insertOrReplace(inAppPaymentSubscriberRecord: InAppPaymentSubscriberRecord) {
Log.i(TAG, "Setting subscriber for currency ${inAppPaymentSubscriberRecord.currencyCode}", Exception(), true)
Log.i(TAG, "Setting subscriber for currency ${inAppPaymentSubscriberRecord.currency.currencyCode}", Exception(), true)
writableDatabase.withinTransaction { db ->
db.insertInto(TABLE_NAME)
@@ -85,7 +86,7 @@ class InAppPaymentSubscriberTable(
.run(conflictStrategy = SQLiteDatabase.CONFLICT_REPLACE)
SignalStore.donationsValues().setSubscriberCurrency(
inAppPaymentSubscriberRecord.currencyCode,
inAppPaymentSubscriberRecord.currency,
inAppPaymentSubscriberRecord.type
)
}
@@ -137,7 +138,7 @@ class InAppPaymentSubscriberTable(
override fun serialize(data: InAppPaymentSubscriberRecord): ContentValues {
return contentValuesOf(
SUBSCRIBER_ID to data.subscriberId.serialize(),
CURRENCY_CODE to data.currencyCode.uppercase(),
CURRENCY_CODE to data.currency.currencyCode.uppercase(),
TYPE to TypeSerializer.serialize(data.type),
REQUIRES_CANCEL to data.requiresCancel,
PAYMENT_METHOD_TYPE to data.paymentMethodType.value
@@ -145,11 +146,13 @@ class InAppPaymentSubscriberTable(
}
override fun deserialize(input: Cursor): InAppPaymentSubscriberRecord {
val type = TypeSerializer.deserialize(input.requireInt(TYPE))
val currencyCode = input.requireNonNullString(CURRENCY_CODE).takeIf { it.isNotEmpty() }
return InAppPaymentSubscriberRecord(
subscriberId = SubscriberId.deserialize(input.requireNonNullString(SUBSCRIBER_ID)),
currencyCode = input.requireNonNullString(CURRENCY_CODE),
type = TypeSerializer.deserialize(input.requireInt(TYPE)),
requiresCancel = input.requireBoolean(REQUIRES_CANCEL),
currency = currencyCode?.let { Currency.getInstance(it) } ?: SignalStore.donationsValues().getSubscriptionCurrency(type),
type = type,
requiresCancel = input.requireBoolean(REQUIRES_CANCEL) || currencyCode.isNullOrBlank(),
paymentMethodType = InAppPaymentData.PaymentMethodType.fromValue(input.requireInt(PAYMENT_METHOD_TYPE)) ?: InAppPaymentData.PaymentMethodType.UNKNOWN
)
}

View File

@@ -8,6 +8,7 @@ package org.thoughtcrime.securesms.database.model
import org.thoughtcrime.securesms.database.InAppPaymentTable
import org.thoughtcrime.securesms.database.model.databaseprotos.InAppPaymentData
import org.whispersystems.signalservice.api.subscriptions.SubscriberId
import java.util.Currency
/**
* Represents a SubscriberId and metadata that can be used for a recurring
@@ -15,7 +16,7 @@ import org.whispersystems.signalservice.api.subscriptions.SubscriberId
*/
data class InAppPaymentSubscriberRecord(
val subscriberId: SubscriberId,
val currencyCode: String,
val currency: Currency,
val type: Type,
val requiresCancel: Boolean,
val paymentMethodType: InAppPaymentData.PaymentMethodType