From 5deab4eea5d6ebbc683a5f7c3ca36d24183d1411 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 13 Mar 2024 00:35:24 +0100 Subject: [PATCH] made the feedback subjects send out via mail nicer as discussed offline with @jakobkoerber --- .../cron/email_templates/feedback_body.gohtml | 2 +- .../email_templates/feedback_body.txt.tmpl | 2 +- server/backend/cron/feedback_email.go | 30 ++++++++++++++++++- server/backend/cron/feedback_email_test.go | 21 +++++++++---- server/backend/feedback.go | 4 +++ 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/server/backend/cron/email_templates/feedback_body.gohtml b/server/backend/cron/email_templates/feedback_body.gohtml index b5dd640c..45e9957e 100644 --- a/server/backend/cron/email_templates/feedback_body.gohtml +++ b/server/backend/cron/email_templates/feedback_body.gohtml @@ -1,4 +1,4 @@ -

Feedback via TumCampusApp:

+Feedback via the TUM Campus App: {{ if .Feedback -}}
{{- .Feedback -}} diff --git a/server/backend/cron/email_templates/feedback_body.txt.tmpl b/server/backend/cron/email_templates/feedback_body.txt.tmpl index b4b2cc16..43effd25 100644 --- a/server/backend/cron/email_templates/feedback_body.txt.tmpl +++ b/server/backend/cron/email_templates/feedback_body.txt.tmpl @@ -1,4 +1,4 @@ -Feedback via TumCampusApp: +Feedback via the TUM Campus App: {{ if .Feedback }} {{- .Feedback -}} diff --git a/server/backend/cron/feedback_email.go b/server/backend/cron/feedback_email.go index 3104d7c0..cbb27566 100644 --- a/server/backend/cron/feedback_email.go +++ b/server/backend/cron/feedback_email.go @@ -2,6 +2,7 @@ package cron import ( "bytes" + "fmt" htmlTemplate "html/template" "os" "strconv" @@ -74,10 +75,37 @@ func messageWithHeaders(feedback *model.Feedback) *gomail.Message { m.SetDateHeader("Date", time.Now()) } // Subject - m.SetHeader("Subject", "Feedback via Tum Campus App") + if feedback.Recipient == "app@tum.de" { + versionString := "TCA" + if feedback.AppVersion.Valid { + versionString = fmt.Sprintf("TCA %s", feedback.AppVersion.String) + } + m.SetHeader("Subject", fmt.Sprintf("[%s] %s", truncate(versionString, 10), truncate(feedback.Feedback, 150))) + } else { + m.SetHeader("Subject", "Feedback via the TUM Campus App") + } + return m } +func truncate(str string, length int) string { + if length <= 0 { + return "" + } + if len(str) <= length { + return str + } + truncated := "" + for i, char := range str { + if i >= length { + break + } + truncated += string(char) + } + truncated += ".." + return truncated +} + func generateTemplatedMail(parsedHtmlBody *htmlTemplate.Template, parsedTxtBody *textTemplate.Template, feedback *model.Feedback) (string, string, error) { var htmlBodyBuffer bytes.Buffer if err := parsedHtmlBody.Execute(&htmlBodyBuffer, feedback); err != nil { diff --git a/server/backend/cron/feedback_email_test.go b/server/backend/cron/feedback_email_test.go index 34ac9620..410bc928 100644 --- a/server/backend/cron/feedback_email_test.go +++ b/server/backend/cron/feedback_email_test.go @@ -60,7 +60,7 @@ func TestHeaderInstantiationWithFullFeedback(t *testing.T) { assert.Equal(t, []string{fb.Recipient}, m.GetHeader("To")) assert.Equal(t, []string{"test@example.de"}, m.GetHeader("Reply-To")) assert.Equal(t, []string{fb.Timestamp.Time.Format(time.RFC1123Z)}, m.GetHeader("Date")) - assert.Equal(t, []string{"Feedback via Tum Campus App"}, m.GetHeader("Subject")) + assert.Equal(t, []string{"Feedback via the TUM Campus App"}, m.GetHeader("Subject")) } func TestHeaderInstantiationWithEmptyFeedback(t *testing.T) { @@ -76,7 +76,16 @@ func TestHeaderInstantiationWithEmptyFeedback(t *testing.T) { date, err := time.Parse(time.RFC1123Z, dates[0]) require.NoError(t, err) assert.WithinDuration(t, time.Now(), date, time.Minute) - assert.Equal(t, []string{"Feedback via Tum Campus App"}, m.GetHeader("Subject")) + assert.Equal(t, []string{"Feedback via the TUM Campus App"}, m.GetHeader("Subject")) +} + +func TestTrunaction(t *testing.T) { + require.Equal(t, truncate("abc", -1), "") + require.Equal(t, truncate("abc", 0), "") + require.Equal(t, truncate("abc", 1), "a..") + require.Equal(t, truncate("abc", 2), "ab..") + require.Equal(t, truncate("abc", 3), "abc") + require.Equal(t, truncate("abc", 200), "abc") } func TestTemplatingResultsWithFullFeedback(t *testing.T) { @@ -84,7 +93,7 @@ func TestTemplatingResultsWithFullFeedback(t *testing.T) { require.NoError(t, err) htmlBody, txtBody, err := generateTemplatedMail(html, txt, fullFeedback()) require.NoError(t, err) - assert.Equal(t, `

Feedback via TumCampusApp:

+ assert.Equal(t, `Feedback via the TUM Campus App:
This is a Test
@@ -114,7 +123,7 @@ func TestTemplatingResultsWithFullFeedback(t *testing.T) { Foto 0`, htmlBody) - assert.Equal(t, `Feedback via TumCampusApp: + assert.Equal(t, `Feedback via the TUM Campus App: This is a Test @@ -133,7 +142,7 @@ func TestTemplatingResultsWithEmptyFeedback(t *testing.T) { require.NoError(t, err) htmlBody, txtBody, err := generateTemplatedMail(html, txt, emptyFeedback()) require.NoError(t, err) - assert.Equal(t, `

Feedback via TumCampusApp:

+ assert.Equal(t, `Feedback via the TUM Campus App:no feedback provided
@@ -149,7 +158,7 @@ func TestTemplatingResultsWithEmptyFeedback(t *testing.T) {
unknown
`, htmlBody) - assert.Equal(t, `Feedback via TumCampusApp: + assert.Equal(t, `Feedback via the TUM Campus App: no feedback provided diff --git a/server/backend/feedback.go b/server/backend/feedback.go index 75cfc457..0a356ff2 100644 --- a/server/backend/feedback.go +++ b/server/backend/feedback.go @@ -7,6 +7,7 @@ import ( "os" "path" "slices" + "strings" pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev" "github.com/TUM-Dev/Campus-Backend/server/backend/cron" @@ -54,6 +55,9 @@ func (s *CampusServer) CreateFeedback(stream pb.Campus_CreateFeedbackServer) err } feedback.ImageCount = int32(len(uploadedFilenames)) // validate feedback + feedback.Feedback = strings.TrimSpace(feedback.Feedback) + feedback.Feedback = strings.ReplaceAll(feedback.Feedback, " ", " ") + feedback.Feedback = strings.ToValidUTF8(feedback.Feedback, "?") if feedback.Feedback == "" && feedback.ImageCount == 0 { return status.Error(codes.InvalidArgument, "Please attach an image or feedback for us") }