Skip to content

Commit

Permalink
made the feedback subjects send out via mail nicer as discussed offli…
Browse files Browse the repository at this point in the history
…ne with @jakobkoerber
  • Loading branch information
CommanderStorm committed Mar 12, 2024
1 parent 95cf2cc commit 5deab4e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/backend/cron/email_templates/feedback_body.gohtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>Feedback via TumCampusApp:</h1>
<b>Feedback via the TUM Campus App:</b>
{{ if .Feedback -}}
<blockquote>
{{- .Feedback -}}
Expand Down
2 changes: 1 addition & 1 deletion server/backend/cron/email_templates/feedback_body.txt.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feedback via TumCampusApp:
Feedback via the TUM Campus App:

{{ if .Feedback }}
{{- .Feedback -}}
Expand Down
30 changes: 29 additions & 1 deletion server/backend/cron/feedback_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"bytes"
"fmt"
htmlTemplate "html/template"
"os"
"strconv"
Expand Down Expand Up @@ -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 == "[email protected]" {
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 {
Expand Down
21 changes: 15 additions & 6 deletions server/backend/cron/feedback_email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestHeaderInstantiationWithFullFeedback(t *testing.T) {
assert.Equal(t, []string{fb.Recipient}, m.GetHeader("To"))
assert.Equal(t, []string{"[email protected]"}, 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) {
Expand All @@ -76,15 +76,24 @@ 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) {
html, txt, err := parseTemplates()
require.NoError(t, err)
htmlBody, txtBody, err := generateTemplatedMail(html, txt, fullFeedback())
require.NoError(t, err)
assert.Equal(t, `<h1>Feedback via TumCampusApp:</h1>
assert.Equal(t, `<b>Feedback via the TUM Campus App:</b>
<blockquote>This is a Test</blockquote>
<table>
<tr>
Expand Down Expand Up @@ -114,7 +123,7 @@ func TestTemplatingResultsWithFullFeedback(t *testing.T) {
<a href="https://app.tum.de/File/feedback/0/0.png">Foto 0</a>
</li>
</ol>`, htmlBody)
assert.Equal(t, `Feedback via TumCampusApp:
assert.Equal(t, `Feedback via the TUM Campus App:
This is a Test
Expand All @@ -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, `<h1>Feedback via TumCampusApp:</h1>
assert.Equal(t, `<b>Feedback via the TUM Campus App:</b>
<i>no feedback provided</i>
<table>
<tr>
Expand All @@ -149,7 +158,7 @@ func TestTemplatingResultsWithEmptyFeedback(t *testing.T) {
<td>unknown</td>
</tr>
</table>`, htmlBody)
assert.Equal(t, `Feedback via TumCampusApp:
assert.Equal(t, `Feedback via the TUM Campus App:
no feedback provided
Expand Down
4 changes: 4 additions & 0 deletions server/backend/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 5deab4e

Please sign in to comment.