Skip to content

Commit

Permalink
Merge pull request #10 from breez/app_data
Browse files Browse the repository at this point in the history
Add app_data to the webhook query params
  • Loading branch information
roeierez authored Jan 17, 2024
2 parents 3f5bc81 + 501859f commit 308df30
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
18 changes: 16 additions & 2 deletions breezsdk/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package breezsdk

import (
"encoding/json"
"fmt"

"firebase.google.com/go/messaging"
"github.com/breez/notify/config"
"github.com/breez/notify/notify"
Expand All @@ -22,7 +25,8 @@ func createMessageFactory() services.FCMMessageBuilder {
case notify.NOTIFICATION_PAYMENT_RECEIVED,
notify.NOTIFICATION_TX_CONFIRMED,
notify.NOTIFICATION_ADDRESS_TXS_CHANGED,
notify.NOTIFICATION_WEBHOOK_CALLBACK:
notify.NOTIFICATION_LNURLPAY_INFO,
notify.NOTIFICATION_LNURLPAY_INVOICE:

return createPush(notification)
}
Expand All @@ -32,8 +36,18 @@ func createMessageFactory() services.FCMMessageBuilder {
}

func createPush(notification *notify.Notification) (*messaging.Message, error) {
data := notification.Data
data := make(map[string]string)

data["notification_type"] = notification.Template
if notification.AppData != nil {
data["app_data"] = *notification.AppData
}
payload, err := json.Marshal(notification.Data)
if err != nil {
return nil, fmt.Errorf("failed to marshal notification data %v", err)
}
data["notification_payload"] = string(payload)

return &messaging.Message{
Token: notification.TargetIdentifier,
Data: data,
Expand Down
56 changes: 36 additions & 20 deletions http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,57 @@ import (
)

type MobilePushWebHookQuery struct {
Platform string `form:"platform" binding:"required,oneof=ios android"`
Token string `form:"token" binding:"required"`
Platform string `form:"platform" binding:"required,oneof=ios android"`
Token string `form:"token" binding:"required"`
AppData *string `form:"app_data"`
}

type NotificationConvertible interface {
ToNotification(query *MobilePushWebHookQuery) *notify.Notification
}

type WebhookCallbackMessagePayload struct {
Template string `json:"template" binding:"required,eq=webhook_callback_message"`
MessageType string `json:"message_type" binding:"required"`
Data struct {
CallbackURL string `json:"callback_url" binding:"required"`
MessagePayload string `json:"message_payload"`
type LnurlPayInfoPayload struct {
Template string `json:"template" binding:"required,eq=lnurlpay_info"`
Data struct {
CallbackURL string `json:"callback_url" binding:"required"`
ReplyURL string `json:"reply_url" binding:"required"`
} `json:"data"`
}

func (p *WebhookCallbackMessagePayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
func (p *LnurlPayInfoPayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return &notify.Notification{
Template: p.Template,
DisplayMessage: p.GenerateDisplayMessage(),
DisplayMessage: "Receiving payment",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]string{
"callback_url": p.Data.CallbackURL,
"message_payload": p.Data.MessagePayload,
"callback_url": p.Data.CallbackURL,
"reply_url": p.Data.ReplyURL,
},
}
}

func (p *WebhookCallbackMessagePayload) GenerateDisplayMessage() string {
switch p.MessageType {
case "lnulrpay_info":
return "Receiving payment"
case "lnurlpay_invoice":
return "Invoice requested"
type LnurlPayInvoicePayload struct {
Template string `json:"template" binding:"required,eq=lnurlpay_invoice"`
Data struct {
Amount string `json:"amount" binding:"required"`
ReplyURL string `json:"reply_url" binding:"required"`
} `json:"data"`
}

func (p *LnurlPayInvoicePayload) ToNotification(query *MobilePushWebHookQuery) *notify.Notification {
return &notify.Notification{
Template: p.Template,
DisplayMessage: "Invoice requested",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]string{
"amount": p.Data.Amount,
"reply_url": p.Data.ReplyURL,
},
}
return ""
}

type PaymentReceivedPayload struct {
Expand All @@ -67,6 +80,7 @@ func (p *PaymentReceivedPayload) ToNotification(query *MobilePushWebHookQuery) *
DisplayMessage: "Incoming payment",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]string{"payment_hash": p.Data.PaymentHash},
}
}
Expand All @@ -84,6 +98,7 @@ func (p *TxConfirmedPayload) ToNotification(query *MobilePushWebHookQuery) *noti
DisplayMessage: "Transaction confirmed",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]string{"tx_id": p.Data.TxID},
}
}
Expand All @@ -101,6 +116,7 @@ func (p *AddressTxsChangedPayload) ToNotification(query *MobilePushWebHookQuery)
DisplayMessage: "Address transactions changed",
Type: query.Platform,
TargetIdentifier: query.Token,
AppData: query.AppData,
Data: map[string]string{"address": p.Data.Address},
}
}
Expand Down Expand Up @@ -132,7 +148,7 @@ func addWebHookRouter(r *gin.RouterGroup, notifier *notify.Notifier) {
}

// Find a matching notification payload
payloads := []NotificationConvertible{&PaymentReceivedPayload{}, &TxConfirmedPayload{}, &AddressTxsChangedPayload{}, &WebhookCallbackMessagePayload{}}
payloads := []NotificationConvertible{&PaymentReceivedPayload{}, &TxConfirmedPayload{}, &AddressTxsChangedPayload{}, &LnurlPayInfoPayload{}, &LnurlPayInvoicePayload{}}
var validPayload NotificationConvertible
for _, p := range payloads {
if err := c.ShouldBindBodyWith(p, binding.JSON); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion http/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (
)

func TestPaymentReceivedHook(t *testing.T) {
testAppData := "testdata"
query := MobilePushWebHookQuery{
Platform: "android",
Token: "1234",
AppData: &testAppData,
}

paymentReceivedPayload := PaymentReceivedPayload{
Template: notify.NOTIFICATION_PAYMENT_RECEIVED,
Data: struct {
Expand All @@ -26,12 +29,13 @@ func TestPaymentReceivedHook(t *testing.T) {
PaymentHash: "1234",
},
}

body, err := json.Marshal(paymentReceivedPayload)
if err != nil {
t.Fatalf("failed to marshal notification %v", err)
}
expected := paymentReceivedPayload.ToNotification(&query)
testValidNotification(t, "/api/v1/notify?platform=android&token=1234", body, expected)
testValidNotification(t, "/api/v1/notify?platform=android&token=1234&app_data=testdata", body, expected)
}

func TestTxConfirmedHook(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const (
NOTIFICATION_PAYMENT_RECEIVED = "payment_received"
NOTIFICATION_TX_CONFIRMED = "tx_confirmed"
NOTIFICATION_ADDRESS_TXS_CHANGED = "address_txs_changed"
NOTIFICATION_WEBHOOK_CALLBACK = "webhook_callback_message"
NOTIFICATION_LNURLPAY_INFO = "lnurlpay_info"
NOTIFICATION_LNURLPAY_INVOICE = "lnurlpay_invoice"
)

var (
Expand All @@ -25,6 +26,7 @@ type Notification struct {
DisplayMessage string
Type string
TargetIdentifier string
AppData *string
Data map[string]string
}

Expand Down

0 comments on commit 308df30

Please sign in to comment.