Skip to content

Commit ab01e86

Browse files
committed
Handle notification title based on content type
1 parent 99395e6 commit ab01e86

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

internal/dispatcher/notification.go

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@ import (
1616
"github.com/pushbits/server/internal/pberrors"
1717
)
1818

19+
type notificationContentType string
20+
21+
const (
22+
contentTypePlain notificationContentType = "text/plain"
23+
contentTypeMarkdown notificationContentType = "text/markdown"
24+
contentTypeHTML notificationContentType = "text/html"
25+
)
26+
27+
func getContentType(extras map[string]any) notificationContentType {
28+
if optionsDisplayRaw, ok := extras["client::display"]; ok {
29+
if optionsDisplay, ok2 := optionsDisplayRaw.(map[string]interface{}); ok2 {
30+
if ctRaw, ok3 := optionsDisplay["contentType"]; ok3 {
31+
contentTypeString := strings.ToLower(fmt.Sprintf("%v", ctRaw))
32+
switch contentTypeString {
33+
case "text/markdown":
34+
return contentTypeMarkdown
35+
case "text/html":
36+
return contentTypeHTML
37+
case "text/plain":
38+
return contentTypePlain
39+
default:
40+
log.L.Printf("Unknown content type specified: %s, defaulting to text/plain", contentTypeString)
41+
return contentTypePlain
42+
}
43+
}
44+
}
45+
}
46+
47+
return contentTypePlain
48+
}
49+
1950
// MessageFormat is a matrix message format
2051
type MessageFormat string
2152

@@ -60,10 +91,10 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
6091
plainMessage := strings.TrimSpace(n.Message)
6192
plainTitle := strings.TrimSpace(n.Title)
6293
message := d.getFormattedMessage(n)
63-
title := d.getFormattedTitle(n)
94+
title := d.getFormattedTitle(n) // Does not append <br /><br /> anymore
6495

6596
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
66-
formattedText := fmt.Sprintf("%s %s", title, message)
97+
formattedText := fmt.Sprintf("%s<br /><br />%s", title, message) // Append <br /><br /> here
6798

6899
messageEvent := &MessageEvent{
69100
Body: text,
@@ -116,37 +147,41 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot
116147
// HTML-formats the title
117148
func (d *Dispatcher) getFormattedTitle(n *model.Notification) string {
118149
trimmedTitle := strings.TrimSpace(n.Title)
119-
title := html.EscapeString(trimmedTitle)
150+
var title string
151+
152+
contentType := getContentType(n.Extras)
153+
154+
switch contentType {
155+
case contentTypeMarkdown:
156+
title = string(markdown.ToHTML([]byte(trimmedTitle), nil, nil))
157+
case contentTypeHTML:
158+
title = trimmedTitle
159+
case contentTypePlain:
160+
title = html.EscapeString(trimmedTitle)
161+
title = "<b>" + title + "</b>"
162+
}
120163

121164
if d.formatting.ColoredTitle {
122165
title = d.coloredText(d.priorityToColor(n.Priority), title)
123166
}
124167

125-
return "<b>" + title + "</b><br /><br />"
168+
return title
126169
}
127170

128171
// Converts different syntaxes to a HTML-formatted message
129172
func (d *Dispatcher) getFormattedMessage(n *model.Notification) string {
130173
trimmedMessage := strings.TrimSpace(n.Message)
131-
message := strings.ReplaceAll(html.EscapeString(trimmedMessage), "\n", "<br />") // default to text/plain
132-
133-
if optionsDisplayRaw, ok := n.Extras["client::display"]; ok {
134-
optionsDisplay, ok := optionsDisplayRaw.(map[string]interface{})
135-
136-
if ok {
137-
if contentTypeRaw, ok := optionsDisplay["contentType"]; ok {
138-
contentType := fmt.Sprintf("%v", contentTypeRaw)
139-
log.L.Printf("Message content type: %s", contentType)
140-
141-
switch contentType {
142-
case "html", "text/html":
143-
message = strings.ReplaceAll(trimmedMessage, "\n", "<br />")
144-
case "markdown", "md", "text/md", "text/markdown":
145-
// Allow HTML in Markdown
146-
message = string(markdown.ToHTML([]byte(trimmedMessage), nil, nil))
147-
}
148-
}
149-
}
174+
var message string
175+
176+
contentType := getContentType(n.Extras)
177+
178+
switch contentType {
179+
case contentTypeMarkdown:
180+
message = string(markdown.ToHTML([]byte(trimmedMessage), nil, nil))
181+
case contentTypeHTML:
182+
message = trimmedMessage
183+
case contentTypePlain:
184+
message = strings.ReplaceAll(html.EscapeString(trimmedMessage), "\n", "<br />")
150185
}
151186

152187
return message

0 commit comments

Comments
 (0)