mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-11-26 21:25:18 +01:00
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"firebase.google.com/go/messaging"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
fcmMessageLimit = 4000
|
||
|
)
|
||
|
|
||
|
// maybeTruncateFCMMessage performs best-effort truncation of FCM messages.
|
||
|
// The docs say the limit is 4000 characters, but during testing it wasn't quite clear
|
||
|
// what fields matter; so we're just capping the serialized JSON to 4000 bytes.
|
||
|
func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message {
|
||
|
s, err := json.Marshal(m)
|
||
|
if err != nil {
|
||
|
return m
|
||
|
}
|
||
|
if len(s) > fcmMessageLimit {
|
||
|
over := len(s) - fcmMessageLimit + 16 // = len("truncated":"1",), sigh ...
|
||
|
message, ok := m.Data["message"]
|
||
|
if ok && len(message) > over {
|
||
|
m.Data["truncated"] = "1"
|
||
|
m.Data["message"] = message[:len(message)-over]
|
||
|
}
|
||
|
}
|
||
|
return m
|
||
|
}
|
||
|
|
||
|
func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
|
||
|
value := strings.ToLower(readParam(r, names...))
|
||
|
if value == "" {
|
||
|
return defaultValue
|
||
|
}
|
||
|
return value == "1" || value == "yes" || value == "true"
|
||
|
}
|
||
|
|
||
|
func readParam(r *http.Request, names ...string) string {
|
||
|
for _, name := range names {
|
||
|
value := r.Header.Get(name)
|
||
|
if value != "" {
|
||
|
return strings.TrimSpace(value)
|
||
|
}
|
||
|
}
|
||
|
for _, name := range names {
|
||
|
value := r.URL.Query().Get(strings.ToLower(name))
|
||
|
if value != "" {
|
||
|
return strings.TrimSpace(value)
|
||
|
}
|
||
|
}
|
||
|
return ""
|
||
|
}
|