Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Refactor and fix obsolete campaign template compilation and validation routines

Kailash Nadhb4e6ed658a4e
diff --git a/campaigns.go b/campaigns.go
index c05ed036..7fa42a23 100644
--- a/campaigns.go
+++ b/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)
}
diff --git a/runner/runner.go b/runner/runner.go
index 914ff048..8a48ebaf 100644
--- a/runner/runner.go
+++ b/runner/runner.go
@@ -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
-}
diff --git a/templates.go b/templates.go
index 9c724bcb..8273a3c7 100644
--- a/templates.go
+++ b/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: "[email protected]",
+ 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.