-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made the feedback subjects send out via mail nicer as discussed offli…
…ne with @jakobkoerber
- Loading branch information
1 parent
95cf2cc
commit 5deab4e
Showing
5 changed files
with
50 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 == "[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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
@@ -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> | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters