From 567c8149686b35d9c6be949a1c94d33654cb0823 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Sat, 1 May 2021 21:34:21 +0200 Subject: [PATCH] added formatting options --- .gitignore | 1 + go.mod | 1 + go.sum | 3 +++ internal/dispatcher/notification.go | 31 +++++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index dd28c34..4471252 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ config.yml # Dependency directories (remove the comment below to include it) # vendor/ +*.code-workspace diff --git a/go.mod b/go.mod index ea44785..e5c9e72 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 93a6155..c30c298 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/dispatcher/notification.go b/internal/dispatcher/notification.go index fd26991..ba7f612 100644 --- a/internal/dispatcher/notification.go +++ b/internal/dispatcher/notification.go @@ -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("%s

%s", escapedTitle, escapedMessage) + formattedText := fmt.Sprintf("%s

%s", escapedTitle, message) _, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)