From 2ff9f5972a22cf95c7b46268594586df79ead190 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Sun, 2 May 2021 15:37:54 +0200 Subject: [PATCH] adopt to review change formatting options to separate struct and clean up --- README.md | 6 +++--- cmd/pushbits/main.go | 2 +- config.example.yml | 6 +++--- internal/configuration/configuration.go | 9 +++++++-- internal/dispatcher/dispatcher.go | 11 ++++++----- internal/dispatcher/notification.go | 8 ++------ 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index bb2f704..d72ead4 100644 --- a/README.md +++ b/README.md @@ -116,19 +116,19 @@ pbcli application show $PB_APPLICATION --url https://pushbits.example.com --user ### Message options -Messages are supporting thre different syntaxes: +Messages are supporting three different syntaxes: * text/plain * text/html * text/markdown -To set a specific syntax you need to set the `extras`: +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/html"}}}' \ + --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" ``` diff --git a/cmd/pushbits/main.go b/cmd/pushbits/main.go index 89d9f87..424816a 100644 --- a/cmd/pushbits/main.go +++ b/cmd/pushbits/main.go @@ -47,7 +47,7 @@ func main() { log.Fatal(err) } - dp, err := dispatcher.Create(db, c.Matrix.Homeserver, c.Matrix.Username, c.Matrix.Password, c.Message) + dp, err := dispatcher.Create(db, c.Matrix.Homeserver, c.Matrix.Username, c.Matrix.Password, c.Formatting) if err != nil { log.Fatal(err) } diff --git a/config.example.yml b/config.example.yml index 057489d..46f3cb1 100644 --- a/config.example.yml +++ b/config.example.yml @@ -57,6 +57,6 @@ crypto: saltlength: 16 keylength: 32 -message: - # add coloring to the title according to syslog priorities - # coloredtitle: true +formatting: + # Whether to use colored titles based on the message priority. + coloredtitle: false diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index 496036b..dc3dc9e 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -18,6 +18,11 @@ type CryptoConfig struct { 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. type Configuration struct { Debug bool `default:"false"` @@ -42,8 +47,8 @@ type Configuration struct { Security struct { CheckHIBP bool `default:"false"` } - Crypto CryptoConfig - Message map[string]interface{} + Crypto CryptoConfig + Formatting Formatting } func configFiles() []string { diff --git a/internal/dispatcher/dispatcher.go b/internal/dispatcher/dispatcher.go index c4c7e5e..a9128ff 100644 --- a/internal/dispatcher/dispatcher.go +++ b/internal/dispatcher/dispatcher.go @@ -4,6 +4,7 @@ import ( "log" "github.com/matrix-org/gomatrix" + "github.com/pushbits/server/internal/configuration" ) var ( @@ -16,13 +17,13 @@ type Database interface { // Dispatcher holds information for sending notifications to clients. type Dispatcher struct { - db Database - client *gomatrix.Client - settings map[string]interface{} + db Database + client *gomatrix.Client + formatting configuration.Formatting } // Create instanciates a dispatcher connection. -func Create(db Database, homeserver, username, password string, settings map[string]interface{}) (*Dispatcher, error) { +func Create(db Database, homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) { log.Println("Setting up dispatcher.") client, err := gomatrix.NewClient(homeserver, "", "") @@ -41,7 +42,7 @@ func Create(db Database, homeserver, username, password string, settings map[str client.SetCredentials(response.UserID, response.AccessToken) - return &Dispatcher{client: client, settings: settings}, nil + return &Dispatcher{client: client, formatting: formatting}, nil } // Close closes the dispatcher connection. diff --git a/internal/dispatcher/notification.go b/internal/dispatcher/notification.go index 1ddaeb0..b81a332 100644 --- a/internal/dispatcher/notification.go +++ b/internal/dispatcher/notification.go @@ -32,12 +32,8 @@ func (d *Dispatcher) getFormattedTitle(n *model.Notification) string { trimmedTitle := strings.TrimSpace(n.Title) title := html.EscapeString(trimmedTitle) - if valueRaw, ok := d.settings["coloredtitle"]; ok { - value, ok := valueRaw.(bool) - - if ok && value { - title = d.coloredText(d.priorityToColor(n.Priority), title) - } + if d.formatting.ColoredTitle { + title = d.coloredText(d.priorityToColor(n.Priority), title) } return "" + title + "

"