Refactor and fix obsolete campaign template compilation and validation routines
This commit is contained in:
parent
d62bb97ee1
commit
b4e6ed658a
3 changed files with 18 additions and 40 deletions
12
campaigns.go
12
campaigns.go
|
@ -162,7 +162,7 @@ func handleCreateCampaign(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Validate.
|
||||
if err := validateCampaignFields(o); err != nil {
|
||||
if err := validateCampaignFields(o, app); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
|
@ -235,7 +235,7 @@ func handleUpdateCampaign(c echo.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := validateCampaignFields(o); err != nil {
|
||||
if err := validateCampaignFields(o, app); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
|
@ -430,7 +430,7 @@ func handleTestCampaign(c echo.Context) error {
|
|||
return err
|
||||
}
|
||||
// Validate.
|
||||
if err := validateCampaignFields(req); err != nil {
|
||||
if err := validateCampaignFields(req, app); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
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.
|
||||
func validateCampaignFields(c campaignReq) error {
|
||||
func validateCampaignFields(c campaignReq, app *App) error {
|
||||
if !regexFromAddress.Match([]byte(c.FromEmail)) {
|
||||
if !govalidator.IsEmail(c.FromEmail) {
|
||||
return errors.New("invalid `from_email`")
|
||||
|
@ -522,8 +522,8 @@ func validateCampaignFields(c campaignReq) error {
|
|||
}
|
||||
}
|
||||
|
||||
_, err := runner.CompileMessageTemplate(tplTag, c.Body)
|
||||
if err != nil {
|
||||
camp := models.Campaign{Body: c.Body, TemplateBody: tplTag}
|
||||
if err := c.CompileTemplate(app.Runner.TemplateFuncs(&camp)); err != nil {
|
||||
return fmt.Errorf("Error compiling campaign body: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -324,27 +324,3 @@ func (r *Runner) trackLink(url, campUUID, subUUID string) string {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
22
templates.go
22
templates.go
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
@ -10,7 +9,6 @@ import (
|
|||
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/knadh/listmonk/models"
|
||||
"github.com/knadh/listmonk/runner"
|
||||
"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>`
|
||||
)
|
||||
|
||||
var dummySubscriber = models.Subscriber{
|
||||
Email: "dummy@listmonk.app",
|
||||
Name: "Dummy User",
|
||||
}
|
||||
|
||||
type dummyMessage struct {
|
||||
UnsubscribeURL string
|
||||
}
|
||||
|
@ -99,20 +102,19 @@ func handlePreviewTemplate(c echo.Context) error {
|
|||
}
|
||||
|
||||
// Compile the template.
|
||||
tpl, err := runner.CompileMessageTemplate(body, dummyTpl)
|
||||
if err != nil {
|
||||
camp := models.Campaign{TemplateBody: body, Body: dummyTpl}
|
||||
if err := camp.CompileTemplate(app.Runner.TemplateFuncs(&camp)); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error compiling template: %v", err))
|
||||
}
|
||||
|
||||
// Render the message body.
|
||||
var out = bytes.Buffer{}
|
||||
if err := tpl.ExecuteTemplate(&out,
|
||||
runner.BaseTPL,
|
||||
dummyMessage{UnsubscribeURL: "#dummy"}); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error executing template: %v", err))
|
||||
m := app.Runner.NewMessage(&camp, &dummySubscriber)
|
||||
if err := m.Render(); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest,
|
||||
fmt.Sprintf("Error rendering message: %v", err))
|
||||
}
|
||||
|
||||
return c.HTML(http.StatusOK, out.String())
|
||||
return c.HTML(http.StatusOK, string(m.Body))
|
||||
}
|
||||
|
||||
// handleCreateTemplate handles template creation.
|
||||
|
|
Loading…
Reference in a new issue