server_firebase.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package server
  2. import (
  3. "context"
  4. "encoding/json"
  5. firebase "firebase.google.com/go"
  6. "firebase.google.com/go/messaging"
  7. "fmt"
  8. "google.golang.org/api/option"
  9. "heckel.io/ntfy/auth"
  10. "strings"
  11. )
  12. const (
  13. fcmMessageLimit = 4000
  14. )
  15. // maybeTruncateFCMMessage performs best-effort truncation of FCM messages.
  16. // The docs say the limit is 4000 characters, but during testing it wasn't quite clear
  17. // what fields matter; so we're just capping the serialized JSON to 4000 bytes.
  18. func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message {
  19. s, err := json.Marshal(m)
  20. if err != nil {
  21. return m
  22. }
  23. if len(s) > fcmMessageLimit {
  24. over := len(s) - fcmMessageLimit + 16 // = len("truncated":"1",), sigh ...
  25. message, ok := m.Data["message"]
  26. if ok && len(message) > over {
  27. m.Data["truncated"] = "1"
  28. m.Data["message"] = message[:len(message)-over]
  29. }
  30. }
  31. return m
  32. }
  33. func createFirebaseSubscriber(credentialsFile string, auther auth.Auther) (subscriber, error) {
  34. fb, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(credentialsFile))
  35. if err != nil {
  36. return nil, err
  37. }
  38. msg, err := fb.Messaging(context.Background())
  39. if err != nil {
  40. return nil, err
  41. }
  42. return func(m *message) error {
  43. fbm, err := toFirebaseMessage(m, auther)
  44. if err != nil {
  45. return err
  46. }
  47. _, err = msg.Send(context.Background(), fbm)
  48. return err
  49. }, nil
  50. }
  51. func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, error) {
  52. var data map[string]string // Mostly matches https://ntfy.sh/docs/subscribe/api/#json-message-format
  53. switch m.Event {
  54. case keepaliveEvent, openEvent:
  55. data = map[string]string{
  56. "id": m.ID,
  57. "time": fmt.Sprintf("%d", m.Time),
  58. "event": m.Event,
  59. "topic": m.Topic,
  60. }
  61. case messageEvent:
  62. allowForward := true
  63. if auther != nil {
  64. allowForward = auther.Authorize(nil, m.Topic, auth.PermissionRead) == nil
  65. }
  66. if allowForward {
  67. data = map[string]string{
  68. "id": m.ID,
  69. "time": fmt.Sprintf("%d", m.Time),
  70. "updated": fmt.Sprintf("%d", m.Updated),
  71. "event": m.Event,
  72. "topic": m.Topic,
  73. "priority": fmt.Sprintf("%d", m.Priority),
  74. "tags": strings.Join(m.Tags, ","),
  75. "click": m.Click,
  76. "title": m.Title,
  77. "message": m.Message,
  78. "encoding": m.Encoding,
  79. }
  80. if m.Attachment != nil {
  81. data["attachment_name"] = m.Attachment.Name
  82. data["attachment_type"] = m.Attachment.Type
  83. data["attachment_size"] = fmt.Sprintf("%d", m.Attachment.Size)
  84. data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
  85. data["attachment_url"] = m.Attachment.URL
  86. }
  87. } else {
  88. // If anonymous read for a topic is not allowed, we cannot send the message along
  89. // via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
  90. data = map[string]string{
  91. "id": m.ID,
  92. "time": fmt.Sprintf("%d", m.Time),
  93. "event": pollRequestEvent,
  94. "topic": m.Topic,
  95. }
  96. }
  97. }
  98. var androidConfig *messaging.AndroidConfig
  99. if m.Priority >= 4 {
  100. androidConfig = &messaging.AndroidConfig{
  101. Priority: "high",
  102. }
  103. }
  104. return maybeTruncateFCMMessage(&messaging.Message{
  105. Topic: m.Topic,
  106. Data: data,
  107. Android: androidConfig,
  108. }), nil
  109. }