Refactor and fix obsolete campaign template compilation and validation routines

This commit is contained in:
Kailash Nadh 2018-10-31 19:42:07 +05:30
parent d62bb97ee1
commit b4e6ed658a
3 changed files with 18 additions and 40 deletions

View File

@ -162,7 +162,7 @@ func handleCreateCampaign(c echo.Context) error {
} }
// Validate. // Validate.
if err := validateCampaignFields(o); err != nil { if err := validateCampaignFields(o, app); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} }
@ -235,7 +235,7 @@ func handleUpdateCampaign(c echo.Context) error {
return err return err
} }
if err := validateCampaignFields(o); err != nil { if err := validateCampaignFields(o, app); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} }
@ -430,7 +430,7 @@ func handleTestCampaign(c echo.Context) error {
return err return err
} }
// Validate. // Validate.
if err := validateCampaignFields(req); err != nil { if err := validateCampaignFields(req, app); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} }
if len(req.SubscriberEmails) == 0 { if len(req.SubscriberEmails) == 0 {
@ -497,7 +497,7 @@ func sendTestMessage(sub *models.Subscriber, camp *models.Campaign, app *App) er
} }
// validateCampaignFields validates incoming campaign field values. // validateCampaignFields validates incoming campaign field values.
func validateCampaignFields(c campaignReq) error { func validateCampaignFields(c campaignReq, app *App) error {
if !regexFromAddress.Match([]byte(c.FromEmail)) { if !regexFromAddress.Match([]byte(c.FromEmail)) {
if !govalidator.IsEmail(c.FromEmail) { if !govalidator.IsEmail(c.FromEmail) {
return errors.New("invalid `from_email`") return errors.New("invalid `from_email`")
@ -522,8 +522,8 @@ func validateCampaignFields(c campaignReq) error {
} }
} }
_, err := runner.CompileMessageTemplate(tplTag, c.Body) camp := models.Campaign{Body: c.Body, TemplateBody: tplTag}
if err != nil { if err := c.CompileTemplate(app.Runner.TemplateFuncs(&camp)); err != nil {
return fmt.Errorf("Error compiling campaign body: %v", err) return fmt.Errorf("Error compiling campaign body: %v", err)
} }

View File

@ -324,27 +324,3 @@ func (r *Runner) trackLink(url, campUUID, subUUID string) string {
return fmt.Sprintf(r.cfg.LinkTrackURL, uu, campUUID, subUUID) return fmt.Sprintf(r.cfg.LinkTrackURL, uu, campUUID, subUUID)
} }
// CompileMessageTemplate takes a base template body string and a child (message) template
// body string, compiles both and inserts the child template as the named template "content"
// and returns the resultant template.
func CompileMessageTemplate(baseBody, childBody string) (*template.Template, error) {
// Compile the base template.
baseTPL, err := template.New(BaseTPL).Parse(baseBody)
if err != nil {
return nil, fmt.Errorf("error compiling base template: %v", err)
}
// Compile the campaign message.
msgTpl, err := template.New(ContentTpl).Parse(childBody)
if err != nil {
return nil, fmt.Errorf("error compiling message: %v", err)
}
out, err := baseTPL.AddParseTree(ContentTpl, msgTpl.Tree)
if err != nil {
return nil, fmt.Errorf("error inserting child template: %v", err)
}
return out, nil
}

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -10,7 +9,6 @@ import (
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
"github.com/knadh/listmonk/models" "github.com/knadh/listmonk/models"
"github.com/knadh/listmonk/runner"
"github.com/labstack/echo" "github.com/labstack/echo"
) )
@ -29,6 +27,11 @@ const (
<p>Here is a link to <a href="https://listmonk.app" target="_blank">listmonk</a>.</p>` <p>Here is a link to <a href="https://listmonk.app" target="_blank">listmonk</a>.</p>`
) )
var dummySubscriber = models.Subscriber{
Email: "dummy@listmonk.app",
Name: "Dummy User",
}
type dummyMessage struct { type dummyMessage struct {
UnsubscribeURL string UnsubscribeURL string
} }
@ -99,20 +102,19 @@ func handlePreviewTemplate(c echo.Context) error {
} }
// Compile the template. // Compile the template.
tpl, err := runner.CompileMessageTemplate(body, dummyTpl) camp := models.Campaign{TemplateBody: body, Body: dummyTpl}
if err != nil { if err := camp.CompileTemplate(app.Runner.TemplateFuncs(&camp)); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error compiling template: %v", err)) return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error compiling template: %v", err))
} }
// Render the message body. // Render the message body.
var out = bytes.Buffer{} m := app.Runner.NewMessage(&camp, &dummySubscriber)
if err := tpl.ExecuteTemplate(&out, if err := m.Render(); err != nil {
runner.BaseTPL, return echo.NewHTTPError(http.StatusBadRequest,
dummyMessage{UnsubscribeURL: "#dummy"}); err != nil { fmt.Sprintf("Error rendering message: %v", err))
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error executing template: %v", err))
} }
return c.HTML(http.StatusOK, out.String()) return c.HTML(http.StatusOK, string(m.Body))
} }
// handleCreateTemplate handles template creation. // handleCreateTemplate handles template creation.