Skip to content

Commit 45c5b08

Browse files
committed
chore: split processSinglePayment in smaller methods
1 parent e51cfcc commit 45c5b08

File tree

1 file changed

+129
-117
lines changed

1 file changed

+129
-117
lines changed

app/src/main/java/to/bitkit/services/CoreService.kt

Lines changed: 129 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -291,128 +291,140 @@ class ActivityService(
291291
}
292292

293293
private suspend fun processSinglePayment(payment: PaymentDetails, forceUpdate: Boolean) {
294-
try {
295-
val state = when (payment.status) {
296-
PaymentStatus.FAILED -> PaymentState.FAILED
297-
PaymentStatus.PENDING -> PaymentState.PENDING
298-
PaymentStatus.SUCCEEDED -> PaymentState.SUCCEEDED
294+
val state = when (payment.status) {
295+
PaymentStatus.FAILED -> PaymentState.FAILED
296+
PaymentStatus.PENDING -> PaymentState.PENDING
297+
PaymentStatus.SUCCEEDED -> PaymentState.SUCCEEDED
298+
}
299+
300+
when (val kind = payment.kind) {
301+
is PaymentKind.Onchain -> {
302+
processOnchainPayment(kind = kind, payment = payment, forceUpdate = forceUpdate)
299303
}
300304

301-
when (val kind = payment.kind) {
302-
is PaymentKind.Onchain -> {
303-
var isConfirmed = false
304-
var confirmedTimestamp: ULong? = null
305-
306-
val status = kind.status
307-
if (status is ConfirmationStatus.Confirmed) {
308-
isConfirmed = true
309-
confirmedTimestamp = status.timestamp
310-
}
311-
312-
// Ensure confirmTimestamp is at least equal to timestamp when confirmed
313-
val timestamp = payment.latestUpdateTimestamp
314-
315-
if (isConfirmed && confirmedTimestamp != null && confirmedTimestamp < timestamp) {
316-
confirmedTimestamp = timestamp
317-
}
318-
319-
val existingActivity = getActivityById(payment.id)
320-
if (existingActivity != null &&
321-
existingActivity is Activity.Onchain &&
322-
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
323-
) {
324-
return
325-
}
326-
327-
val onChain = if (existingActivity is Activity.Onchain) {
328-
existingActivity.v1.copy(
329-
confirmed = isConfirmed,
330-
confirmTimestamp = confirmedTimestamp,
331-
updatedAt = timestamp,
332-
)
333-
} else {
334-
OnchainActivity(
335-
id = payment.id,
336-
txType = payment.direction.toPaymentType(),
337-
txId = kind.txid,
338-
value = payment.amountSats ?: 0u,
339-
fee = (payment.feePaidMsat ?: 0u) / 1000u,
340-
feeRate = 1u,
341-
address = "Loading...",
342-
confirmed = isConfirmed,
343-
timestamp = timestamp,
344-
isBoosted = false,
345-
isTransfer = false,
346-
doesExist = true,
347-
confirmTimestamp = confirmedTimestamp,
348-
channelId = null,
349-
transferTxId = null,
350-
createdAt = timestamp,
351-
updatedAt = timestamp,
352-
)
353-
}
354-
355-
if (onChain.id in cacheStore.data.first().deletedActivities && !forceUpdate) {
356-
Logger.debug("Activity ${onChain.id} was already deleted, skipping", context = TAG)
357-
return
358-
}
359-
360-
if (existingActivity != null) {
361-
updateActivity(payment.id, Activity.Onchain(onChain))
362-
} else {
363-
upsertActivity(Activity.Onchain(onChain))
364-
}
365-
}
305+
is PaymentKind.Bolt11 -> {
306+
processBolt11(kind = kind, payment = payment, state = state)
307+
}
366308

367-
is PaymentKind.Bolt11 -> {
368-
// Skip pending inbound payments, just means an invoice was created
369-
if (
370-
payment.status == PaymentStatus.PENDING &&
371-
payment.direction == PaymentDirection.INBOUND
372-
) {
373-
return
374-
}
375-
376-
val existingActivity = getActivityById(payment.id)
377-
if (
378-
existingActivity as? Activity.Lightning != null &&
379-
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
380-
) {
381-
return
382-
}
383-
384-
val ln = if (existingActivity is Activity.Lightning) {
385-
existingActivity.v1.copy(
386-
updatedAt = payment.latestUpdateTimestamp,
387-
status = state
388-
)
389-
} else {
390-
LightningActivity(
391-
id = payment.id,
392-
txType = payment.direction.toPaymentType(),
393-
status = state,
394-
value = payment.amountSats ?: 0u,
395-
fee = (payment.feePaidMsat ?: 0u) / 1000u,
396-
invoice = kind.bolt11 ?: "Loading...",
397-
message = kind.description.orEmpty(),
398-
timestamp = payment.latestUpdateTimestamp,
399-
preimage = kind.preimage,
400-
createdAt = payment.latestUpdateTimestamp,
401-
updatedAt = payment.latestUpdateTimestamp,
402-
)
403-
}
309+
else -> Unit // Handle spontaneous payments if needed
310+
}
311+
}
404312

405-
if (getActivityById(payment.id) != null) {
406-
updateActivity(payment.id, Activity.Lightning(ln))
407-
} else {
408-
upsertActivity(Activity.Lightning(ln))
409-
}
410-
}
313+
private suspend fun processBolt11(
314+
kind: PaymentKind.Bolt11,
315+
payment: PaymentDetails,
316+
state: PaymentState,
317+
) {
318+
// Skip pending inbound payments, just means an invoice was created
319+
if (
320+
payment.status == PaymentStatus.PENDING &&
321+
payment.direction == PaymentDirection.INBOUND
322+
) {
323+
return
324+
}
411325

412-
else -> Unit // Handle spontaneous payments if needed
413-
}
414-
} catch (e: Throwable) {
415-
throw e
326+
val existingActivity = getActivityById(payment.id)
327+
if (
328+
existingActivity as? Activity.Lightning != null &&
329+
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
330+
) {
331+
return
332+
}
333+
334+
val ln = if (existingActivity is Activity.Lightning) {
335+
existingActivity.v1.copy(
336+
updatedAt = payment.latestUpdateTimestamp,
337+
status = state
338+
)
339+
} else {
340+
LightningActivity(
341+
id = payment.id,
342+
txType = payment.direction.toPaymentType(),
343+
status = state,
344+
value = payment.amountSats ?: 0u,
345+
fee = (payment.feePaidMsat ?: 0u) / 1000u,
346+
invoice = kind.bolt11 ?: "Loading...",
347+
message = kind.description.orEmpty(),
348+
timestamp = payment.latestUpdateTimestamp,
349+
preimage = kind.preimage,
350+
createdAt = payment.latestUpdateTimestamp,
351+
updatedAt = payment.latestUpdateTimestamp,
352+
)
353+
}
354+
355+
if (getActivityById(payment.id) != null) {
356+
updateActivity(payment.id, Activity.Lightning(ln))
357+
} else {
358+
upsertActivity(Activity.Lightning(ln))
359+
}
360+
}
361+
362+
private suspend fun processOnchainPayment(
363+
kind: PaymentKind.Onchain,
364+
payment: PaymentDetails,
365+
forceUpdate: Boolean,
366+
) {
367+
var isConfirmed = false
368+
var confirmedTimestamp: ULong? = null
369+
370+
val status = kind.status
371+
if (status is ConfirmationStatus.Confirmed) {
372+
isConfirmed = true
373+
confirmedTimestamp = status.timestamp
374+
}
375+
376+
// Ensure confirmTimestamp is at least equal to timestamp when confirmed
377+
val timestamp = payment.latestUpdateTimestamp
378+
379+
if (isConfirmed && confirmedTimestamp != null && confirmedTimestamp < timestamp) {
380+
confirmedTimestamp = timestamp
381+
}
382+
383+
val existingActivity = getActivityById(payment.id)
384+
if (existingActivity != null &&
385+
existingActivity is Activity.Onchain &&
386+
(existingActivity.v1.updatedAt ?: 0u) > payment.latestUpdateTimestamp
387+
) {
388+
return
389+
}
390+
391+
val onChain = if (existingActivity is Activity.Onchain) {
392+
existingActivity.v1.copy(
393+
confirmed = isConfirmed,
394+
confirmTimestamp = confirmedTimestamp,
395+
updatedAt = timestamp,
396+
)
397+
} else {
398+
OnchainActivity(
399+
id = payment.id,
400+
txType = payment.direction.toPaymentType(),
401+
txId = kind.txid,
402+
value = payment.amountSats ?: 0u,
403+
fee = (payment.feePaidMsat ?: 0u) / 1000u,
404+
feeRate = 1u,
405+
address = "Loading...",
406+
confirmed = isConfirmed,
407+
timestamp = timestamp,
408+
isBoosted = false,
409+
isTransfer = false,
410+
doesExist = true,
411+
confirmTimestamp = confirmedTimestamp,
412+
channelId = null,
413+
transferTxId = null,
414+
createdAt = timestamp,
415+
updatedAt = timestamp,
416+
)
417+
}
418+
419+
if (onChain.id in cacheStore.data.first().deletedActivities && !forceUpdate) {
420+
Logger.debug("Activity ${onChain.id} was already deleted, skipping", context = TAG)
421+
return
422+
}
423+
424+
if (existingActivity != null) {
425+
updateActivity(payment.id, Activity.Onchain(onChain))
426+
} else {
427+
upsertActivity(Activity.Onchain(onChain))
416428
}
417429
}
418430

0 commit comments

Comments
 (0)