From cfaa789632ea83215c029dff06f5db3cbdd4a515 Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Sat, 15 Feb 2020 22:27:52 -0600 Subject: [PATCH] Changed caching to only load the fields required for generating the campaign emails. --- models/campaign.go | 32 ++++++++++++++++++++++++++++++++ models/maillog.go | 4 ++-- worker/worker.go | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/models/campaign.go b/models/campaign.go index a0fc0395..90884e14 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -362,6 +362,38 @@ func GetCampaignSummary(id int64, uid int64) (CampaignSummary, error) { return cs, nil } +// GetCampaignMailContext returns a campaign object with just the relevant +// data needed to generate and send emails. This includes the top-level +// metadata, the template, and the sending profile. +// +// This should only ever be used if you specifically want this lightweight +// context, since it returns a non-standard campaign object. +// ref: #1726 +func GetCampaignMailContext(id int64, uid int64) (Campaign, error) { + c := Campaign{} + err := db.Where("id = ?", id).Where("user_id = ?", uid).Find(&c).Error + if err != nil { + return c, err + } + err = db.Table("smtp").Where("id=?", c.SMTPId).Find(&c.SMTP).Error + if err != nil { + return c, err + } + err = db.Where("smtp_id=?", c.SMTP.Id).Find(&c.SMTP.Headers).Error + if err != nil && err != gorm.ErrRecordNotFound { + return c, err + } + err = db.Table("templates").Where("id=?", c.TemplateId).Find(&c.Template).Error + if err != nil { + return c, err + } + err = db.Where("template_id=?", c.Template.Id).Find(&c.Template.Attachments).Error + if err != nil && err != gorm.ErrRecordNotFound { + return c, err + } + return c, nil +} + // GetCampaign returns the campaign, if it exists, specified by the given id and user_id. func GetCampaign(id int64, uid int64) (Campaign, error) { c := Campaign{} diff --git a/models/maillog.go b/models/maillog.go index 6de5bb7c..082ddceb 100644 --- a/models/maillog.go +++ b/models/maillog.go @@ -132,7 +132,7 @@ func (m *MailLog) Success() error { func (m *MailLog) GetDialer() (mailer.Dialer, error) { c := m.cachedCampaign if c == nil { - campaign, err := GetCampaign(m.CampaignId, m.UserId) + campaign, err := GetCampaignMailContext(m.CampaignId, m.UserId) if err != nil { return nil, err } @@ -162,7 +162,7 @@ func (m *MailLog) Generate(msg *gomail.Message) error { } c := m.cachedCampaign if c == nil { - campaign, err := GetCampaign(m.CampaignId, m.UserId) + campaign, err := GetCampaignMailContext(m.CampaignId, m.UserId) if err != nil { return err } diff --git a/worker/worker.go b/worker/worker.go index bec1c220..c4f2f1f0 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -69,7 +69,7 @@ func (w *DefaultWorker) processCampaigns(t time.Time) error { // generate the message (ref #1726) c, ok := campaignCache[m.CampaignId] if !ok { - c, err = models.GetCampaign(m.CampaignId, m.UserId) + c, err = models.GetCampaignMailContext(m.CampaignId, m.UserId) if err != nil { return err }