Refactor campaign preview to use dummy campaign and subscriber.

Use a dummy subscriber instead of fetching a random one from the
DB. In addition, replace the preview campaign UUID with a dummy
one to prevent clicks and views being registered against the
campaign when previewing.
This commit is contained in:
Kailash Nadh 2021-04-21 15:32:05 +05:30
parent fe61e898a3
commit 37824136c0
2 changed files with 6 additions and 17 deletions

View File

@ -181,20 +181,9 @@ func handlePreviewCampaign(c echo.Context) error {
camp.Body = c.FormValue("body")
}
var sub models.Subscriber
// Get a random subscriber from the campaign.
if err := app.queries.GetOneCampaignSubscriber.Get(&sub, camp.ID); err != nil {
if err == sql.ErrNoRows {
// There's no subscriber. Mock one.
sub = dummySubscriber
} else {
app.log.Printf("error fetching subscriber: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching",
"name", "{globals.terms.subscriber}", "error", pqErrMsg(err)))
}
}
// Use a dummy campaign ID to prevent views and clicks from {{ TrackView }}
// and {{ TrackLink }} being registered on preview.
camp.UUID = dummySubscriber.UUID
if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
app.log.Printf("error compiling template: %v", err)
return echo.NewHTTPError(http.StatusBadRequest,
@ -202,7 +191,7 @@ func handlePreviewCampaign(c echo.Context) error {
}
// Render the message body.
m := app.manager.NewCampaignMessage(&camp, sub)
m := app.manager.NewCampaignMessage(&camp, dummySubscriber)
if err := m.Render(); err != nil {
app.log.Printf("error rendering message: %v", err)
return echo.NewHTTPError(http.StatusBadRequest,

View File

@ -69,8 +69,8 @@ type subOptin struct {
var (
dummySubscriber = models.Subscriber{
Email: "dummy@listmonk.app",
Name: "Dummy Subscriber",
Email: "demo@listmonk.app",
Name: "Demo Subscriber",
UUID: dummyUUID,
}