Simplify again

pull/114/head
Philipp Heckel 2022-01-31 20:12:47 -05:00
parent c2f31b9c9f
commit b1bbbf0103
2 changed files with 60 additions and 63 deletions

View File

@ -142,11 +142,8 @@ func New(conf *Config) (*Server, error) {
}
var firebaseSubscriber subscriber
if conf.FirebaseKeyFile != "" {
sender, err := createFirebaseSender(conf)
if err != nil {
return nil, err
}
firebaseSubscriber, err = createFirebaseSubscriber(auther, sender)
var err error
firebaseSubscriber, err = createFirebaseSubscriber(conf.FirebaseKeyFile, auther)
if err != nil {
return nil, err
}

View File

@ -10,10 +10,8 @@ import (
"strings"
)
type firebaseSender func(m *messaging.Message) error
func createFirebaseSender(conf *Config) (firebaseSender, error) {
fb, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(conf.FirebaseKeyFile))
func createFirebaseSubscriber(credentialsFile string, auther auth.Auther) (subscriber, error) {
fb, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(credentialsFile))
if err != nil {
return nil, err
}
@ -21,14 +19,17 @@ func createFirebaseSender(conf *Config) (firebaseSender, error) {
if err != nil {
return nil, err
}
return func(m *messaging.Message) error {
_, err := msg.Send(context.Background(), m)
return func(m *message) error {
fbm, err := toFirebaseMessage(m, auther)
if err != nil {
return err
}
_, err = msg.Send(context.Background(), fbm)
return err
}, nil
}
func createFirebaseSubscriber(auther auth.Auther, sender firebaseSender) (subscriber, error) {
return func(m *message) error {
func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, error) {
var data map[string]string // Mostly matches https://ntfy.sh/docs/subscribe/api/#json-message-format
switch m.Event {
case keepaliveEvent, openEvent:
@ -80,10 +81,9 @@ func createFirebaseSubscriber(auther auth.Auther, sender firebaseSender) (subscr
Priority: "high",
}
}
return sender(maybeTruncateFCMMessage(&messaging.Message{
return maybeTruncateFCMMessage(&messaging.Message{
Topic: m.Topic,
Data: data,
Android: androidConfig,
}))
}, nil
}), nil
}