Simplify again
This commit is contained in:
parent
c2f31b9c9f
commit
b1bbbf0103
2 changed files with 60 additions and 63 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,69 +19,71 @@ 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:
|
data = map[string]string{
|
||||||
|
"id": m.ID,
|
||||||
|
"time": fmt.Sprintf("%d", m.Time),
|
||||||
|
"event": m.Event,
|
||||||
|
"topic": m.Topic,
|
||||||
|
}
|
||||||
|
case messageEvent:
|
||||||
|
allowForward := true
|
||||||
|
if auther != nil {
|
||||||
|
allowForward = auther.Authorize(nil, m.Topic, auth.PermissionRead) == nil
|
||||||
|
}
|
||||||
|
if allowForward {
|
||||||
|
data = map[string]string{
|
||||||
|
"id": m.ID,
|
||||||
|
"time": fmt.Sprintf("%d", m.Time),
|
||||||
|
"event": m.Event,
|
||||||
|
"topic": m.Topic,
|
||||||
|
"priority": fmt.Sprintf("%d", m.Priority),
|
||||||
|
"tags": strings.Join(m.Tags, ","),
|
||||||
|
"click": m.Click,
|
||||||
|
"title": m.Title,
|
||||||
|
"message": m.Message,
|
||||||
|
"encoding": m.Encoding,
|
||||||
|
}
|
||||||
|
if m.Attachment != nil {
|
||||||
|
data["attachment_name"] = m.Attachment.Name
|
||||||
|
data["attachment_type"] = m.Attachment.Type
|
||||||
|
data["attachment_size"] = fmt.Sprintf("%d", m.Attachment.Size)
|
||||||
|
data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
|
||||||
|
data["attachment_url"] = m.Attachment.URL
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If anonymous read for a topic is not allowed, we cannot send the message along
|
||||||
|
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
|
||||||
data = map[string]string{
|
data = map[string]string{
|
||||||
"id": m.ID,
|
"id": m.ID,
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
"time": fmt.Sprintf("%d", m.Time),
|
||||||
"event": m.Event,
|
"event": pollRequestEvent,
|
||||||
"topic": m.Topic,
|
"topic": m.Topic,
|
||||||
}
|
}
|
||||||
case messageEvent:
|
|
||||||
allowForward := true
|
|
||||||
if auther != nil {
|
|
||||||
allowForward = auther.Authorize(nil, m.Topic, auth.PermissionRead) == nil
|
|
||||||
}
|
|
||||||
if allowForward {
|
|
||||||
data = map[string]string{
|
|
||||||
"id": m.ID,
|
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
|
||||||
"event": m.Event,
|
|
||||||
"topic": m.Topic,
|
|
||||||
"priority": fmt.Sprintf("%d", m.Priority),
|
|
||||||
"tags": strings.Join(m.Tags, ","),
|
|
||||||
"click": m.Click,
|
|
||||||
"title": m.Title,
|
|
||||||
"message": m.Message,
|
|
||||||
"encoding": m.Encoding,
|
|
||||||
}
|
|
||||||
if m.Attachment != nil {
|
|
||||||
data["attachment_name"] = m.Attachment.Name
|
|
||||||
data["attachment_type"] = m.Attachment.Type
|
|
||||||
data["attachment_size"] = fmt.Sprintf("%d", m.Attachment.Size)
|
|
||||||
data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
|
|
||||||
data["attachment_url"] = m.Attachment.URL
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If anonymous read for a topic is not allowed, we cannot send the message along
|
|
||||||
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
|
|
||||||
data = map[string]string{
|
|
||||||
"id": m.ID,
|
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
|
||||||
"event": pollRequestEvent,
|
|
||||||
"topic": m.Topic,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var androidConfig *messaging.AndroidConfig
|
}
|
||||||
if m.Priority >= 4 {
|
var androidConfig *messaging.AndroidConfig
|
||||||
androidConfig = &messaging.AndroidConfig{
|
if m.Priority >= 4 {
|
||||||
Priority: "high",
|
androidConfig = &messaging.AndroidConfig{
|
||||||
}
|
Priority: "high",
|
||||||
}
|
}
|
||||||
return sender(maybeTruncateFCMMessage(&messaging.Message{
|
}
|
||||||
Topic: m.Topic,
|
return maybeTruncateFCMMessage(&messaging.Message{
|
||||||
Data: data,
|
Topic: m.Topic,
|
||||||
Android: androidConfig,
|
Data: data,
|
||||||
}))
|
Android: androidConfig,
|
||||||
}, nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue