-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWakeNodeWorker.kt
More file actions
282 lines (250 loc) · 11.6 KB
/
WakeNodeWorker.kt
File metadata and controls
282 lines (250 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package to.bitkit.fcm
import android.content.Context
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import androidx.work.workDataOf
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withTimeout
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonObject
import org.lightningdevkit.ldknode.Event
import to.bitkit.App
import to.bitkit.R
import to.bitkit.data.CacheStore
import to.bitkit.data.SettingsStore
import to.bitkit.di.json
import to.bitkit.ext.amountOnClose
import to.bitkit.ext.toUserMessage
import to.bitkit.models.BITCOIN_SYMBOL
import to.bitkit.models.BlocktankNotificationType
import to.bitkit.models.msatCeilOf
import to.bitkit.models.BlocktankNotificationType.cjitPaymentArrived
import to.bitkit.models.BlocktankNotificationType.incomingHtlc
import to.bitkit.models.BlocktankNotificationType.mutualClose
import to.bitkit.models.BlocktankNotificationType.orderPaymentConfirmed
import to.bitkit.models.BlocktankNotificationType.wakeToTimeout
import to.bitkit.models.NewTransactionSheetDetails
import to.bitkit.models.NewTransactionSheetDirection
import to.bitkit.models.NewTransactionSheetType
import to.bitkit.models.NotificationDetails
import to.bitkit.repositories.ActivityRepo
import to.bitkit.repositories.BlocktankRepo
import to.bitkit.repositories.LightningRepo
import to.bitkit.ui.pushNotification
import to.bitkit.utils.Logger
import to.bitkit.utils.measured
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
@Suppress("LongParameterList")
@HiltWorker
class WakeNodeWorker @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted private val workerParams: WorkerParameters,
private val lightningRepo: LightningRepo,
private val blocktankRepo: BlocktankRepo,
private val activityRepo: ActivityRepo,
private val settingsStore: SettingsStore,
private val cacheStore: CacheStore,
) : CoroutineWorker(appContext, workerParams) {
private var bestAttemptContent: NotificationDetails? = null
private var notificationType: BlocktankNotificationType? = null
private var notificationPayload: JsonObject? = null
private val timeout = 2.minutes
private val deliverSignal = CompletableDeferred<Unit>()
override suspend fun doWork(): Result {
Logger.debug("Node wakeup from notification…", context = TAG)
notificationType = workerParams.inputData.getString("type")?.let { BlocktankNotificationType.valueOf(it) }
notificationPayload = workerParams.inputData.getString("payload")?.let {
runCatching { json.parseToJsonElement(it).jsonObject }.getOrNull()
}
Logger.debug("notification type: $notificationType", context = TAG)
Logger.debug("notification payload: $notificationPayload", context = TAG)
if (notificationType == null) {
Logger.warn("Notification type is null, proceeding with node wake", context = TAG)
}
return runCatching {
measured(label = "doWork", context = TAG) {
lightningRepo.start(
walletIndex = 0,
timeout = timeout,
eventHandler = { event -> handleLdkEvent(event) }
)
// Once node is started, handle the manual channel opening if needed
if (notificationType == orderPaymentConfirmed) {
val orderId = (notificationPayload?.get("orderId") as? JsonPrimitive)?.contentOrNull
if (orderId == null) {
Logger.error("Missing orderId", context = TAG)
} else {
Logger.info("Open channel request for order $orderId", context = TAG)
blocktankRepo.openChannel(orderId).onFailure {
Logger.error("Failed to open channel", it, context = TAG)
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__channel_open_failed_title),
body = it.message ?: appContext.getString(R.string.common__error_body),
)
deliver()
}
}
}
}
// Stops node on timeout & avoids notification replay by OS
withTimeout(timeout) { deliverSignal.await() }
}
.fold(
onSuccess = { Result.success() },
onFailure = { e ->
if (e is CancellationException) {
Logger.debug("Work cancelled", context = TAG)
return@fold Result.failure(workDataOf("Reason" to "Cancelled"))
}
val reason = e.message ?: appContext.getString(R.string.common__error_body)
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__lightning_error_title),
body = reason,
)
Logger.error("Lightning error", e, context = TAG)
deliver()
Result.failure(workDataOf("Reason" to reason))
}
)
}
/**
* Listens for LDK events and delivers the notification if the event matches the notification type.
* @param event The LDK event to check.
*/
private suspend fun handleLdkEvent(event: Event) {
val showDetails = settingsStore.data.first().showNotificationDetails
val hiddenBody = appContext.getString(R.string.notification__received__body_hidden)
when (event) {
is Event.PaymentReceived -> onPaymentReceived(event, showDetails, hiddenBody)
is Event.ChannelPending -> {
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__channel_opened_title),
body = appContext.getString(R.string.notification__channel_pending_body),
)
// Don't deliver, give a chance for channelReady event to update the content if it's a turbo channel
}
is Event.ChannelReady -> onChannelReady(event, showDetails, hiddenBody)
is Event.ChannelClosed -> onChannelClosed(event)
is Event.PaymentFailed -> {
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__payment_failed_title),
body = "⚡ ${event.reason.toUserMessage(appContext)}",
)
if (notificationType == wakeToTimeout) {
deliver()
}
}
else -> Unit
}
}
private suspend fun onChannelClosed(event: Event.ChannelClosed) {
bestAttemptContent = when (notificationType) {
mutualClose -> NotificationDetails(
title = appContext.getString(R.string.notification__channel_closed__title),
body = appContext.getString(R.string.notification__channel_closed__mutual_body),
)
orderPaymentConfirmed -> NotificationDetails(
title = appContext.getString(R.string.notification__channel_open_bg_failed_title),
body = appContext.getString(R.string.notification__please_try_again_body),
)
else -> NotificationDetails(
title = appContext.getString(R.string.notification__channel_closed__title),
body = appContext.getString(R.string.notification__channel_closed__reason_body, event.reason),
)
}
deliver()
}
private suspend fun onPaymentReceived(
event: Event.PaymentReceived,
showDetails: Boolean,
hiddenBody: String,
) {
val sats = msatCeilOf(event.amountMsat)
// Save for UI to pick up
cacheStore.setBackgroundReceive(
NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
paymentHashOrTxId = event.paymentHash,
sats = sats.toLong(),
)
)
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else hiddenBody
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__received__title),
body = content,
)
if (notificationType == incomingHtlc) {
deliver()
}
}
private suspend fun onChannelReady(
event: Event.ChannelReady,
showDetails: Boolean,
hiddenBody: String,
) {
val viaNewChannel = appContext.getString(R.string.notification__received__body_channel)
if (notificationType == cjitPaymentArrived) {
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__received__title),
body = viaNewChannel,
)
lightningRepo.getChannels()?.find { it.channelId == event.channelId }?.let { channel ->
val sats = channel.amountOnClose
val content = if (showDetails) "$BITCOIN_SYMBOL $sats" else hiddenBody
bestAttemptContent = NotificationDetails(
title = content,
body = viaNewChannel,
)
val cjitEntry = channel.let { blocktankRepo.getCjitEntry(it) }
if (cjitEntry != null) {
// Save for UI to pick up
cacheStore.setBackgroundReceive(
NewTransactionSheetDetails(
type = NewTransactionSheetType.LIGHTNING,
direction = NewTransactionSheetDirection.RECEIVED,
sats = sats.toLong(),
)
)
activityRepo.insertActivityFromCjit(cjitEntry = cjitEntry, channel = channel)
}
}
} else if (notificationType == orderPaymentConfirmed) {
bestAttemptContent = NotificationDetails(
title = appContext.getString(R.string.notification__channel_opened_title),
body = appContext.getString(R.string.notification__channel_ready_body),
)
}
deliver()
}
private suspend fun deliver() {
// Send notification first
bestAttemptContent?.run {
appContext.pushNotification(title, body)
Logger.info("Delivered notification", context = TAG)
}
// Delay briefly to allow app to come to foreground if user clicked notification
delay(1.seconds)
// Only stop node if app is not in foreground
// LightningNodeService will keep node running in background when notifications are enabled
if (App.currentActivity?.value == null) {
Logger.debug("App in background, stopping node after notification delivery", context = TAG)
lightningRepo.stop()
} else {
Logger.debug("App in foreground, keeping node running", context = TAG)
}
deliverSignal.complete(Unit)
}
companion object {
private const val TAG = "WakeNodeWorker"
}
}