From 2e40b895a707b787d7243bff98dfd6617557c00d Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 4 Jan 2022 21:09:47 +0100 Subject: [PATCH] Fix message truncation, relates to #84 --- server/server.go | 10 +++++----- server/server_test.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/server.go b/server/server.go index a9799c6..8a96c28 100644 --- a/server/server.go +++ b/server/server.go @@ -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" diff --git a/server/server_test.go b/server/server_test.go index 92952a3..ac956a0 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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"])