adopt to review

change formatting options to separate struct and clean up
This commit is contained in:
Cubicroot 2021-05-02 15:37:54 +02:00
parent fe1cbdf79e
commit 2ff9f5972a
6 changed files with 22 additions and 20 deletions

View file

@ -116,19 +116,19 @@ pbcli application show $PB_APPLICATION --url https://pushbits.example.com --user
### Message options ### Message options
Messages are supporting thre different syntaxes: Messages are supporting three different syntaxes:
* text/plain * text/plain
* text/html * text/html
* text/markdown * 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 ```bash
curl \ curl \
--header "Content-Type: application/json" \ --header "Content-Type: application/json" \
--request POST \ --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" "https://pushbits.example.com/message?token=$PB_TOKEN"
``` ```

View file

@ -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, c.Message) 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)
} }

View file

@ -57,6 +57,6 @@ crypto:
saltlength: 16 saltlength: 16
keylength: 32 keylength: 32
message: formatting:
# add coloring to the title according to syslog priorities # Whether to use colored titles based on the message priority.
# coloredtitle: true coloredtitle: false

View file

@ -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,8 +47,8 @@ type Configuration struct {
Security struct { Security struct {
CheckHIBP bool `default:"false"` CheckHIBP bool `default:"false"`
} }
Crypto CryptoConfig Crypto CryptoConfig
Message map[string]interface{} Formatting Formatting
} }
func configFiles() []string { func configFiles() []string {

View file

@ -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,13 +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
settings map[string]interface{} formatting configuration.Formatting
} }
// Create instanciates a dispatcher connection. // 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.") log.Println("Setting up dispatcher.")
client, err := gomatrix.NewClient(homeserver, "", "") 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) 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. // Close closes the dispatcher connection.

View file

@ -32,12 +32,8 @@ func (d *Dispatcher) getFormattedTitle(n *model.Notification) string {
trimmedTitle := strings.TrimSpace(n.Title) trimmedTitle := strings.TrimSpace(n.Title)
title := html.EscapeString(trimmedTitle) title := html.EscapeString(trimmedTitle)
if valueRaw, ok := d.settings["coloredtitle"]; ok { if d.formatting.ColoredTitle {
value, ok := valueRaw.(bool) title = d.coloredText(d.priorityToColor(n.Priority), title)
if ok && value {
title = d.coloredText(d.priorityToColor(n.Priority), title)
}
} }
return "<b>" + title + "</b><br /><br />" return "<b>" + title + "</b><br /><br />"