2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-01 18:37:02 +00:00
|
|
|
"database/sql"
|
2018-10-25 13:51:47 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-11-26 12:06:05 +00:00
|
|
|
"regexp"
|
2018-10-25 13:51:47 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/knadh/listmonk/models"
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2018-10-29 09:50:11 +00:00
|
|
|
const (
|
|
|
|
// tplTag is the template tag that should be present in a template
|
|
|
|
// as the placeholder for campaign bodies.
|
|
|
|
tplTag = `{{ template "content" . }}`
|
|
|
|
|
|
|
|
dummyTpl = `
|
2018-10-29 09:50:49 +00:00
|
|
|
<p>Hi there</p>
|
|
|
|
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis et elit ac elit sollicitudin condimentum non a magna. Sed tempor mauris in facilisis vehicula. Aenean nisl urna, accumsan ac tincidunt vitae, interdum cursus massa. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam varius turpis et turpis lacinia placerat. Aenean id ligula a orci lacinia blandit at eu felis. Phasellus vel lobortis lacus. Suspendisse leo elit, luctus sed erat ut, venenatis fermentum ipsum. Donec bibendum neque quis.</p>
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-29 09:50:49 +00:00
|
|
|
<h3>Sub heading</h3>
|
|
|
|
<p>Nam luctus dui non placerat mattis. Morbi non accumsan orci, vel interdum urna. Duis faucibus id nunc ut euismod. Curabitur et eros id erat feugiat fringilla in eget neque. Aliquam accumsan cursus eros sed faucibus.</p>
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-29 09:50:11 +00:00
|
|
|
<p>Here is a link to <a href="https://listmonk.app" target="_blank">listmonk</a>.</p>`
|
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-11-26 12:06:05 +00:00
|
|
|
var (
|
|
|
|
regexpTplTag = regexp.MustCompile(`{{(\s+)?template\s+?"content"(\s+)?\.(\s+)?}}`)
|
|
|
|
)
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// handleGetTemplates handles retrieval of templates.
|
|
|
|
func handleGetTemplates(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
out []models.Template
|
|
|
|
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
single = false
|
|
|
|
noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fetch one list.
|
|
|
|
if id > 0 {
|
|
|
|
single = true
|
|
|
|
}
|
|
|
|
|
|
|
|
err := app.Queries.GetTemplates.Select(&out, id, noBody)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching templates: %s", pqErrMsg(err)))
|
2019-10-25 05:41:47 +00:00
|
|
|
}
|
|
|
|
if single && len(out) == 0 {
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Template not found.")
|
|
|
|
}
|
|
|
|
|
2019-10-25 05:41:47 +00:00
|
|
|
if len(out) == 0 {
|
|
|
|
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
if single {
|
|
|
|
return c.JSON(http.StatusOK, okResp{out[0]})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePreviewTemplate renders the HTML preview of a template.
|
|
|
|
func handlePreviewTemplate(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
2018-10-26 05:48:17 +00:00
|
|
|
body = c.FormValue("body")
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-26 05:48:17 +00:00
|
|
|
tpls []models.Template
|
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
if body != "" {
|
2018-11-26 12:06:05 +00:00
|
|
|
if !regexpTplTag.MatchString(body) {
|
2018-10-31 12:54:21 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
fmt.Sprintf("Template body should contain the %s placeholder exactly once", tplTag))
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-26 05:48:17 +00:00
|
|
|
if id < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := app.Queries.GetTemplates.Select(&tpls, id, false)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching templates: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tpls) == 0 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Template not found.")
|
|
|
|
}
|
|
|
|
body = tpls[0].Body
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the template.
|
2018-11-02 07:49:18 +00:00
|
|
|
camp := models.Campaign{
|
|
|
|
UUID: "00000000-0000-0000-0000-000000000000",
|
|
|
|
Name: "Dummy Campaign",
|
|
|
|
Subject: "Dummy Campaign Subject",
|
|
|
|
FromEmail: "dummy-campaign@listmonk.app",
|
|
|
|
TemplateBody: body,
|
|
|
|
Body: dummyTpl,
|
|
|
|
}
|
|
|
|
|
2018-12-19 06:33:13 +00:00
|
|
|
if err := camp.CompileTemplate(app.Manager.TemplateFuncs(&camp)); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Error compiling template: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the message body.
|
2018-12-19 06:33:13 +00:00
|
|
|
m := app.Manager.NewMessage(&camp, &dummySubscriber)
|
2018-10-31 14:12:07 +00:00
|
|
|
if err := m.Render(); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
fmt.Sprintf("Error rendering message: %v", err))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 14:12:07 +00:00
|
|
|
return c.HTML(http.StatusOK, string(m.Body))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleCreateTemplate handles template creation.
|
|
|
|
func handleCreateTemplate(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
o = models.Template{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := c.Bind(&o); err != nil {
|
2018-12-26 10:08:08 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateTemplate(o); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert and read ID.
|
|
|
|
var newID int
|
|
|
|
if err := app.Queries.CreateTemplate.Get(&newID,
|
|
|
|
o.Name,
|
|
|
|
o.Body); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error template user: %v", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hand over to the GET handler to return the last insertion.
|
|
|
|
c.SetParamNames("id")
|
|
|
|
c.SetParamValues(fmt.Sprintf("%d", newID))
|
|
|
|
return c.JSON(http.StatusOK, handleGetLists(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdateTemplate handles template modification.
|
|
|
|
func handleUpdateTemplate(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var o models.Template
|
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateTemplate(o); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: PASSWORD HASHING.
|
|
|
|
res, err := app.Queries.UpdateTemplate.Exec(o.ID, o.Name, o.Body)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error updating template: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Template not found.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleGetTemplates(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleTemplateSetDefault handles template modification.
|
|
|
|
func handleTemplateSetDefault(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := app.Queries.SetDefaultTemplate.Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error updating template: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleGetTemplates(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDeleteTemplate handles template deletion.
|
|
|
|
func handleDeleteTemplate(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
|
|
|
|
} else if id == 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Cannot delete the primordial template.")
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:37:02 +00:00
|
|
|
var delID int
|
|
|
|
err := app.Queries.DeleteTemplate.Get(&delID, id)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
2018-11-01 18:37:02 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
fmt.Sprintf("Error deleting template: %v", err))
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:37:02 +00:00
|
|
|
if delID == 0 {
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
"Cannot delete the last, default, or non-existent template.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateTemplate validates template fields.
|
|
|
|
func validateTemplate(o models.Template) error {
|
|
|
|
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
|
|
|
|
return errors.New("invalid length for `name`")
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:06:05 +00:00
|
|
|
if !regexpTplTag.MatchString(o.Body) {
|
2018-10-31 12:54:21 +00:00
|
|
|
return fmt.Errorf("template body should contain the %s placeholder exactly once", tplTag)
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|