~gjabell/mfn

298287072d83612d77b1acee812a011f045ac5b3 — Galen Abell 1 year, 1 month ago 7e71842
Add HTML option for email and webhook
6 files changed, 88 insertions(+), 34 deletions(-)

M config.go
M email.go
M gotify.go
M notify.go
A template.go
M webhook.go
M config.go => config.go +26 -25
@@ 2,7 2,6 @@ package main

import (
	"fmt"
	"html/template"

	"github.com/BurntSushi/toml"
)


@@ 12,15 11,16 @@ type validator interface {
}

type EmailConfig struct {
	SubjectTemplate    *template.Template `toml:"-"`
	SubjectTemplateStr string             `toml:"subject_template"`
	BodyTemplate       *template.Template `toml:"-"`
	BodyTemplateStr    string             `toml:"body_template"`
	EmailTo            string             `toml:"to"`
	EmailFrom          string             `toml:"from"`
	Username           string             `toml:"username"`
	Password           string             `toml:"password"`
	Server             string             `toml:"server"`
	SubjectTemplate    *Template `toml:"-"`
	SubjectTemplateStr string    `toml:"subject_template"`
	BodyTemplate       *Template `toml:"-"`
	BodyTemplateStr    string    `toml:"body_template"`
	EmailTo            string    `toml:"to"`
	EmailFrom          string    `toml:"from"`
	Username           string    `toml:"username"`
	Password           string    `toml:"password"`
	Server             string    `toml:"server"`
	HTML               bool      `toml:"html"`
}

func (c *EmailConfig) validate() error {


@@ 39,13 39,13 @@ func (c *EmailConfig) validate() error {
	}

	// parse the templates
	subjTmpl, err := template.New("subject_template").Parse(c.SubjectTemplateStr)
	subjTmpl, err := NewTemplate("subject_template", c.HTML).Parse(c.SubjectTemplateStr)
	if err != nil {
		return err
	}
	c.SubjectTemplate = subjTmpl

	bodyTmpl, err := template.New("body_template").Parse(c.BodyTemplateStr)
	bodyTmpl, err := NewTemplate("body_template", c.HTML).Parse(c.BodyTemplateStr)
	if err != nil {
		return err
	}


@@ 55,13 55,13 @@ func (c *EmailConfig) validate() error {
}

type GotifyConfig struct {
	TitleTemplate      *template.Template `toml:"-"`
	TitleTemplateStr   string             `toml:"title_template"`
	MessageTemplate    *template.Template `toml:"-"`
	MessageTemplateStr string             `toml:"message_template"`
	Server             string             `toml:"server"`
	Token              string             `toml:"token"`
	Priority           int                `toml:"priority"`
	TitleTemplate      *Template `toml:"-"`
	TitleTemplateStr   string    `toml:"title_template"`
	MessageTemplate    *Template `toml:"-"`
	MessageTemplateStr string    `toml:"message_template"`
	Server             string    `toml:"server"`
	Token              string    `toml:"token"`
	Priority           int       `toml:"priority"`
}

func (c *GotifyConfig) validate() error {


@@ 77,13 77,13 @@ func (c *GotifyConfig) validate() error {
	}

	// parse the templates
	titleTmpl, err := template.New("title_template").Parse(c.TitleTemplateStr)
	titleTmpl, err := NewTemplate("title_template", false).Parse(c.TitleTemplateStr)
	if err != nil {
		return err
	}
	c.TitleTemplate = titleTmpl

	messageTmpl, err := template.New("message_template").Parse(c.MessageTemplateStr)
	messageTmpl, err := NewTemplate("message_template", false).Parse(c.MessageTemplateStr)
	if err != nil {
		return err
	}


@@ 93,9 93,10 @@ func (c *GotifyConfig) validate() error {
}

type WebhookConfig struct {
	Template    *template.Template `toml:"-"`
	TemplateStr string             `toml:"template"`
	Endpoint    string             `toml:"endpoint"`
	Template    *Template `toml:"-"`
	TemplateStr string    `toml:"template"`
	Endpoint    string    `toml:"endpoint"`
	HTML        bool      `toml:"html"`
}

func (c *WebhookConfig) validate() error {


@@ 109,7 110,7 @@ func (c *WebhookConfig) validate() error {
	}

	// parse the template
	tmpl, err := template.New("webhook").Parse(c.TemplateStr)
	tmpl, err := NewTemplate("webhook", c.HTML).Parse(c.TemplateStr)
	if err != nil {
		return err
	}

M email.go => email.go +2 -3
@@ 2,7 2,6 @@ package main

import (
	"bytes"
	"html/template"
	"net/mail"
	"strings"



@@ 12,8 11,8 @@ import (
)

type Email struct {
	subjectTemplate *template.Template
	bodyTemplate    *template.Template
	subjectTemplate *Template
	bodyTemplate    *Template
	emailTo         string
	emailFrom       string
	username        string

M gotify.go => gotify.go +2 -3
@@ 2,7 2,6 @@ package main

import (
	"bytes"
	"html/template"
	"net/http"
	"net/url"
	"strconv"


@@ 11,8 10,8 @@ import (
)

type Gotify struct {
	titleTemplate   *template.Template
	messageTemplate *template.Template
	titleTemplate   *Template
	messageTemplate *Template
	server          string
	token           string
	priority        int

M notify.go => notify.go +1 -1
@@ 20,7 20,7 @@ func Test(n Notifier) error {
		Title:      "Test Entry",
		URL:        "https://miniflux.app",
		Date:       time.Now(),
		Content:    "This is a test miniflux entry",
		Content:    "<p>This is a test miniflux entry</p>",
		Author:     "Author",
		Starred:    false,
		Enclosures: make([]*mf.Enclosure, 0),

A template.go => template.go +56 -0
@@ 0,0 1,56 @@
package main

import (
	html "html/template"
	"io"
	text "text/template"
)

// Templater delegates calls to either text.Template or html.Template
// type Templater interface {
// 	Parse(string) (*Template, error)
// 	Execute(io.Writer, interface{}) error
// }

// Template is a wrapper around text and html templates
type Template struct {
	name         string
	html         bool
	textTemplate *text.Template
	htmlTemplate *html.Template
}

func NewTemplate(name string, html bool) *Template {
	return &Template{
		name: name,
		html: html,
	}
}

func (t *Template) Parse(template string) (*Template, error) {
	if t.html {
		tmpl, err := text.New(t.name).Parse(template)
		if err != nil {
			return nil, err
		}

		t.textTemplate = tmpl
		return t, nil
	}

	tmpl, err := html.New(t.name).Parse(template)
	if err != nil {
		return nil, err
	}

	t.htmlTemplate = tmpl
	return t, nil
}

func (t *Template) Execute(wr io.Writer, data interface{}) error {
	if t.html {
		return t.textTemplate.Execute(wr, data)
	}

	return t.htmlTemplate.Execute(wr, data)
}

M webhook.go => webhook.go +1 -2
@@ 3,7 3,6 @@ package main
import (
	"bytes"
	"encoding/json"
	"html/template"
	"io/ioutil"
	"log"
	"net/http"


@@ 12,7 11,7 @@ import (
)

type Webhook struct {
	template *template.Template
	template *Template
	endpoint string
}