Skip to content

Commit

Permalink
Refactor campaign preview to use dummy campaign and subscriber.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
knadh committed Apr 21, 2021
1 parent fe61e89 commit 3782413
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
19 changes: 4 additions & 15 deletions cmd/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,17 @@ 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,
app.i18n.Ts("templates.errorCompiling", "error", err.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,
Expand Down
4 changes: 2 additions & 2 deletions cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit 3782413

Please sign in to comment.