mirror of
https://github.com/pushbits/server.git
synced 2025-08-06 10:08:55 +02:00
Merge pull request #26 from CubicrootXYZ/master
Add support for HTML and Markdown + option for colored title
This commit is contained in:
commit
6c69be7d34
9 changed files with 114 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -19,3 +19,4 @@ config.yml
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
|
*.code-workspace
|
||||||
|
|
20
README.md
20
README.md
|
@ -114,6 +114,26 @@ You can retrieve the token using [pbcli](https://github.com/PushBits/cli) by run
|
||||||
pbcli application show $PB_APPLICATION --url https://pushbits.example.com --username $PB_USERNAME
|
pbcli application show $PB_APPLICATION --url https://pushbits.example.com --username $PB_USERNAME
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Message options
|
||||||
|
|
||||||
|
Messages are supporting three different syntaxes:
|
||||||
|
|
||||||
|
* text/plain
|
||||||
|
* text/html
|
||||||
|
* text/markdown
|
||||||
|
|
||||||
|
To set a specific syntax you need to set the `extras` ([inspired by Gotifys message extras](https://gotify.net/docs/msgextras#clientdisplay)):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl \
|
||||||
|
--header "Content-Type: application/json" \
|
||||||
|
--request POST \
|
||||||
|
--data '{"message":"my message with\n\n**Markdown** _support_.","title":"my title","extras":{"client::display":{"contentType": "text/markdown"}}}' \
|
||||||
|
"https://pushbits.example.com/message?token=$PB_TOKEN"
|
||||||
|
```
|
||||||
|
|
||||||
|
HTML-Content might not be fully rendered in your Matrix-Client - see the corresponding [Matrix specs](https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes). This also holds for Markdown, as it is transfered to the corresponding HTML-syntax.
|
||||||
|
|
||||||
## Acknowledgments
|
## Acknowledgments
|
||||||
|
|
||||||
The idea for this software and most parts of the initial source are heavily inspired by [Gotify](https://gotify.net/).
|
The idea for this software and most parts of the initial source are heavily inspired by [Gotify](https://gotify.net/).
|
||||||
|
|
|
@ -47,7 +47,7 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dp, err := dispatcher.Create(db, c.Matrix.Homeserver, c.Matrix.Username, c.Matrix.Password)
|
dp, err := dispatcher.Create(db, c.Matrix.Homeserver, c.Matrix.Username, c.Matrix.Password, c.Formatting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,3 +56,7 @@ crypto:
|
||||||
parallelism: 4
|
parallelism: 4
|
||||||
saltlength: 16
|
saltlength: 16
|
||||||
keylength: 32
|
keylength: 32
|
||||||
|
|
||||||
|
formatting:
|
||||||
|
# Whether to use colored titles based on the message priority (<0: grey, 0-3: default, 4-10: yellow, 10-20: orange, >20: red).
|
||||||
|
coloredtitle: false
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -8,6 +8,7 @@ require (
|
||||||
github.com/gin-gonic/gin v1.6.3
|
github.com/gin-gonic/gin v1.6.3
|
||||||
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
||||||
github.com/golang/protobuf v1.4.3 // 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/google/go-cmp v0.5.0 // indirect
|
||||||
github.com/jinzhu/configor v1.2.1
|
github.com/jinzhu/configor v1.2.1
|
||||||
github.com/json-iterator/go v1.1.10 // indirect
|
github.com/json-iterator/go v1.1.10 // indirect
|
||||||
|
|
3
go.sum
3
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.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
|
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
|
||||||
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
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.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.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
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.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 h1:C5VurWRRCKjuENsbM6GYVw8W++WVW9rSxoACKIvxzz8=
|
||||||
github.com/ugorji/go/codec v1.2.4/go.mod h1:bWBu1+kIRWcF8uMklKaJrR6fTWQOwAlrIzX22pHwryA=
|
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-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 h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
|
||||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||||
|
|
|
@ -18,6 +18,11 @@ type CryptoConfig struct {
|
||||||
Argon2 Argon2Config
|
Argon2 Argon2Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formatting holds additional parameters used for formatting messages
|
||||||
|
type Formatting struct {
|
||||||
|
ColoredTitle bool `default:"false"`
|
||||||
|
}
|
||||||
|
|
||||||
// Configuration holds values that can be configured by the user.
|
// Configuration holds values that can be configured by the user.
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
Debug bool `default:"false"`
|
Debug bool `default:"false"`
|
||||||
|
@ -42,7 +47,8 @@ type Configuration struct {
|
||||||
Security struct {
|
Security struct {
|
||||||
CheckHIBP bool `default:"false"`
|
CheckHIBP bool `default:"false"`
|
||||||
}
|
}
|
||||||
Crypto CryptoConfig
|
Crypto CryptoConfig
|
||||||
|
Formatting Formatting
|
||||||
}
|
}
|
||||||
|
|
||||||
func configFiles() []string {
|
func configFiles() []string {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
|
"github.com/pushbits/server/internal/configuration"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -16,12 +17,13 @@ type Database interface {
|
||||||
|
|
||||||
// Dispatcher holds information for sending notifications to clients.
|
// Dispatcher holds information for sending notifications to clients.
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
db Database
|
db Database
|
||||||
client *gomatrix.Client
|
client *gomatrix.Client
|
||||||
|
formatting configuration.Formatting
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create instanciates a dispatcher connection.
|
// Create instanciates a dispatcher connection.
|
||||||
func Create(db Database, homeserver, username, password string) (*Dispatcher, error) {
|
func Create(db Database, homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
|
||||||
log.Println("Setting up dispatcher.")
|
log.Println("Setting up dispatcher.")
|
||||||
|
|
||||||
client, err := gomatrix.NewClient(homeserver, "", "")
|
client, err := gomatrix.NewClient(homeserver, "", "")
|
||||||
|
@ -40,7 +42,7 @@ func Create(db Database, homeserver, username, password string) (*Dispatcher, er
|
||||||
|
|
||||||
client.SetCredentials(response.UserID, response.AccessToken)
|
client.SetCredentials(response.UserID, response.AccessToken)
|
||||||
|
|
||||||
return &Dispatcher{client: client}, nil
|
return &Dispatcher{client: client, formatting: formatting}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the dispatcher connection.
|
// Close closes the dispatcher connection.
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
"github.com/pushbits/server/internal/model"
|
"github.com/pushbits/server/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,15 +14,81 @@ import (
|
||||||
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) error {
|
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) error {
|
||||||
log.Printf("Sending notification to room %s.", a.MatrixID)
|
log.Printf("Sending notification to room %s.", a.MatrixID)
|
||||||
|
|
||||||
plainTitle := strings.TrimSpace(n.Title)
|
|
||||||
plainMessage := strings.TrimSpace(n.Message)
|
plainMessage := strings.TrimSpace(n.Message)
|
||||||
escapedTitle := html.EscapeString(plainTitle)
|
plainTitle := strings.TrimSpace(n.Title)
|
||||||
escapedMessage := html.EscapeString(plainMessage)
|
message := d.getFormattedMessage(n)
|
||||||
|
title := d.getFormattedTitle(n)
|
||||||
|
|
||||||
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
|
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
|
||||||
formattedText := fmt.Sprintf("<b>%s</b><br /><br />%s", escapedTitle, escapedMessage)
|
formattedText := fmt.Sprintf("%s %s", title, message)
|
||||||
|
|
||||||
_, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)
|
_, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTML-formats the title
|
||||||
|
func (d *Dispatcher) getFormattedTitle(n *model.Notification) string {
|
||||||
|
trimmedTitle := strings.TrimSpace(n.Title)
|
||||||
|
title := html.EscapeString(trimmedTitle)
|
||||||
|
|
||||||
|
if d.formatting.ColoredTitle {
|
||||||
|
title = d.coloredText(d.priorityToColor(n.Priority), title)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<b>" + title + "</b><br /><br />"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts different syntaxes to a HTML-formatted message
|
||||||
|
func (d *Dispatcher) getFormattedMessage(n *model.Notification) string {
|
||||||
|
trimmedMessage := strings.TrimSpace(n.Message)
|
||||||
|
message := strings.Replace(html.EscapeString(trimmedMessage), "\n", "<br />", -1) // default to text/plain
|
||||||
|
|
||||||
|
if optionsDisplayRaw, ok := n.Extras["client::display"]; ok {
|
||||||
|
optionsDisplay, ok := optionsDisplayRaw.(map[string]interface{})
|
||||||
|
|
||||||
|
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 = strings.Replace(trimmedMessage, "\n", "<br />", -1)
|
||||||
|
case "markdown", "md", "text/md", "text/markdown":
|
||||||
|
// allow HTML in Markdown
|
||||||
|
message = string(markdown.ToHTML([]byte(trimmedMessage), nil, nil))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return message
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maps priorities to hex colors
|
||||||
|
func (d *Dispatcher) priorityToColor(prio int) string {
|
||||||
|
switch {
|
||||||
|
case prio < 0:
|
||||||
|
return "#828282"
|
||||||
|
case prio <= 3: // info - default color
|
||||||
|
return ""
|
||||||
|
case prio <= 10: // low - yellow
|
||||||
|
return "#edd711"
|
||||||
|
case prio <= 20: // mid - orange
|
||||||
|
return "#ed6d11"
|
||||||
|
case prio > 20: // high - red
|
||||||
|
return "#ed1f11"
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maps a priority to a color tag
|
||||||
|
func (d *Dispatcher) coloredText(color string, text string) string {
|
||||||
|
if color == "" {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
return "<font data-mx-color='" + color + "'>" + text + "</font>"
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue