From 37824136c07a140af405ea071dbf5ea24d8d5bbf Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Wed, 21 Apr 2021 15:32:05 +0530 Subject: [PATCH] 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. --- cmd/campaigns.go | 19 ++++--------------- cmd/subscribers.go | 4 ++-- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/cmd/campaigns.go b/cmd/campaigns.go index 3a03aed..0e606bb 100644 --- a/cmd/campaigns.go +++ b/cmd/campaigns.go @@ -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, diff --git a/cmd/subscribers.go b/cmd/subscribers.go index 20d214c..ce363c3 100644 --- a/cmd/subscribers.go +++ b/cmd/subscribers.go @@ -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, }