added formatting options

This commit is contained in:
Cubicroot 2021-05-01 21:34:21 +02:00
parent 2e2326843f
commit 567c814968
4 changed files with 34 additions and 2 deletions

1
.gitignore vendored
View file

@ -19,3 +19,4 @@ config.yml
# Dependency directories (remove the comment below to include it)
# vendor/
*.code-workspace

1
go.mod
View file

@ -8,6 +8,7 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gomarkdown/markdown v0.0.0-20210408062403-ad838ccf8cdd
github.com/google/go-cmp v0.5.0 // indirect
github.com/jinzhu/configor v1.2.1
github.com/json-iterator/go v1.1.10 // indirect

3
go.sum
View file

@ -32,6 +32,8 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/gomarkdown/markdown v0.0.0-20210408062403-ad838ccf8cdd h1:0b8AqsWQb6A0jjx80UXLG/uMTXQkGD0IGuXWqsrNz1M=
github.com/gomarkdown/markdown v0.0.0-20210408062403-ad838ccf8cdd/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@ -83,6 +85,7 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.4 h1:C5VurWRRCKjuENsbM6GYVw8W++WVW9rSxoACKIvxzz8=
github.com/ugorji/go/codec v1.2.4/go.mod h1:bWBu1+kIRWcF8uMklKaJrR6fTWQOwAlrIzX22pHwryA=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=

View file

@ -6,6 +6,7 @@ import (
"log"
"strings"
"github.com/gomarkdown/markdown"
"github.com/pushbits/server/internal/model"
)
@ -16,10 +17,36 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
plainTitle := strings.TrimSpace(n.Title)
plainMessage := strings.TrimSpace(n.Message)
escapedTitle := html.EscapeString(plainTitle)
escapedMessage := html.EscapeString(plainMessage)
message := html.EscapeString(plainMessage) // default to text/plain
if optionsDisplayRaw, ok := n.Extras["client::display"]; ok {
optionsDisplay, ok := optionsDisplayRaw.(map[string]interface{})
log.Printf("%s", optionsDisplay)
if ok {
if contentTypeRaw, ok := optionsDisplay["contentType"]; ok {
contentType := fmt.Sprintf("%v", contentTypeRaw)
log.Printf("Message content type: %s", contentType)
switch contentType {
case "html", "text/html":
message = plainMessage
case "markdown", "md", "text/md", "text/markdown":
message = string(markdown.ToHTML([]byte(plainMessage), nil, nil))
}
}
}
}
// TODO cubicroot: add colors for priority https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes
// maybe make this optional in the settings or so
// TODO cubicroot: check if we somehow can handle \n or other methods of line breaks
// TODO cubicroot: add docu
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
formattedText := fmt.Sprintf("<b>%s</b><br /><br />%s", escapedTitle, escapedMessage)
formattedText := fmt.Sprintf("<b>%s</b><br /><br />%s", escapedTitle, message)
_, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)