Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (notifications): Limit Discord messages to 2000 characters

Rostislav Dugin11d5fd64bd37
diff --git a/backend/internal/features/notifiers/models/discord/constants.go b/backend/internal/features/notifiers/models/discord/constants.go
new file mode 100644
index 00000000..52b68d61
--- /dev/null
+++ b/backend/internal/features/notifiers/models/discord/constants.go
@@ -0,0 +1,3 @@
+package discord_notifier
+
+const maxContentRunes = 2000
diff --git a/backend/internal/features/notifiers/models/discord/model.go b/backend/internal/features/notifiers/models/discord/model.go
index 90ae6879..75dfb480 100644
--- a/backend/internal/features/notifiers/models/discord/model.go
+++ b/backend/internal/features/notifiers/models/discord/model.go
@@ -43,13 +43,8 @@ func (d *DiscordNotifier) Send(
return fmt.Errorf("failed to decrypt webhook URL: %w", err)
}
- fullMessage := notification.Heading
- if notification.Message != "" {
- fullMessage = fmt.Sprintf("%s\n\n%s", notification.Heading, notification.Message)
- }
-
payload := map[string]any{
- "content": fullMessage,
+ "content": buildAndTruncateContent(notification),
}
jsonPayload, err := json.Marshal(payload)
@@ -85,6 +80,20 @@ func (d *DiscordNotifier) Send(
return nil
}
+func buildAndTruncateContent(notification notifier_models.Notification) string {
+ content := notification.Heading
+ if notification.Message != "" {
+ content = fmt.Sprintf("%s\n\n%s", notification.Heading, notification.Message)
+ }
+
+ contentRunes := []rune(content)
+ if len(contentRunes) > maxContentRunes {
+ return string(contentRunes[:maxContentRunes])
+ }
+
+ return content
+}
+
func (d *DiscordNotifier) HideSensitiveData() {
d.ChannelWebhookURL = ""
}
diff --git a/backend/internal/features/notifiers/models/discord/model_test.go b/backend/internal/features/notifiers/models/discord/model_test.go
new file mode 100644
index 00000000..6b9b6785
--- /dev/null
+++ b/backend/internal/features/notifiers/models/discord/model_test.go
@@ -0,0 +1,46 @@
+package discord_notifier
+
+import (
+ "encoding/json"
+ "io"
+ "log/slog"
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ notifier_models "databasus-backend/internal/features/notifiers/models"
+)
+
+func Test_Send_WhenContentExceedsDiscordLimit_TruncatesContentByRunes(t *testing.T) {
+ webhookURL, recorder := notifier_models.StartRecordingServer(
+ t,
+ notifier_models.StubResponse{StatusCode: http.StatusNoContent},
+ )
+ notifier := &DiscordNotifier{ChannelWebhookURL: webhookURL}
+ heading := "Backup failed"
+
+ require.NoError(t, notifier.Send(
+ notifier_models.PassthroughEncryptor{},
+ slog.New(slog.NewTextHandler(io.Discard, nil)),
+ notifier_models.Notification{
+ Type: notifier_models.NotificationTypeBackupFailed,
+ Heading: heading,
+ Message: strings.Repeat("界", maxContentRunes),
+ },
+ ))
+
+ require.Equal(t, 1, recorder.GetRequestCount())
+ request := recorder.GetLastRequest()
+ assert.Equal(t, http.MethodPost, request.Method)
+ assert.Equal(t, "application/json", request.Headers.Get("Content-Type"))
+
+ var payload map[string]string
+ require.NoError(t, json.Unmarshal([]byte(request.Body), &payload))
+
+ messagePrefixLength := maxContentRunes - len([]rune(heading)) - len([]rune("\n\n"))
+ truncatedContent := heading + "\n\n" + strings.Repeat("界", messagePrefixLength)
+ assert.Equal(t, truncatedContent, payload["content"])
+}