Skip to content

Commit

Permalink
simplify notification payload structure
Browse files Browse the repository at this point in the history
  • Loading branch information
roeierez committed Jan 16, 2024
1 parent a017c8f commit b38cb79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
15 changes: 13 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,11 +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
47 changes: 29 additions & 18 deletions http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,48 @@ 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 "lnurlpay_info":
return "Receiving payment"
case "lnurlpay_invoice":
return "Invoice requested"
type LnurlPayInvoicePayload struct {
Template string `json:"template" binding:"required,eq=lnurlpay_info"`
Data struct {
Amount string `json:"amount" binding:"required"`
ServerReplyURL 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.ServerReplyURL,
},
}
return ""
}

type PaymentReceivedPayload struct {
Expand Down Expand Up @@ -137,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
3 changes: 2 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 Down

0 comments on commit b38cb79

Please sign in to comment.