Fix message truncation, relates to #84

This commit is contained in:
Philipp Heckel 2022-01-04 21:09:47 +01:00
parent 76d102f964
commit 2e40b895a7
2 changed files with 8 additions and 8 deletions

View file

@ -138,7 +138,7 @@ var (
const ( const (
firebaseControlTopic = "~control" // See Android if changed firebaseControlTopic = "~control" // See Android if changed
emptyMessageBody = "triggered" 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 // 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. // 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 // The docs say the limit is 4000 characters, but during testing it wasn't quite clear
// serialized payload; I tested this diligently. // what fields matter; so we're just capping the serialized JSON to 4000 bytes.
func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message { func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message {
s, err := json.Marshal(m) s, err := json.Marshal(m)
if err != nil { if err != nil {
return m return m
} }
if len(s) > fcmMessageLimitReal { if len(s) > fcmMessageLimit {
over := len(s) - fcmMessageLimitReal + 16 // = len("truncated":"1",), sigh ... over := len(s) - fcmMessageLimit + 16 // = len("truncated":"1",), sigh ...
message, ok := m.Data["message"] message, ok := m.Data["message"]
if ok && len(message) > over { if ok && len(message) > over {
m.Data["truncated"] = "1" m.Data["truncated"] = "1"

View file

@ -612,12 +612,12 @@ func TestServer_MaybeTruncateFCMMessage(t *testing.T) {
} }
origMessageLength := len(origFCMMessage.Data["message"]) origMessageLength := len(origFCMMessage.Data["message"])
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage) serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimitReal) // Pre-condition require.Greater(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage) truncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
truncatedMessageLength := len(truncatedFCMMessage.Data["message"]) truncatedMessageLength := len(truncatedFCMMessage.Data["message"])
serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage) serializedTruncatedFCMMessage, _ := json.Marshal(truncatedFCMMessage)
require.Equal(t, fcmMessageLimitReal, len(serializedTruncatedFCMMessage)) require.Equal(t, fcmMessageLimit, len(serializedTruncatedFCMMessage))
require.Equal(t, "1", truncatedFCMMessage.Data["truncated"]) require.Equal(t, "1", truncatedFCMMessage.Data["truncated"])
require.NotEqual(t, origMessageLength, truncatedMessageLength) require.NotEqual(t, origMessageLength, truncatedMessageLength)
} }
@ -639,7 +639,7 @@ func TestServer_MaybeTruncateFCMMessage_NotTooLong(t *testing.T) {
} }
origMessageLength := len(origFCMMessage.Data["message"]) origMessageLength := len(origFCMMessage.Data["message"])
serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage) serializedOrigFCMMessage, _ := json.Marshal(origFCMMessage)
require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimitReal) // Pre-condition require.LessOrEqual(t, len(serializedOrigFCMMessage), fcmMessageLimit) // Pre-condition
notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage) notTruncatedFCMMessage := maybeTruncateFCMMessage(origFCMMessage)
notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"]) notTruncatedMessageLength := len(notTruncatedFCMMessage.Data["message"])