From ca032c89d6c56b5a47030d0856f20b50691fd9f5 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 8 Mar 2020 15:02:20 +0530 Subject: [PATCH] Refactor manager.CampaignMessage's exposed fields --- campaigns.go | 4 ++-- internal/manager/manager.go | 13 ++++++++++--- templates.go | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/campaigns.go b/campaigns.go index 0a6c437..5c31c63 100644 --- a/campaigns.go +++ b/campaigns.go @@ -187,7 +187,7 @@ func handlePreviewCampaign(c echo.Context) error { fmt.Sprintf("Error rendering message: %v", err)) } - return c.HTML(http.StatusOK, string(m.Body)) + return c.HTML(http.StatusOK, string(m.Body())) } // handleCreateCampaign handles campaign creation. @@ -562,7 +562,7 @@ func sendTestMessage(sub models.Subscriber, camp *models.Campaign, app *App) err fmt.Sprintf("Error rendering message: %v", err)) } - if err := app.messenger.Push(camp.FromEmail, []string{sub.Email}, camp.Subject, m.Body, nil); err != nil { + if err := app.messenger.Push(camp.FromEmail, []string{sub.Email}, camp.Subject, m.Body(), nil); err != nil { return err } diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 6a8d070..930ac7a 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -65,10 +65,10 @@ type Manager struct { type CampaignMessage struct { Campaign *models.Campaign Subscriber models.Subscriber - Body []byte from string to string + body []byte unsubURL string } @@ -263,7 +263,7 @@ func (m *Manager) SpawnWorkers() { } err := m.messengers[msg.Campaign.MessengerID].Push( - msg.from, []string{msg.to}, msg.Campaign.Subject, msg.Body, nil) + msg.from, []string{msg.to}, msg.Campaign.Subject, msg.body, nil) if err != nil { m.logger.Printf("error sending message in campaign %s: %v", msg.Campaign.Name, err) @@ -472,6 +472,13 @@ func (m *CampaignMessage) Render() error { if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil { return err } - m.Body = out.Bytes() + m.body = out.Bytes() return nil } + +// Body returns a copy of the message body. +func (m *CampaignMessage) Body() []byte { + out := make([]byte, len(m.body)) + copy(out, m.body) + return out +} diff --git a/templates.go b/templates.go index 28db89a..fc4ff91 100644 --- a/templates.go +++ b/templates.go @@ -119,7 +119,7 @@ func handlePreviewTemplate(c echo.Context) error { fmt.Sprintf("Error rendering message: %v", err)) } - return c.HTML(http.StatusOK, string(m.Body)) + return c.HTML(http.StatusOK, string(m.Body())) } // handleCreateTemplate handles template creation.