Refactor manager.CampaignMessage's exposed fields

This commit is contained in:
Kailash Nadh 2020-03-08 15:02:20 +05:30
parent a0d9a4771a
commit ca032c89d6
3 changed files with 13 additions and 6 deletions

View File

@ -187,7 +187,7 @@ func handlePreviewCampaign(c echo.Context) error {
fmt.Sprintf("Error rendering message: %v", err)) 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. // 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)) 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 return err
} }

View File

@ -65,10 +65,10 @@ type Manager struct {
type CampaignMessage struct { type CampaignMessage struct {
Campaign *models.Campaign Campaign *models.Campaign
Subscriber models.Subscriber Subscriber models.Subscriber
Body []byte
from string from string
to string to string
body []byte
unsubURL string unsubURL string
} }
@ -263,7 +263,7 @@ func (m *Manager) SpawnWorkers() {
} }
err := m.messengers[msg.Campaign.MessengerID].Push( 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 { if err != nil {
m.logger.Printf("error sending message in campaign %s: %v", msg.Campaign.Name, err) 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 { if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil {
return err return err
} }
m.Body = out.Bytes() m.body = out.Bytes()
return nil 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
}

View File

@ -119,7 +119,7 @@ func handlePreviewTemplate(c echo.Context) error {
fmt.Sprintf("Error rendering message: %v", err)) 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. // handleCreateTemplate handles template creation.