1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-06-23 04:48:03 +02:00

Format emojis in the service worker directly

This commit is contained in:
nimbleghost 2023-05-31 18:27:32 +02:00
parent 44913c1668
commit 4648f83669
12 changed files with 85 additions and 112 deletions

View file

@ -6,7 +6,6 @@ import (
"github.com/SherClockHolmes/webpush-go"
"heckel.io/ntfy/log"
"net/http"
"strings"
)
func (s *Server) handleTopicWebPushSubscribe(w http.ResponseWriter, r *http.Request, v *visitor) error {
@ -55,27 +54,6 @@ func (s *Server) publishToWebPushEndpoints(v *visitor, m *message) {
return
}
ctx := log.Context{"topic": m.Topic, "message_id": m.ID, "total_count": len(subscriptions)}
// Importing the emojis in the service worker would add unnecessary complexity,
// simply do it here for web push notifications instead
var titleWithDefault, formattedTitle string
emojis, _, err := toEmojis(m.Tags)
if err != nil {
logvm(v, m).Err(err).Fields(ctx).Debug("Unable to publish web push message")
return
}
if m.Title == "" {
titleWithDefault = m.Topic
} else {
titleWithDefault = m.Title
}
if len(emojis) > 0 {
formattedTitle = fmt.Sprintf("%s %s", strings.Join(emojis[:], " "), titleWithDefault)
} else {
formattedTitle = titleWithDefault
}
for i, xi := range subscriptions {
go func(i int, sub webPushSubscription) {
ctx := log.Context{"endpoint": sub.BrowserSubscription.Endpoint, "username": sub.UserID, "topic": m.Topic, "message_id": m.ID}
@ -83,7 +61,6 @@ func (s *Server) publishToWebPushEndpoints(v *visitor, m *message) {
payload := &webPushPayload{
SubscriptionID: fmt.Sprintf("%s/%s", s.config.BaseURL, m.Topic),
Message: *m,
FormattedTitle: formattedTitle,
}
jsonPayload, err := json.Marshal(payload)

View file

@ -1,6 +1,8 @@
package server
import (
_ "embed" // required by go:embed
"encoding/json"
"fmt"
"mime"
"net"
@ -128,3 +130,25 @@ This message was sent by {ip} at {time} via {topicURL}`
body = strings.ReplaceAll(body, "{ip}", senderIP)
return body, nil
}
var (
//go:embed "mailer_emoji_map.json"
emojisJSON string
)
func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) {
var emojiMap map[string]string
if err = json.Unmarshal([]byte(emojisJSON), &emojiMap); err != nil {
return nil, nil, err
}
tagsOut = make([]string, 0)
emojisOut = make([]string, 0)
for _, t := range tags {
if emoji, ok := emojiMap[t]; ok {
emojisOut = append(emojisOut, emoji)
} else {
tagsOut = append(tagsOut, t)
}
}
return
}

View file

@ -469,7 +469,6 @@ type apiStripeSubscriptionDeletedEvent struct {
type webPushPayload struct {
SubscriptionID string `json:"subscription_id"`
Message message `json:"message"`
FormattedTitle string `json:"formatted_title"`
}
type webPushSubscription struct {

View file

@ -2,8 +2,6 @@ package server
import (
"context"
_ "embed" // required by go:embed
"encoding/json"
"fmt"
"heckel.io/ntfy/util"
"io"
@ -135,25 +133,3 @@ func maybeDecodeHeader(header string) string {
}
return decoded
}
var (
//go:embed "mailer_emoji_map.json"
emojisJSON string
)
func toEmojis(tags []string) (emojisOut []string, tagsOut []string, err error) {
var emojiMap map[string]string
if err = json.Unmarshal([]byte(emojisJSON), &emojiMap); err != nil {
return nil, nil, err
}
tagsOut = make([]string, 0)
emojisOut = make([]string, 0)
for _, t := range tags {
if emoji, ok := emojiMap[t]; ok {
emojisOut = append(emojisOut, emoji)
} else {
tagsOut = append(tagsOut, t)
}
}
return
}