mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-01-31 11:54:24 +01:00
Notification Plugin: Add Cache-Control header for responses (#902)
* Add Cache-Control header for lnurlpay_info response * Add Cache-Control header for lnurlpay_verify response * Add max age constants
This commit is contained in:
@@ -4,6 +4,10 @@ object Constants {
|
||||
const val SERVICE_TIMEOUT_MS = 3 * 60 * 1000L
|
||||
const val SHUTDOWN_DELAY_MS = 60 * 1000L
|
||||
|
||||
// Cache Control
|
||||
const val CACHE_CONTROL_MAX_AGE_DAY = 60 * 60 * 24
|
||||
const val CACHE_CONTROL_MAX_AGE_WEEK = 60 * 60 * 24 * 7
|
||||
|
||||
// Notification Channels
|
||||
const val NOTIFICATION_CHANNEL_DISMISSIBLE = "DISMISSIBLE"
|
||||
const val NOTIFICATION_CHANNEL_FOREGROUND_SERVICE = "FOREGROUND_SERVICE"
|
||||
|
||||
@@ -21,6 +21,7 @@ interface Job : EventListener {
|
||||
fun replyServer(
|
||||
payload: String,
|
||||
replyURL: String,
|
||||
maxAge: Int = 0,
|
||||
): Boolean {
|
||||
val url = URL(replyURL)
|
||||
val response = payload.toByteArray()
|
||||
@@ -31,6 +32,9 @@ interface Job : EventListener {
|
||||
useCaches = false
|
||||
setRequestProperty("Content-Type", "application/json")
|
||||
setRequestProperty("Content-Length", response.size.toString())
|
||||
if (maxAge > 0) {
|
||||
setRequestProperty("Cache-Control", "max-age=$maxAge")
|
||||
}
|
||||
DataOutputStream(outputStream).use { it.write(response, 0, response.size) }
|
||||
|
||||
return responseCode == 200
|
||||
|
||||
@@ -2,6 +2,7 @@ package breez_sdk_liquid_notification.job
|
||||
|
||||
import android.content.Context
|
||||
import breez_sdk_liquid.BindingLiquidSdk
|
||||
import breez_sdk_liquid_notification.Constants.CACHE_CONTROL_MAX_AGE_DAY
|
||||
import breez_sdk_liquid_notification.Constants.DEFAULT_LNURL_PAY_INFO_NOTIFICATION_TITLE
|
||||
import breez_sdk_liquid_notification.Constants.DEFAULT_LNURL_PAY_METADATA_PLAIN_TEXT
|
||||
import breez_sdk_liquid_notification.Constants.DEFAULT_LNURL_PAY_NOTIFICATION_FAILURE_TITLE
|
||||
@@ -74,7 +75,7 @@ class LnurlPayInfoJob(
|
||||
"[[\"text/plain\",\"$plainTextMetadata\"]]",
|
||||
"payRequest",
|
||||
)
|
||||
val success = replyServer(Json.encodeToString(response), request.replyURL)
|
||||
val success = replyServer(Json.encodeToString(response), request.replyURL, CACHE_CONTROL_MAX_AGE_DAY)
|
||||
notifyChannel(
|
||||
context,
|
||||
NOTIFICATION_CHANNEL_REPLACEABLE,
|
||||
|
||||
@@ -5,6 +5,7 @@ import breez_sdk_liquid.BindingLiquidSdk
|
||||
import breez_sdk_liquid.GetPaymentRequest
|
||||
import breez_sdk_liquid.PaymentDetails
|
||||
import breez_sdk_liquid.PaymentState
|
||||
import breez_sdk_liquid_notification.Constants.CACHE_CONTROL_MAX_AGE_WEEK
|
||||
import breez_sdk_liquid_notification.Constants.DEFAULT_LNURL_PAY_VERIFY_NOTIFICATION_TITLE
|
||||
import breez_sdk_liquid_notification.Constants.DEFAULT_LNURL_PAY_VERIFY_NOTIFICATION_FAILURE_TITLE
|
||||
import breez_sdk_liquid_notification.Constants.LNURL_PAY_VERIFY_NOTIFICATION_TITLE
|
||||
@@ -75,7 +76,8 @@ class LnurlPayVerifyJob(
|
||||
throw InvalidLnurlPayException("Not found")
|
||||
}
|
||||
|
||||
val success = replyServer(Json.encodeToString(response), request.replyURL)
|
||||
val maxAge = if (response.settled) CACHE_CONTROL_MAX_AGE_WEEK else 0
|
||||
val success = replyServer(Json.encodeToString(response), request.replyURL, maxAge)
|
||||
notifyChannel(
|
||||
context,
|
||||
NOTIFICATION_CHANNEL_REPLACEABLE,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import Foundation
|
||||
|
||||
struct Constants {
|
||||
// Cache Control
|
||||
static let CACHE_CONTROL_MAX_AGE_DAY = 60 * 60 * 24
|
||||
static let CACHE_CONTROL_MAX_AGE_WEEK = 60 * 60 * 24 * 7
|
||||
|
||||
// Notification Threads
|
||||
static let NOTIFICATION_THREAD_DISMISSIBLE = "DISMISSIBLE"
|
||||
static let NOTIFICATION_THREAD_REPLACEABLE = "REPLACEABLE"
|
||||
|
||||
@@ -55,7 +55,7 @@ class LnurlPayInfoTask : LnurlPayTask {
|
||||
let plainTextMetadata = ResourceHelper.shared.getString(key: Constants.LNURL_PAY_METADATA_PLAIN_TEXT, fallback: Constants.DEFAULT_LNURL_PAY_METADATA_PLAIN_TEXT)
|
||||
let metadata = "[[\"text/plain\",\"\(plainTextMetadata)\"]]"
|
||||
replyServer(encodable: LnurlInfoResponse(callback: request!.callback_url, maxSendable: maxSendableMsat, minSendable: minSendableMsat, metadata: metadata, tag: "payRequest"),
|
||||
replyURL: request!.reply_url)
|
||||
replyURL: request!.reply_url, maxAge: Constants.CACHE_CONTROL_MAX_AGE_DAY)
|
||||
} catch let e {
|
||||
self.logger.log(tag: TAG, line: "failed to process lnurl: \(e)", level: "ERROR")
|
||||
fail(withError: e.localizedDescription, replyURL: request!.reply_url)
|
||||
|
||||
@@ -64,7 +64,8 @@ class LnurlPayVerifyTask : LnurlPayTask {
|
||||
if response == nil {
|
||||
throw InvalidLnurlPayError.notFound
|
||||
}
|
||||
replyServer(encodable: response, replyURL: request!.reply_url)
|
||||
let maxAge = response!.settled ? Constants.CACHE_CONTROL_MAX_AGE_WEEK : 0
|
||||
replyServer(encodable: response, replyURL: request!.reply_url, maxAge: maxAge)
|
||||
} catch let e {
|
||||
self.logger.log(tag: TAG, line: "failed to process lnurl verify: \(e)", level: "ERROR")
|
||||
fail(withError: e.localizedDescription, replyURL: request!.reply_url)
|
||||
|
||||
@@ -85,12 +85,15 @@ class ReplyableTask : TaskProtocol {
|
||||
displayPushNotification(title: self.failNotificationTitle, logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_REPLACEABLE)
|
||||
}
|
||||
|
||||
func replyServer(encodable: Encodable, replyURL: String) {
|
||||
func replyServer(encodable: Encodable, replyURL: String, maxAge: Int = 0) {
|
||||
guard let serverReplyURL = URL(string: replyURL) else {
|
||||
self.displayPushNotification(title: self.failNotificationTitle, logger: self.logger, threadIdentifier: Constants.NOTIFICATION_THREAD_REPLACEABLE)
|
||||
return
|
||||
}
|
||||
var request = URLRequest(url: serverReplyURL)
|
||||
if maxAge > 0 {
|
||||
request.setValue("max-age=\(maxAge)", forHTTPHeaderField: "Cache-Control")
|
||||
}
|
||||
request.httpMethod = "POST"
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .withoutEscapingSlashes
|
||||
|
||||
Reference in New Issue
Block a user