Simplify again

This commit is contained in:
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 var firebaseSubscriber subscriber
if conf.FirebaseKeyFile != "" { if conf.FirebaseKeyFile != "" {
sender, err := createFirebaseSender(conf) var err error
if err != nil { firebaseSubscriber, err = createFirebaseSubscriber(conf.FirebaseKeyFile, auther)
return nil, err
}
firebaseSubscriber, err = createFirebaseSubscriber(auther, sender)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

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