mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-14 11:47:33 +00:00
Fix message truncation, relates to #84
This commit is contained in:
parent
76d102f964
commit
2e40b895a7
2 changed files with 8 additions and 8 deletions
|
@ -138,7 +138,7 @@ var (
|
|||
const (
|
||||
firebaseControlTopic = "~control" // See Android if changed
|
||||
emptyMessageBody = "triggered"
|
||||
fcmMessageLimitReal = 4100 // see maybeTruncateFCMMessage for details
|
||||
fcmMessageLimit = 4000 // see maybeTruncateFCMMessage for details
|
||||
)
|
||||
|
||||
// New instantiates a new Server. It creates the cache and adds a Firebase
|
||||
|
@ -230,15 +230,15 @@ func createFirebaseSubscriber(conf *Config) (subscriber, error) {
|
|||
}
|
||||
|
||||
// maybeTruncateFCMMessage performs best-effort truncation of FCM messages.
|
||||
// The docs says the limit is 4000 characters, but the real FCM message limit is 4100 of the
|
||||
// serialized payload; I tested this diligently.
|
||||
// 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) > fcmMessageLimitReal {
|
||||
over := len(s) - fcmMessageLimitReal + 16 // = len("truncated":"1",), sigh ...
|
||||
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"
|
||||
|
|
|
@ -612,12 +612,12 @@ func TestServer_MaybeTruncateFCMMessage(t *testing.T) {
|
|||
}
|
||||
origMessageLength := len(origFCMMessage.Data["message"])
|
||||
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
|
||||
require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimitReal) // Pre-condition
|
||||
require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
|
||||
|
||||
truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
|
||||
truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
|
||||
serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
|
||||
require.Equal(t, fcmMessageLimitReal, len(serializedTruncatedFCMMessage))
|
||||
require.Equal(t, fcmMessageLimit, len(serializedTruncatedFCMMessage))
|
||||
require.Equal(t, "1", truncatedFCMMessage.Data["truncated"])
|
||||
require.NotEqual(t, origMessageLength, truncatedMessageLength)
|
||||
}
|
||||
|
@ -639,7 +639,7 @@ func TestServer_MaybeTruncateFCMMessage_NotTooLong(t *testing.T) {
|
|||
}
|
||||
origMessageLength := len(origFCMMessage.Data["message"])
|
||||
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
|
||||
require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimitReal) // Pre-condition
|
||||
require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
|
||||
|
||||
notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
|
||||
notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"])
|
||||
|
|
Loading…
Reference in a new issue