2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-03 07:48:26 +00:00
|
|
|
"bytes"
|
2018-10-25 13:51:47 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-02-03 07:48:26 +00:00
|
|
|
"html/template"
|
2018-10-25 13:51:47 +00:00
|
|
|
"net/http"
|
2020-02-03 07:48:26 +00:00
|
|
|
"net/url"
|
2018-10-25 13:51:47 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2018-10-29 09:50:49 +00:00
|
|
|
"strings"
|
2018-10-25 13:51:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/knadh/listmonk/models"
|
2021-12-09 15:21:07 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2021-05-09 10:06:31 +00:00
|
|
|
// campaignReq is a wrapper over the Campaign model for receiving
|
2022-02-28 13:19:50 +00:00
|
|
|
// campaign creation and update data from APIs.
|
2018-10-25 13:51:47 +00:00
|
|
|
type campaignReq struct {
|
|
|
|
models.Campaign
|
2019-03-30 07:01:24 +00:00
|
|
|
|
2019-08-26 18:15:18 +00:00
|
|
|
// Indicates if the "send_at" date should be written or set to null.
|
2022-04-03 15:24:40 +00:00
|
|
|
SendLater bool `json:"send_later"`
|
2019-08-26 18:15:18 +00:00
|
|
|
|
2019-03-30 07:01:24 +00:00
|
|
|
// This overrides Campaign.Lists to receive and
|
|
|
|
// write a list of int IDs during creation and updation.
|
|
|
|
// Campaign.Lists is JSONText for sending lists children
|
|
|
|
// to the outside world.
|
2022-04-03 15:24:40 +00:00
|
|
|
ListIDs []int `json:"lists"`
|
2018-10-29 09:50:49 +00:00
|
|
|
|
|
|
|
// This is only relevant to campaign test requests.
|
|
|
|
SubscriberEmails pq.StringArray `json:"subscribers"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:06:31 +00:00
|
|
|
// campaignContentReq wraps params coming from API requests for converting
|
|
|
|
// campaign content formats.
|
|
|
|
type campaignContentReq struct {
|
|
|
|
models.Campaign
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
}
|
|
|
|
|
2019-03-28 11:47:51 +00:00
|
|
|
var (
|
2022-04-03 15:24:40 +00:00
|
|
|
regexFromAddress = regexp.MustCompile(`(.+?)\s<(.+?)@(.+?)>`)
|
2019-03-28 11:47:51 +00:00
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// handleGetCampaigns handles retrieval of campaigns.
|
|
|
|
func handleGetCampaigns(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2021-06-05 06:31:33 +00:00
|
|
|
pg = getPagination(c.QueryParams(), 20)
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-03-28 11:47:51 +00:00
|
|
|
status = c.QueryParams()["status"]
|
|
|
|
query = strings.TrimSpace(c.FormValue("query"))
|
2020-10-24 14:30:29 +00:00
|
|
|
orderBy = c.FormValue("order_by")
|
|
|
|
order = c.FormValue("order")
|
2018-10-25 13:51:47 +00:00
|
|
|
noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
|
|
|
|
)
|
|
|
|
|
2022-05-01 09:46:52 +00:00
|
|
|
res, total, err := app.core.QueryCampaigns(query, status, orderBy, order, pg.Offset, pg.Limit)
|
2022-04-03 15:24:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2020-10-24 14:30:29 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if noBody {
|
|
|
|
for i := 0; i < len(res); i++ {
|
|
|
|
res[i].Body = ""
|
|
|
|
}
|
2019-10-25 05:41:47 +00:00
|
|
|
}
|
2022-04-03 15:24:40 +00:00
|
|
|
|
|
|
|
var out models.PageResults
|
|
|
|
if len(res) == 0 {
|
2019-12-06 16:09:18 +00:00
|
|
|
out.Results = []models.Campaign{}
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// Meta.
|
2022-05-01 09:46:52 +00:00
|
|
|
out.Query = query
|
2022-04-03 15:24:40 +00:00
|
|
|
out.Results = res
|
2022-05-01 09:46:52 +00:00
|
|
|
out.Total = total
|
2022-04-03 15:24:40 +00:00
|
|
|
out.Page = pg.Page
|
|
|
|
out.PerPage = pg.PerPage
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// handleGetCampaign handles retrieval of campaigns.
|
|
|
|
func handleGetCampaign(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
|
|
|
|
)
|
2019-04-01 11:37:24 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.GetCampaign(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if noBody {
|
|
|
|
out.Body = ""
|
|
|
|
}
|
2019-03-28 11:47:51 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
2021-01-30 09:29:21 +00:00
|
|
|
// handlePreviewCampaign renders the HTML preview of a campaign body.
|
2018-10-25 13:51:47 +00:00
|
|
|
func handlePreviewCampaign(c echo.Context) error {
|
|
|
|
var (
|
2021-10-31 06:19:43 +00:00
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
tplID, _ = strconv.Atoi(c.FormValue("template_id"))
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
camp, err := app.core.GetCampaignForPreview(id, tplID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 10:43:43 +00:00
|
|
|
// There's a body in the request to preview instead of the body in the DB.
|
|
|
|
if c.Request().Method == http.MethodPost {
|
|
|
|
camp.ContentType = c.FormValue("content_type")
|
|
|
|
camp.Body = c.FormValue("body")
|
|
|
|
}
|
|
|
|
|
2021-04-21 10:02:05 +00:00
|
|
|
// Use a dummy campaign ID to prevent views and clicks from {{ TrackView }}
|
|
|
|
// and {{ TrackLink }} being registered on preview.
|
|
|
|
camp.UUID = dummySubscriber.UUID
|
2021-04-11 10:43:43 +00:00
|
|
|
if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error compiling template: %v", err)
|
2018-10-29 09:50:49 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("templates.errorCompiling", "error", err.Error()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the message body.
|
2021-05-21 18:05:08 +00:00
|
|
|
msg, err := app.manager.NewCampaignMessage(&camp, dummySubscriber)
|
|
|
|
if err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error rendering message: %v", err)
|
2018-10-29 09:50:49 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("templates.errorRendering", "error", err.Error()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 03:51:39 +00:00
|
|
|
if camp.ContentType == models.CampaignContentTypePlain {
|
|
|
|
return c.String(http.StatusOK, string(msg.Body()))
|
|
|
|
}
|
|
|
|
|
2021-05-21 18:05:08 +00:00
|
|
|
return c.HTML(http.StatusOK, string(msg.Body()))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:06:31 +00:00
|
|
|
// handleCampaignContent handles campaign content (body) format conversions.
|
|
|
|
func handleCampaignContent(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
|
|
|
}
|
|
|
|
|
|
|
|
var camp campaignContentReq
|
|
|
|
if err := c.Bind(&camp); err != nil {
|
2021-01-30 09:29:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-09 10:06:31 +00:00
|
|
|
out, err := camp.ConvertContent(camp.From, camp.To)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2021-01-30 09:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// handleCreateCampaign handles campaign creation.
|
|
|
|
// Newly created campaigns are always drafts.
|
|
|
|
func handleCreateCampaign(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
o campaignReq
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:48:26 +00:00
|
|
|
// If the campaign's 'opt-in', prepare a default message.
|
|
|
|
if o.Type == models.CampaignTypeOptin {
|
|
|
|
op, err := makeOptinCampaignMessage(o, app)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o = op
|
2021-09-26 11:13:10 +00:00
|
|
|
} else if o.Type == "" {
|
|
|
|
o.Type = models.CampaignTypeRegular
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ContentType == "" {
|
|
|
|
o.ContentType = models.CampaignContentTypeRichtext
|
|
|
|
}
|
|
|
|
if o.Messenger == "" {
|
|
|
|
o.Messenger = "email"
|
2020-02-03 07:48:26 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// Validate.
|
2020-02-03 07:48:26 +00:00
|
|
|
if c, err := validateCampaignFields(o, app); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
2020-02-03 07:48:26 +00:00
|
|
|
} else {
|
|
|
|
o = c
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.CreateCampaign(o.Campaign, o.ListIDs)
|
2020-03-07 15:07:48 +00:00
|
|
|
if err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdateCampaign handles campaign modification.
|
|
|
|
// Campaigns that are done cannot be modified.
|
|
|
|
func handleUpdateCampaign(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
cm, err := app.core.GetCampaign(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isCampaignalMutable(cm.Status) {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.cantUpdate"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 09:29:21 +00:00
|
|
|
// Read the incoming params into the existing campaign fields from the DB.
|
2022-02-28 13:19:50 +00:00
|
|
|
// This allows updating of values that have been sent whereas fields
|
2021-01-30 09:29:21 +00:00
|
|
|
// that are not in the request retain the old values.
|
|
|
|
o := campaignReq{Campaign: cm}
|
2018-10-25 13:51:47 +00:00
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:48:26 +00:00
|
|
|
if c, err := validateCampaignFields(o, app); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
2020-02-03 07:48:26 +00:00
|
|
|
} else {
|
|
|
|
o = c
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.UpdateCampaign(id, o.Campaign, o.ListIDs, o.SendLater)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdateCampaignStatus handles campaign status modification.
|
|
|
|
func handleUpdateCampaignStatus(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
var o struct {
|
|
|
|
Status string `json:"status"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.UpdateCampaignStatus(id, o.Status)
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleDeleteCampaign handles campaign deletion.
|
|
|
|
// Only scheduled campaigns that have not started yet can be deleted.
|
|
|
|
func handleDeleteCampaign(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if err := app.core.DeleteCampaign(id); err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetRunningCampaignStats returns stats of a given set of campaign IDs.
|
|
|
|
func handleGetRunningCampaignStats(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
)
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.GetRunningCampaignStats()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if len(out) == 0 {
|
2018-10-25 13:51:47 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute rate.
|
|
|
|
for i, c := range out {
|
|
|
|
if c.Started.Valid && c.UpdatedAt.Valid {
|
2022-02-06 06:08:02 +00:00
|
|
|
diff := int(c.UpdatedAt.Time.Sub(c.Started.Time).Minutes())
|
|
|
|
if diff < 1 {
|
|
|
|
diff = 1
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2022-02-06 06:08:02 +00:00
|
|
|
|
|
|
|
rate := c.Sent / diff
|
|
|
|
if rate > c.Sent || rate > c.ToSend {
|
|
|
|
rate = c.Sent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rate since the starting of the campaign.
|
|
|
|
out[i].NetRate = rate
|
|
|
|
|
|
|
|
// Realtime running rate over the last minute.
|
|
|
|
out[i].Rate = app.manager.GetCampaignStats(c.ID).SendRate
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
2018-10-29 09:50:49 +00:00
|
|
|
// handleTestCampaign handles the sending of a campaign message to
|
|
|
|
// arbitrary subscribers for testing.
|
|
|
|
func handleTestCampaign(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
campID, _ = strconv.Atoi(c.Param("id"))
|
2021-11-07 05:04:37 +00:00
|
|
|
tplID, _ = strconv.Atoi(c.FormValue("template_id"))
|
2018-10-29 09:50:49 +00:00
|
|
|
req campaignReq
|
|
|
|
)
|
|
|
|
|
|
|
|
if campID < 1 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.errorID"))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get and validate fields.
|
|
|
|
if err := c.Bind(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-20 11:01:24 +00:00
|
|
|
|
2018-10-29 09:50:49 +00:00
|
|
|
// Validate.
|
2020-02-03 07:48:26 +00:00
|
|
|
if c, err := validateCampaignFields(req, app); err != nil {
|
2018-10-29 09:50:49 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
2020-02-03 07:48:26 +00:00
|
|
|
} else {
|
|
|
|
req = c
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
if len(req.SubscriberEmails) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.noSubsToTest"))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the subscribers.
|
|
|
|
for i := 0; i < len(req.SubscriberEmails); i++ {
|
|
|
|
req.SubscriberEmails[i] = strings.ToLower(strings.TrimSpace(req.SubscriberEmails[i]))
|
|
|
|
}
|
2022-04-03 15:24:40 +00:00
|
|
|
|
|
|
|
subs, err := app.core.GetSubscribersByEmail(req.SubscriberEmails)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The campaign.
|
2022-04-03 15:24:40 +00:00
|
|
|
camp, err := app.core.GetCampaignForPreview(campID, tplID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 09:29:21 +00:00
|
|
|
// Override certain values from the DB with incoming values.
|
2018-10-29 09:50:49 +00:00
|
|
|
camp.Name = req.Name
|
|
|
|
camp.Subject = req.Subject
|
|
|
|
camp.FromEmail = req.FromEmail
|
|
|
|
camp.Body = req.Body
|
2021-01-30 09:29:21 +00:00
|
|
|
camp.AltBody = req.AltBody
|
2020-09-20 11:01:24 +00:00
|
|
|
camp.Messenger = req.Messenger
|
|
|
|
camp.ContentType = req.ContentType
|
2022-01-04 16:46:21 +00:00
|
|
|
camp.Headers = req.Headers
|
2020-09-20 11:01:24 +00:00
|
|
|
camp.TemplateID = req.TemplateID
|
2018-10-29 09:50:49 +00:00
|
|
|
|
|
|
|
// Send the test messages.
|
|
|
|
for _, s := range subs {
|
2019-10-25 05:41:47 +00:00
|
|
|
sub := s
|
2020-03-08 05:37:24 +00:00
|
|
|
if err := sendTestMessage(sub, &camp, app); err != nil {
|
2021-01-28 18:00:39 +00:00
|
|
|
app.log.Printf("error sending test message: %v", err)
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("campaigns.errorSendTest", "error", err.Error()))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|
|
|
|
|
2021-09-11 07:27:55 +00:00
|
|
|
// handleGetCampaignViewAnalytics retrieves view counts for a campaign.
|
|
|
|
func handleGetCampaignViewAnalytics(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
|
|
|
|
typ = c.Param("type")
|
|
|
|
from = c.QueryParams().Get("from")
|
|
|
|
to = c.QueryParams().Get("to")
|
|
|
|
)
|
|
|
|
|
|
|
|
ids, err := parseStringIDs(c.Request().URL.Query()["id"])
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
app.i18n.Ts("globals.messages.errorInvalidIDs", "error", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
|
|
|
app.i18n.Ts("globals.messages.missingFields", "name", "`id`"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strHasLen(from, 10, 30) || !strHasLen(to, 10, 30) {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("analytics.invalidDates"))
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// Campaign link stats.
|
|
|
|
if typ == "links" {
|
|
|
|
out, err := app.core.GetCampaignAnalyticsLinks(ids, typ, from, to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
|
|
|
// View, click, bounce stats.
|
|
|
|
out, err := app.core.GetCampaignAnalyticsCounts(ids, typ, from, to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-11 07:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:37:00 +00:00
|
|
|
// sendTestMessage takes a campaign and a subscriber and sends out a sample campaign message.
|
2020-03-08 05:37:24 +00:00
|
|
|
func sendTestMessage(sub models.Subscriber, camp *models.Campaign, app *App) error {
|
2020-03-07 18:33:22 +00:00
|
|
|
if err := camp.CompileTemplate(app.manager.TemplateFuncs(camp)); err != nil {
|
|
|
|
app.log.Printf("error compiling template: %v", err)
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("templates.errorCompiling", "error", err.Error()))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:05:08 +00:00
|
|
|
// Create a sample campaign message.
|
|
|
|
msg, err := app.manager.NewCampaignMessage(camp, sub)
|
|
|
|
if err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error rendering message: %v", err)
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusNotFound,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("templates.errorRendering", "error", err.Error()))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 18:05:08 +00:00
|
|
|
return app.manager.PushCampaignMessage(msg)
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// validateCampaignFields validates incoming campaign field values.
|
2020-02-03 07:48:26 +00:00
|
|
|
func validateCampaignFields(c campaignReq, app *App) (campaignReq, error) {
|
|
|
|
if c.FromEmail == "" {
|
2020-03-07 18:33:22 +00:00
|
|
|
c.FromEmail = app.constants.FromEmail
|
2020-02-03 07:48:26 +00:00
|
|
|
} else if !regexFromAddress.Match([]byte(c.FromEmail)) {
|
2021-09-25 07:27:55 +00:00
|
|
|
if _, err := app.importer.SanitizeEmail(c.FromEmail); err != nil {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c, errors.New(app.i18n.T("campaigns.fieldInvalidFromEmail"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:33:38 +00:00
|
|
|
if !strHasLen(c.Name, 1, stdInputMaxLen) {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c, errors.New(app.i18n.T("campaigns.fieldInvalidName"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2020-03-08 07:33:38 +00:00
|
|
|
if !strHasLen(c.Subject, 1, stdInputMaxLen) {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c, errors.New(app.i18n.T("campaigns.fieldInvalidSubject"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:33:38 +00:00
|
|
|
// if !hasLen(c.Body, 1, bodyMaxLen) {
|
2020-02-03 07:48:26 +00:00
|
|
|
// return c,errors.New("invalid length for `body`")
|
2018-10-25 13:51:47 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// If there's a "send_at" date, it should be in the future.
|
|
|
|
if c.SendAt.Valid {
|
|
|
|
if c.SendAt.Time.Before(time.Now()) {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c, errors.New(app.i18n.T("campaigns.fieldInvalidSendAt"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 18:03:55 +00:00
|
|
|
if len(c.ListIDs) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return c, errors.New(app.i18n.T("campaigns.fieldInvalidListIDs"))
|
2020-06-06 18:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 11:01:24 +00:00
|
|
|
if !app.manager.HasMessenger(c.Messenger) {
|
2021-01-23 13:24:33 +00:00
|
|
|
return c, errors.New(app.i18n.Ts("campaigns.fieldInvalidMessenger", "name", c.Messenger))
|
2020-09-20 11:01:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 14:12:07 +00:00
|
|
|
camp := models.Campaign{Body: c.Body, TemplateBody: tplTag}
|
2020-03-07 18:33:22 +00:00
|
|
|
if err := c.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
|
2021-01-23 13:24:33 +00:00
|
|
|
return c, errors.New(app.i18n.Ts("campaigns.fieldInvalidBody", "error", err.Error()))
|
2018-10-29 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 16:46:21 +00:00
|
|
|
if len(c.Headers) == 0 {
|
|
|
|
c.Headers = make([]map[string]string, 0)
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:48:26 +00:00
|
|
|
return c, nil
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isCampaignalMutable tells if a campaign's in a state where it's
|
|
|
|
// properties can be mutated.
|
|
|
|
func isCampaignalMutable(status string) bool {
|
|
|
|
return status == models.CampaignStatusRunning ||
|
|
|
|
status == models.CampaignStatusCancelled ||
|
|
|
|
status == models.CampaignStatusFinished
|
|
|
|
}
|
2020-02-03 07:48:26 +00:00
|
|
|
|
|
|
|
// makeOptinCampaignMessage makes a default opt-in campaign message body.
|
|
|
|
func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
|
|
|
|
if len(o.ListIDs) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return o, echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.fieldInvalidListIDs"))
|
2020-02-03 07:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch double opt-in lists from the given list IDs.
|
2022-04-03 15:24:40 +00:00
|
|
|
lists, err := app.core.GetListsByOptin(o.ListIDs, models.ListOptinDouble)
|
2020-02-03 07:48:26 +00:00
|
|
|
if err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
return o, err
|
2020-02-03 07:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No opt-in lists.
|
|
|
|
if len(lists) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return o, echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.noOptinLists"))
|
2020-02-03 07:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the opt-in URL with list IDs.
|
2020-08-01 10:01:13 +00:00
|
|
|
listIDs := url.Values{}
|
2020-02-03 07:48:26 +00:00
|
|
|
for _, l := range lists {
|
|
|
|
listIDs.Add("l", l.UUID)
|
|
|
|
}
|
|
|
|
// optinURLFunc := template.URL("{{ OptinURL }}?" + listIDs.Encode())
|
|
|
|
optinURLAttr := template.HTMLAttr(fmt.Sprintf(`href="{{ OptinURL }}%s"`, listIDs.Encode()))
|
|
|
|
|
|
|
|
// Prepare sample opt-in message for the campaign.
|
|
|
|
var b bytes.Buffer
|
2021-10-28 14:39:06 +00:00
|
|
|
if err := app.notifTpls.tpls.ExecuteTemplate(&b, "optin-campaign", struct {
|
2020-02-03 07:48:26 +00:00
|
|
|
Lists []models.List
|
|
|
|
OptinURLAttr template.HTMLAttr
|
|
|
|
}{lists, optinURLAttr}); err != nil {
|
2020-03-07 18:33:22 +00:00
|
|
|
app.log.Printf("error compiling 'optin-campaign' template: %v", err)
|
2020-12-19 10:55:52 +00:00
|
|
|
return o, echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("templates.errorCompiling", "error", err.Error()))
|
2020-02-03 07:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o.Body = b.String()
|
|
|
|
return o, nil
|
|
|
|
}
|