Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Split models file to domain specific files (#2775)

Kailash Nadh817048985c63
diff --git a/models/bounces.go b/models/bounces.go
new file mode 100644
index 00000000..0d9c4b09
--- /dev/null
+++ b/models/bounces.go
@@ -0,0 +1,34 @@
+package models
+
+import (
+ "encoding/json"
+ "time"
+)
+
+const (
+ BounceTypeHard = "hard"
+ BounceTypeSoft = "soft"
+ BounceTypeComplaint = "complaint"
+)
+
+// Bounce represents a single bounce event.
+type Bounce struct {
+ ID int `db:"id" json:"id"`
+ Type string `db:"type" json:"type"`
+ Source string `db:"source" json:"source"`
+ Meta json.RawMessage `db:"meta" json:"meta"`
+ CreatedAt time.Time `db:"created_at" json:"created_at"`
+
+ // One of these should be provided.
+ Email string `db:"email" json:"email,omitempty"`
+ SubscriberUUID string `db:"subscriber_uuid" json:"subscriber_uuid,omitempty"`
+ SubscriberID int `db:"subscriber_id" json:"subscriber_id,omitempty"`
+ SubscriberStatus string `db:"subscriber_status" json:"subscriber_status"`
+
+ CampaignUUID string `db:"campaign_uuid" json:"campaign_uuid,omitempty"`
+ Campaign *json.RawMessage `db:"campaign" json:"campaign"`
+
+ // Pseudofield for getting the total number of bounces
+ // in searches and queries.
+ Total int `db:"total" json:"-"`
+}
diff --git a/models/campaigns.go b/models/campaigns.go
new file mode 100644
index 00000000..10af385f
--- /dev/null
+++ b/models/campaigns.go
@@ -0,0 +1,233 @@
+package models
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "html/template"
+ "strings"
+ txttpl "text/template"
+
+ "github.com/jmoiron/sqlx"
+ "github.com/jmoiron/sqlx/types"
+ "github.com/lib/pq"
+ null "gopkg.in/volatiletech/null.v6"
+)
+
+const (
+ CampaignStatusDraft = "draft"
+ CampaignStatusScheduled = "scheduled"
+ CampaignStatusRunning = "running"
+ CampaignStatusPaused = "paused"
+ CampaignStatusFinished = "finished"
+ CampaignStatusCancelled = "cancelled"
+ CampaignTypeRegular = "regular"
+ CampaignTypeOptin = "optin"
+ CampaignContentTypeRichtext = "richtext"
+ CampaignContentTypeHTML = "html"
+ CampaignContentTypeMarkdown = "markdown"
+ CampaignContentTypePlain = "plain"
+ CampaignContentTypeVisual = "visual"
+)
+
+// Campaigns represents a slice of Campaigns.
+type Campaigns []Campaign
+
+// Campaign represents an e-mail campaign.
+type Campaign struct {
+ Base
+ CampaignMeta
+
+ UUID string `db:"uuid" json:"uuid"`
+ Type string `db:"type" json:"type"`
+ Name string `db:"name" json:"name"`
+ Subject string `db:"subject" json:"subject"`
+ FromEmail string `db:"from_email" json:"from_email"`
+ Body string `db:"body" json:"body"`
+ BodySource null.String `db:"body_source" json:"body_source"`
+ AltBody null.String `db:"altbody" json:"altbody"`
+ SendAt null.Time `db:"send_at" json:"send_at"`
+ Status string `db:"status" json:"status"`
+ ContentType string `db:"content_type" json:"content_type"`
+ Tags pq.StringArray `db:"tags" json:"tags"`
+ Headers Headers `db:"headers" json:"headers"`
+ TemplateID null.Int `db:"template_id" json:"template_id"`
+ Messenger string `db:"messenger" json:"messenger"`
+ Archive bool `db:"archive" json:"archive"`
+ ArchiveSlug null.String `db:"archive_slug" json:"archive_slug"`
+ ArchiveTemplateID null.Int `db:"archive_template_id" json:"archive_template_id"`
+ ArchiveMeta json.RawMessage `db:"archive_meta" json:"archive_meta"`
+
+ // TemplateBody is joined in from templates by the next-campaigns query.
+ TemplateBody string `db:"template_body" json:"-"`
+ ArchiveTemplateBody string `db:"archive_template_body" json:"-"`
+ Tpl *template.Template `json:"-"`
+ SubjectTpl *txttpl.Template `json:"-"`
+ AltBodyTpl *template.Template `json:"-"`
+
+ // List of media (attachment) IDs obtained from the next-campaign query
+ // while sending a campaign.
+ MediaIDs pq.Int64Array `json:"-" db:"media_id"`
+
+ // Fetched bodies of the attachments.
+ Attachments []Attachment `json:"-" db:"-"`
+
+ // Pseudofield for getting the total number of subscribers
+ // in searches and queries.
+ Total int `db:"total" json:"-"`
+}
+
+// CampaignMeta contains fields tracking a campaign's progress.
+type CampaignMeta struct {
+ CampaignID int `db:"campaign_id" json:"-"`
+ Views int `db:"views" json:"views"`
+ Clicks int `db:"clicks" json:"clicks"`
+ Bounces int `db:"bounces" json:"bounces"`
+
+ // This is a list of {list_id, name} pairs unlike Subscriber.Lists[]
+ // because lists can be deleted after a campaign is finished, resulting
+ // in null lists data to be returned. For that reason, campaign_lists maintains
+ // campaign-list associations with a historical record of id + name that persist
+ // even after a list is deleted.
+ Lists types.JSONText `db:"lists" json:"lists"`
+ Media types.JSONText `db:"media" json:"media"`
+
+ StartedAt null.Time `db:"started_at" json:"started_at"`
+ ToSend int `db:"to_send" json:"to_send"`
+ Sent int `db:"sent" json:"sent"`
+}
+
+// GetIDs returns the list of campaign IDs.
+func (camps Campaigns) GetIDs() []int {
+ IDs := make([]int, len(camps))
+ for i, c := range camps {
+ IDs[i] = c.ID
+ }
+
+ return IDs
+}
+
+// LoadStats lazy loads campaign stats onto a list of campaigns.
+func (camps Campaigns) LoadStats(stmt *sqlx.Stmt) error {
+ var meta []CampaignMeta
+ if err := stmt.Select(&meta, pq.Array(camps.GetIDs())); err != nil {
+ return err
+ }
+
+ if len(camps) != len(meta) {
+ return errors.New("campaign stats count does not match")
+ }
+
+ for i, c := range meta {
+ if c.CampaignID == camps[i].ID {
+ camps[i].Lists = c.Lists
+ camps[i].Views = c.Views
+ camps[i].Clicks = c.Clicks
+ camps[i].Bounces = c.Bounces
+ camps[i].Media = c.Media
+ }
+ }
+
+ return nil
+}
+
+// CompileTemplate compiles a campaign body template into its base
+// template and sets the resultant template to Campaign.Tpl.
+func (c *Campaign) CompileTemplate(f template.FuncMap) error {
+ // If the subject line has a template string, compile it.
+ if strings.Contains(c.Subject, "{{") {
+ subj := c.Subject
+ for _, r := range regTplFuncs {
+ subj = r.regExp.ReplaceAllString(subj, r.replace)
+ }
+
+ var txtFuncs map[string]any = f
+ subjTpl, err := txttpl.New(ContentTpl).Funcs(txtFuncs).Parse(subj)
+ if err != nil {
+ return fmt.Errorf("error compiling subject: %v", err)
+ }
+ c.SubjectTpl = subjTpl
+ }
+
+ // Compile the base template.
+ body := c.TemplateBody
+
+ if body == "" || c.ContentType == CampaignContentTypeVisual {
+ body = `{{ template "content" . }}`
+ }
+
+ for _, r := range regTplFuncs {
+ body = r.regExp.ReplaceAllString(body, r.replace)
+ }
+
+ baseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)
+ if err != nil {
+ return fmt.Errorf("error compiling base template: %v", err)
+ }
+
+ // If the format is markdown, convert Markdown to HTML.
+ if c.ContentType == CampaignContentTypeMarkdown {
+ var b bytes.Buffer
+ if err := markdown.Convert([]byte(c.Body), &b); err != nil {
+ return err
+ }
+ body = b.String()
+ } else {
+ body = c.Body
+ }
+
+ // Compile the campaign message.
+ for _, r := range regTplFuncs {
+ body = r.regExp.ReplaceAllString(body, r.replace)
+ }
+
+ msgTpl, err := template.New(ContentTpl).Funcs(f).Parse(body)
+ if err != nil {
+ return fmt.Errorf("error compiling message: %v", err)
+ }
+
+ out, err := baseTPL.AddParseTree(ContentTpl, msgTpl.Tree)
+ if err != nil {
+ return fmt.Errorf("error inserting child template: %v", err)
+ }
+ c.Tpl = out
+
+ if strings.Contains(c.AltBody.String, "{{") {
+ b := c.AltBody.String
+ for _, r := range regTplFuncs {
+ b = r.regExp.ReplaceAllString(b, r.replace)
+ }
+ bTpl, err := template.New(ContentTpl).Funcs(f).Parse(b)
+ if err != nil {
+ return fmt.Errorf("error compiling alt plaintext message: %v", err)
+ }
+ c.AltBodyTpl = bTpl
+ }
+
+ return nil
+}
+
+// ConvertContent converts a campaign's body from one format to another,
+// for example, Markdown to HTML.
+func (c *Campaign) ConvertContent(from, to string) (string, error) {
+ body := c.Body
+ for _, r := range regTplFuncs {
+ body = r.regExp.ReplaceAllString(body, r.replace)
+ }
+
+ // If the format is markdown, convert Markdown to HTML.
+ var out string
+ if from == CampaignContentTypeMarkdown &&
+ (to == CampaignContentTypeHTML || to == CampaignContentTypeRichtext) {
+ var b bytes.Buffer
+ if err := markdown.Convert([]byte(c.Body), &b); err != nil {
+ return out, err
+ }
+ out = b.String()
+ } else {
+ return out, errors.New("unknown formats to convert")
+ }
+
+ return out, nil
+}
diff --git a/models/common.go b/models/common.go
new file mode 100644
index 00000000..273b629d
--- /dev/null
+++ b/models/common.go
@@ -0,0 +1,184 @@
+package models
+
+import (
+ "database/sql/driver"
+ "encoding/json"
+ "fmt"
+ "regexp"
+
+ "github.com/yuin/goldmark"
+ "github.com/yuin/goldmark/extension"
+ "github.com/yuin/goldmark/parser"
+ "github.com/yuin/goldmark/renderer/html"
+ null "gopkg.in/volatiletech/null.v6"
+)
+
+// Enum values for various statuses.
+const (
+ // Headers attached to e-mails for bounce tracking.
+ EmailHeaderSubscriberUUID = "X-Listmonk-Subscriber"
+ EmailHeaderCampaignUUID = "X-Listmonk-Campaign"
+
+ // Standard e-mail headers.
+ EmailHeaderDate = "Date"
+ EmailHeaderFrom = "From"
+ EmailHeaderSubject = "Subject"
+ EmailHeaderMessageId = "Message-Id"
+ EmailHeaderDeliveredTo = "Delivered-To"
+ EmailHeaderReceived = "Received"
+
+ // TwoFA types.
+ TwofaTypeNone = "none"
+ TwofaTypeTOTP = "totp"
+)
+
+// regTplFunc represents contains a regular expression for wrapping and
+// substituting a Go template function from the user's shorthand to a full
+// function call.
+type regTplFunc struct {
+ regExp *regexp.Regexp
+ replace string
+}
+
+var regTplFuncs = []regTplFunc{
+ // Regular expression for matching {{ TrackLink "http://link.com" }} in the template
+ // and substituting it with {{ TrackLink "http://link.com" . }} (the dot context)
+ // before compilation. This is to make linking easier for users.
+ {
+ regExp: regexp.MustCompile(`{{\s*TrackLink\s+"([^"]+)"\s*}}`),
+ replace: `{{ TrackLink "$1" . }}`,
+ },
+
+ // Convert the shorthand https://google.com@TrackLink to {{ TrackLink ... }}.
+ // This is for WYSIWYG editors that encode and break quotes {{ "" }} when inserted
+ // inside <a href="{{ TrackLink "https://these-quotes-break" }}>.
+ // The regex matches all characters that may occur in an URL
+ // (see "2. Characters" in RFC3986: https://www.ietf.org/rfc/rfc3986.txt)
+ {
+ regExp: regexp.MustCompile(`(https?://[\p{L}\p{N}_\-\.~!#$&'()*+,/:;=?@\[\]]*)@TrackLink`),
+ replace: `{{ TrackLink "$1" . }}`,
+ },
+
+ {
+ regExp: regexp.MustCompile(`{{(\s+)?(TrackView|UnsubscribeURL|ManageURL|OptinURL|MessageURL)(\s+)?}}`),
+ replace: `{{ $2 . }}`,
+ },
+}
+
+// markdown is a global instance of Markdown parser and renderer.
+var markdown = goldmark.New(
+ goldmark.WithParserOptions(
+ parser.WithAutoHeadingID(),
+ ),
+ goldmark.WithRendererOptions(
+ html.WithXHTML(),
+ html.WithUnsafe(),
+ ),
+ goldmark.WithExtensions(
+ extension.Table,
+ extension.Strikethrough,
+ extension.TaskList,
+ extension.NewTypographer(
+ extension.WithTypographicSubstitutions(extension.TypographicSubstitutions{
+ extension.LeftDoubleQuote: []byte(`"`),
+ extension.RightDoubleQuote: []byte(`"`),
+ }),
+ ),
+ ),
+)
+
+// Headers represents an array of string maps used to represent SMTP, HTTP headers etc.
+// similar to url.Values{}
+type Headers []map[string]string
+
+// PageResults is a generic HTTP response container for paginated results of list of items.
+type PageResults struct {
+ Results any `json:"results"`
+
+ Search string `json:"search"`
+ Query string `json:"query"`
+ Total int `json:"total"`
+ PerPage int `json:"per_page"`
+ Page int `json:"page"`
+}
+
+// Base holds common fields shared across models.
+type Base struct {
+ ID int `db:"id" json:"id"`
+ CreatedAt null.Time `db:"created_at" json:"created_at"`
+ UpdatedAt null.Time `db:"updated_at" json:"updated_at"`
+}
+
+// JSON is the wrapper for reading and writing arbitrary JSONB fields from the DB.
+type JSON map[string]any
+
+// StringIntMap is used to define DB Scan()s.
+type StringIntMap map[string]int
+
+// Value returns the JSON marshalled SubscriberAttribs.
+func (s JSON) Value() (driver.Value, error) {
+ return json.Marshal(s)
+}
+
+// Scan unmarshals JSONB from the DB.
+func (s JSON) Scan(src any) error {
+ if src == nil {
+ s = make(JSON)
+ return nil
+ }
+
+ if data, ok := src.([]byte); ok {
+ return json.Unmarshal(data, &s)
+ }
+ return fmt.Errorf("could not not decode type %T -> %T", src, s)
+}
+
+// Scan unmarshals JSONB from the DB.
+func (s StringIntMap) Scan(src any) error {
+ if src == nil {
+ s = make(StringIntMap)
+ return nil
+ }
+
+ if data, ok := src.([]byte); ok {
+ return json.Unmarshal(data, &s)
+ }
+ return fmt.Errorf("could not not decode type %T -> %T", src, s)
+}
+
+// Scan implements the sql.Scanner interface.
+func (h *Headers) Scan(src any) error {
+ var b []byte
+ switch src := src.(type) {
+ case []byte:
+ b = src
+ case string:
+ b = []byte(src)
+ case nil:
+ return nil
+ }
+
+ if err := json.Unmarshal(b, h); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// Value implements the driver.Valuer interface.
+func (h Headers) Value() (driver.Value, error) {
+ if h == nil {
+ return nil, nil
+ }
+
+ if n := len(h); n > 0 {
+ b, err := json.Marshal(h)
+ if err != nil {
+ return nil, err
+ }
+
+ return b, nil
+ }
+
+ return "[]", nil
+}
diff --git a/models/lists.go b/models/lists.go
new file mode 100644
index 00000000..d9c8e2fc
--- /dev/null
+++ b/models/lists.go
@@ -0,0 +1,37 @@
+package models
+
+import (
+ "github.com/lib/pq"
+ null "gopkg.in/volatiletech/null.v6"
+)
+
+const (
+ ListTypePrivate = "private"
+ ListTypePublic = "public"
+ ListOptinSingle = "single"
+ ListOptinDouble = "double"
+)
+
+// List represents a mailing list.
+type List struct {
+ Base
+
+ UUID string `db:"uuid" json:"uuid"`
+ Name string `db:"name" json:"name"`
+ Type string `db:"type" json:"type"`
+ Optin string `db:"optin" json:"optin"`
+ Tags pq.StringArray `db:"tags" json:"tags"`
+ Description string `db:"description" json:"description"`
+ SubscriberCount int `db:"subscriber_count" json:"subscriber_count"`
+ SubscriberCounts StringIntMap `db:"subscriber_statuses" json:"subscriber_statuses"`
+ SubscriberID int `db:"subscriber_id" json:"-"`
+
+ // This is only relevant when querying the lists of a subscriber.
+ SubscriptionStatus string `db:"subscription_status" json:"subscription_status,omitempty"`
+ SubscriptionCreatedAt null.Time `db:"subscription_created_at" json:"subscription_created_at,omitempty"`
+ SubscriptionUpdatedAt null.Time `db:"subscription_updated_at" json:"subscription_updated_at,omitempty"`
+
+ // Pseudofield for getting the total number of subscribers
+ // in searches and queries.
+ Total int `db:"total" json:"-"`
+}
diff --git a/models/messages.go b/models/messages.go
new file mode 100644
index 00000000..3e82dabd
--- /dev/null
+++ b/models/messages.go
@@ -0,0 +1,112 @@
+package models
+
+import (
+ "bytes"
+ "fmt"
+ "html/template"
+ "net/textproto"
+ "strings"
+ txttpl "text/template"
+)
+
+// Message is the message pushed to a Messenger.
+type Message struct {
+ From string
+ To []string
+ Subject string
+ ContentType string
+ Body []byte
+ AltBody []byte
+ Headers textproto.MIMEHeader
+ Attachments []Attachment
+
+ Subscriber Subscriber
+
+ // Campaign is generally the same instance for a large number of subscribers.
+ Campaign *Campaign
+
+ // Messenger is the messenger backend to use: email|postback.
+ Messenger string
+}
+
+// Attachment represents a file or blob attachment that can be
+// sent along with a message by a Messenger.
+type Attachment struct {
+ Name string
+ Header textproto.MIMEHeader
+ Content []byte
+}
+
+// TxMessage represents an e-mail campaign.
+type TxMessage struct {
+ SubscriberEmails []string `json:"subscriber_emails"`
+ SubscriberIDs []int `json:"subscriber_ids"`
+
+ // Deprecated.
+ SubscriberEmail string `json:"subscriber_email"`
+ SubscriberID int `json:"subscriber_id"`
+
+ TemplateID int `json:"template_id"`
+ Data map[string]any `json:"data"`
+ FromEmail string `json:"from_email"`
+ Headers Headers `json:"headers"`
+ ContentType string `json:"content_type"`
+ Messenger string `json:"messenger"`
+ Subject string `json:"subject"`
+
+ // File attachments added from multi-part form data.
+ Attachments []Attachment `json:"-"`
+
+ Body []byte `json:"-"`
+ Tpl *template.Template `json:"-"`
+ SubjectTpl *txttpl.Template `json:"-"`
+}
+
+func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {
+ data := struct {
+ Subscriber Subscriber
+ Tx *TxMessage
+ }{sub, m}
+
+ // Render the body.
+ b := bytes.Buffer{}
+ if err := tpl.Tpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
+ return err
+ }
+ m.Body = make([]byte, b.Len())
+ copy(m.Body, b.Bytes())
+ b.Reset()
+
+ // Was a subject provided in the message?
+ var (
+ subjTpl *txttpl.Template
+ subject = m.Subject
+ )
+ if subject != "" {
+ if strings.Contains(m.Subject, "{{") {
+ // If the subject has a template string, render that.
+ s, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(nil)).Parse(m.Subject)
+ if err != nil {
+ return fmt.Errorf("error compiling subject: %v", err)
+ }
+ subjTpl = s
+ }
+ } else {
+ // Use the subject from the template.
+ subject = tpl.Subject
+ subjTpl = tpl.SubjectTpl
+ }
+
+ // If the subject is also a template, render that.
+ if subjTpl != nil {
+ if err := subjTpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
+ return err
+ }
+ m.Subject = b.String()
+ b.Reset()
+ } else {
+ m.Subject = subject
+ }
+
+ return nil
+}
diff --git a/models/models.go b/models/models.go
deleted file mode 100644
index 6cf91b7f..00000000
--- a/models/models.go
+++ /dev/null
@@ -1,764 +0,0 @@
-package models
-
-import (
- "bytes"
- "database/sql/driver"
- "encoding/json"
- "errors"
- "fmt"
- "html/template"
- "net/textproto"
- "regexp"
- "strings"
- txttpl "text/template"
- "time"
-
- "github.com/jmoiron/sqlx"
- "github.com/jmoiron/sqlx/types"
- "github.com/lib/pq"
- "github.com/yuin/goldmark"
- "github.com/yuin/goldmark/extension"
- "github.com/yuin/goldmark/parser"
- "github.com/yuin/goldmark/renderer/html"
- null "gopkg.in/volatiletech/null.v6"
-)
-
-// Enum values for various statuses.
-const (
- // Subscriber.
- SubscriberStatusEnabled = "enabled"
- SubscriberStatusDisabled = "disabled"
- SubscriberStatusBlockListed = "blocklisted"
-
- // Subscription.
- SubscriptionStatusUnconfirmed = "unconfirmed"
- SubscriptionStatusConfirmed = "confirmed"
- SubscriptionStatusUnsubscribed = "unsubscribed"
-
- // Campaign.
- CampaignStatusDraft = "draft"
- CampaignStatusScheduled = "scheduled"
- CampaignStatusRunning = "running"
- CampaignStatusPaused = "paused"
- CampaignStatusFinished = "finished"
- CampaignStatusCancelled = "cancelled"
- CampaignTypeRegular = "regular"
- CampaignTypeOptin = "optin"
- CampaignContentTypeRichtext = "richtext"
- CampaignContentTypeHTML = "html"
- CampaignContentTypeMarkdown = "markdown"
- CampaignContentTypePlain = "plain"
- CampaignContentTypeVisual = "visual"
-
- // List.
- ListTypePrivate = "private"
- ListTypePublic = "public"
- ListOptinSingle = "single"
- ListOptinDouble = "double"
-
- // BaseTpl is the name of the base template.
- BaseTpl = "base"
-
- // ContentTpl is the name of the compiled message.
- ContentTpl = "content"
-
- // Headers attached to e-mails for bounce tracking.
- EmailHeaderSubscriberUUID = "X-Listmonk-Subscriber"
- EmailHeaderCampaignUUID = "X-Listmonk-Campaign"
-
- // Standard e-mail headers.
- EmailHeaderDate = "Date"
- EmailHeaderFrom = "From"
- EmailHeaderSubject = "Subject"
- EmailHeaderMessageId = "Message-Id"
- EmailHeaderDeliveredTo = "Delivered-To"
- EmailHeaderReceived = "Received"
-
- BounceTypeHard = "hard"
- BounceTypeSoft = "soft"
- BounceTypeComplaint = "complaint"
-
- // Templates.
- TemplateTypeCampaign = "campaign"
- TemplateTypeCampaignVisual = "campaign_visual"
- TemplateTypeTx = "tx"
-
- // TwoFA types.
- TwofaTypeNone = "none"
- TwofaTypeTOTP = "totp"
-)
-
-// Headers represents an array of string maps used to represent SMTP, HTTP headers etc.
-// similar to url.Values{}
-type Headers []map[string]string
-
-// regTplFunc represents contains a regular expression for wrapping and
-// substituting a Go template function from the user's shorthand to a full
-// function call.
-type regTplFunc struct {
- regExp *regexp.Regexp
- replace string
-}
-
-var regTplFuncs = []regTplFunc{
- // Regular expression for matching {{ TrackLink "http://link.com" }} in the template
- // and substituting it with {{ TrackLink "http://link.com" . }} (the dot context)
- // before compilation. This is to make linking easier for users.
- {
- regExp: regexp.MustCompile(`{{\s*TrackLink\s+"([^"]+)"\s*}}`),
- replace: `{{ TrackLink "$1" . }}`,
- },
-
- // Convert the shorthand https://google.com@TrackLink to {{ TrackLink ... }}.
- // This is for WYSIWYG editors that encode and break quotes {{ "" }} when inserted
- // inside <a href="{{ TrackLink "https://these-quotes-break" }}>.
- // The regex matches all characters that may occur in an URL
- // (see "2. Characters" in RFC3986: https://www.ietf.org/rfc/rfc3986.txt)
- {
- regExp: regexp.MustCompile(`(https?://[\p{L}\p{N}_\-\.~!#$&'()*+,/:;=?@\[\]]*)@TrackLink`),
- replace: `{{ TrackLink "$1" . }}`,
- },
-
- {
- regExp: regexp.MustCompile(`{{(\s+)?(TrackView|UnsubscribeURL|ManageURL|OptinURL|MessageURL)(\s+)?}}`),
- replace: `{{ $2 . }}`,
- },
-}
-
-// PageResults is a generic HTTP response container for paginated results of list of items.
-type PageResults struct {
- Results any `json:"results"`
-
- Search string `json:"search"`
- Query string `json:"query"`
- Total int `json:"total"`
- PerPage int `json:"per_page"`
- Page int `json:"page"`
-}
-
-// Base holds common fields shared across models.
-type Base struct {
- ID int `db:"id" json:"id"`
- CreatedAt null.Time `db:"created_at" json:"created_at"`
- UpdatedAt null.Time `db:"updated_at" json:"updated_at"`
-}
-
-// Subscriber represents an e-mail subscriber.
-type Subscriber struct {
- Base
-
- UUID string `db:"uuid" json:"uuid"`
- Email string `db:"email" json:"email" form:"email"`
- Name string `db:"name" json:"name" form:"name"`
- Attribs JSON `db:"attribs" json:"attribs"`
- Status string `db:"status" json:"status"`
- Lists types.JSONText `db:"lists" json:"lists"`
-}
-type subLists struct {
- SubscriberID int `db:"subscriber_id"`
- Lists types.JSONText `db:"lists"`
-}
-
-// Subscription represents a list attached to a subscriber.
-type Subscription struct {
- List
- SubscriptionStatus null.String `db:"subscription_status" json:"subscription_status"`
- SubscriptionCreatedAt null.String `db:"subscription_created_at" json:"subscription_created_at"`
- Meta json.RawMessage `db:"meta" json:"meta"`
-}
-
-// SubscriberExportProfile represents a subscriber's collated data in JSON for export.
-type SubscriberExportProfile struct {
- Email string `db:"email" json:"-"`
- Profile json.RawMessage `db:"profile" json:"profile,omitempty"`
- Subscriptions json.RawMessage `db:"subscriptions" json:"subscriptions,omitempty"`
- CampaignViews json.RawMessage `db:"campaign_views" json:"campaign_views,omitempty"`
- LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks,omitempty"`
-}
-
-// SubscriberActivity represents a subscriber's campaign views and link clicks for the Activity tab.
-type SubscriberActivity struct {
- CampaignViews json.RawMessage `db:"campaign_views" json:"campaign_views"`
- LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks"`
-}
-
-// JSON is the wrapper for reading and writing arbitrary JSONB fields from the DB.
-type JSON map[string]any
-
-// StringIntMap is used to define DB Scan()s.
-type StringIntMap map[string]int
-
-// Subscribers represents a slice of Subscriber.
-type Subscribers []Subscriber
-
-// SubscriberExport represents a subscriber record that is exported to raw data.
-type SubscriberExport struct {
- Base
-
- UUID string `db:"uuid" json:"uuid"`
- Email string `db:"email" json:"email"`
- Name string `db:"name" json:"name"`
- Attribs string `db:"attribs" json:"attribs"`
- Status string `db:"status" json:"status"`
-}
-
-// List represents a mailing list.
-type List struct {
- Base
-
- UUID string `db:"uuid" json:"uuid"`
- Name string `db:"name" json:"name"`
- Type string `db:"type" json:"type"`
- Optin string `db:"optin" json:"optin"`
- Tags pq.StringArray `db:"tags" json:"tags"`
- Description string `db:"description" json:"description"`
- SubscriberCount int `db:"subscriber_count" json:"subscriber_count"`
- SubscriberCounts StringIntMap `db:"subscriber_statuses" json:"subscriber_statuses"`
- SubscriberID int `db:"subscriber_id" json:"-"`
-
- // This is only relevant when querying the lists of a subscriber.
- SubscriptionStatus string `db:"subscription_status" json:"subscription_status,omitempty"`
- SubscriptionCreatedAt null.Time `db:"subscription_created_at" json:"subscription_created_at,omitempty"`
- SubscriptionUpdatedAt null.Time `db:"subscription_updated_at" json:"subscription_updated_at,omitempty"`
-
- // Pseudofield for getting the total number of subscribers
- // in searches and queries.
- Total int `db:"total" json:"-"`
-}
-
-// Campaign represents an e-mail campaign.
-type Campaign struct {
- Base
- CampaignMeta
-
- UUID string `db:"uuid" json:"uuid"`
- Type string `db:"type" json:"type"`
- Name string `db:"name" json:"name"`
- Subject string `db:"subject" json:"subject"`
- FromEmail string `db:"from_email" json:"from_email"`
- Body string `db:"body" json:"body"`
- BodySource null.String `db:"body_source" json:"body_source"`
- AltBody null.String `db:"altbody" json:"altbody"`
- SendAt null.Time `db:"send_at" json:"send_at"`
- Status string `db:"status" json:"status"`
- ContentType string `db:"content_type" json:"content_type"`
- Tags pq.StringArray `db:"tags" json:"tags"`
- Headers Headers `db:"headers" json:"headers"`
- TemplateID null.Int `db:"template_id" json:"template_id"`
- Messenger string `db:"messenger" json:"messenger"`
- Archive bool `db:"archive" json:"archive"`
- ArchiveSlug null.String `db:"archive_slug" json:"archive_slug"`
- ArchiveTemplateID null.Int `db:"archive_template_id" json:"archive_template_id"`
- ArchiveMeta json.RawMessage `db:"archive_meta" json:"archive_meta"`
-
- // TemplateBody is joined in from templates by the next-campaigns query.
- TemplateBody string `db:"template_body" json:"-"`
- ArchiveTemplateBody string `db:"archive_template_body" json:"-"`
- Tpl *template.Template `json:"-"`
- SubjectTpl *txttpl.Template `json:"-"`
- AltBodyTpl *template.Template `json:"-"`
-
- // List of media (attachment) IDs obtained from the next-campaign query
- // while sending a campaign.
- MediaIDs pq.Int64Array `json:"-" db:"media_id"`
-
- // Fetched bodies of the attachments.
- Attachments []Attachment `json:"-" db:"-"`
-
- // Pseudofield for getting the total number of subscribers
- // in searches and queries.
- Total int `db:"total" json:"-"`
-}
-
-// CampaignMeta contains fields tracking a campaign's progress.
-type CampaignMeta struct {
- CampaignID int `db:"campaign_id" json:"-"`
- Views int `db:"views" json:"views"`
- Clicks int `db:"clicks" json:"clicks"`
- Bounces int `db:"bounces" json:"bounces"`
-
- // This is a list of {list_id, name} pairs unlike Subscriber.Lists[]
- // because lists can be deleted after a campaign is finished, resulting
- // in null lists data to be returned. For that reason, campaign_lists maintains
- // campaign-list associations with a historical record of id + name that persist
- // even after a list is deleted.
- Lists types.JSONText `db:"lists" json:"lists"`
- Media types.JSONText `db:"media" json:"media"`
-
- StartedAt null.Time `db:"started_at" json:"started_at"`
- ToSend int `db:"to_send" json:"to_send"`
- Sent int `db:"sent" json:"sent"`
-}
-
-type CampaignStats struct {
- ID int `db:"id" json:"id"`
- Status string `db:"status" json:"status"`
- ToSend int `db:"to_send" json:"to_send"`
- Sent int `db:"sent" json:"sent"`
- Started null.Time `db:"started_at" json:"started_at"`
- UpdatedAt null.Time `db:"updated_at" json:"updated_at"`
- Rate int `json:"rate"`
- NetRate int `json:"net_rate"`
-}
-
-type CampaignAnalyticsCount struct {
- CampaignID int `db:"campaign_id" json:"campaign_id"`
- Count int `db:"count" json:"count"`
- Timestamp time.Time `db:"timestamp" json:"timestamp"`
-}
-
-type CampaignAnalyticsLink struct {
- URL string `db:"url" json:"url"`
- Count int `db:"count" json:"count"`
-}
-
-// Campaigns represents a slice of Campaigns.
-type Campaigns []Campaign
-
-// Template represents a reusable e-mail template.
-type Template struct {
- Base
-
- Name string `db:"name" json:"name"`
- // Subject is only for type=tx.
- Subject string `db:"subject" json:"subject"`
- Type string `db:"type" json:"type"`
- Body string `db:"body" json:"body,omitempty"`
- BodySource null.String `db:"body_source" json:"body_source,omitempty"`
- IsDefault bool `db:"is_default" json:"is_default"`
-
- // Only relevant to tx (transactional) templates.
- SubjectTpl *txttpl.Template `json:"-"`
- Tpl *template.Template `json:"-"`
-}
-
-// Bounce represents a single bounce event.
-type Bounce struct {
- ID int `db:"id" json:"id"`
- Type string `db:"type" json:"type"`
- Source string `db:"source" json:"source"`
- Meta json.RawMessage `db:"meta" json:"meta"`
- CreatedAt time.Time `db:"created_at" json:"created_at"`
-
- // One of these should be provided.
- Email string `db:"email" json:"email,omitempty"`
- SubscriberUUID string `db:"subscriber_uuid" json:"subscriber_uuid,omitempty"`
- SubscriberID int `db:"subscriber_id" json:"subscriber_id,omitempty"`
- SubscriberStatus string `db:"subscriber_status" json:"subscriber_status"`
-
- CampaignUUID string `db:"campaign_uuid" json:"campaign_uuid,omitempty"`
- Campaign *json.RawMessage `db:"campaign" json:"campaign"`
-
- // Pseudofield for getting the total number of bounces
- // in searches and queries.
- Total int `db:"total" json:"-"`
-}
-
-// Message is the message pushed to a Messenger.
-type Message struct {
- From string
- To []string
- Subject string
- ContentType string
- Body []byte
- AltBody []byte
- Headers textproto.MIMEHeader
- Attachments []Attachment
-
- Subscriber Subscriber
-
- // Campaign is generally the same instance for a large number of subscribers.
- Campaign *Campaign
-
- // Messenger is the messenger backend to use: email|postback.
- Messenger string
-}
-
-// Attachment represents a file or blob attachment that can be
-// sent along with a message by a Messenger.
-type Attachment struct {
- Name string
- Header textproto.MIMEHeader
- Content []byte
-}
-
-// TxMessage represents an e-mail campaign.
-type TxMessage struct {
- SubscriberEmails []string `json:"subscriber_emails"`
- SubscriberIDs []int `json:"subscriber_ids"`
-
- // Deprecated.
- SubscriberEmail string `json:"subscriber_email"`
- SubscriberID int `json:"subscriber_id"`
-
- TemplateID int `json:"template_id"`
- Data map[string]any `json:"data"`
- FromEmail string `json:"from_email"`
- Headers Headers `json:"headers"`
- ContentType string `json:"content_type"`
- Messenger string `json:"messenger"`
- Subject string `json:"subject"`
-
- // File attachments added from multi-part form data.
- Attachments []Attachment `json:"-"`
-
- Body []byte `json:"-"`
- Tpl *template.Template `json:"-"`
- SubjectTpl *txttpl.Template `json:"-"`
-}
-
-// markdown is a global instance of Markdown parser and renderer.
-var markdown = goldmark.New(
- goldmark.WithParserOptions(
- parser.WithAutoHeadingID(),
- ),
- goldmark.WithRendererOptions(
- html.WithXHTML(),
- html.WithUnsafe(),
- ),
- goldmark.WithExtensions(
- extension.Table,
- extension.Strikethrough,
- extension.TaskList,
- extension.NewTypographer(
- extension.WithTypographicSubstitutions(extension.TypographicSubstitutions{
- extension.LeftDoubleQuote: []byte(`"`),
- extension.RightDoubleQuote: []byte(`"`),
- }),
- ),
- ),
-)
-
-// GetIDs returns the list of subscriber IDs.
-func (subs Subscribers) GetIDs() []int {
- IDs := make([]int, len(subs))
- for i, c := range subs {
- IDs[i] = c.ID
- }
-
- return IDs
-}
-
-// LoadLists lazy loads the lists for all the subscribers
-// in the Subscribers slice and attaches them to their []Lists property.
-func (subs Subscribers) LoadLists(stmt *sqlx.Stmt) error {
- var sl []subLists
- err := stmt.Select(&sl, pq.Array(subs.GetIDs()))
- if err != nil {
- return err
- }
-
- if len(subs) != len(sl) {
- return errors.New("campaign stats count does not match")
- }
-
- for i, s := range sl {
- if s.SubscriberID == subs[i].ID {
- subs[i].Lists = s.Lists
- }
- }
-
- return nil
-}
-
-// Value returns the JSON marshalled SubscriberAttribs.
-func (s JSON) Value() (driver.Value, error) {
- return json.Marshal(s)
-}
-
-// Scan unmarshals JSONB from the DB.
-func (s JSON) Scan(src any) error {
- if src == nil {
- s = make(JSON)
- return nil
- }
-
- if data, ok := src.([]byte); ok {
- return json.Unmarshal(data, &s)
- }
- return fmt.Errorf("could not not decode type %T -> %T", src, s)
-}
-
-// Scan unmarshals JSONB from the DB.
-func (s StringIntMap) Scan(src any) error {
- if src == nil {
- s = make(StringIntMap)
- return nil
- }
-
- if data, ok := src.([]byte); ok {
- return json.Unmarshal(data, &s)
- }
- return fmt.Errorf("could not not decode type %T -> %T", src, s)
-}
-
-// GetIDs returns the list of campaign IDs.
-func (camps Campaigns) GetIDs() []int {
- IDs := make([]int, len(camps))
- for i, c := range camps {
- IDs[i] = c.ID
- }
-
- return IDs
-}
-
-// LoadStats lazy loads campaign stats onto a list of campaigns.
-func (camps Campaigns) LoadStats(stmt *sqlx.Stmt) error {
- var meta []CampaignMeta
- if err := stmt.Select(&meta, pq.Array(camps.GetIDs())); err != nil {
- return err
- }
-
- if len(camps) != len(meta) {
- return errors.New("campaign stats count does not match")
- }
-
- for i, c := range meta {
- if c.CampaignID == camps[i].ID {
- camps[i].Lists = c.Lists
- camps[i].Views = c.Views
- camps[i].Clicks = c.Clicks
- camps[i].Bounces = c.Bounces
- camps[i].Media = c.Media
- }
- }
-
- return nil
-}
-
-// CompileTemplate compiles a campaign body template into its base
-// template and sets the resultant template to Campaign.Tpl.
-func (c *Campaign) CompileTemplate(f template.FuncMap) error {
- // If the subject line has a template string, compile it.
- if strings.Contains(c.Subject, "{{") {
- subj := c.Subject
- for _, r := range regTplFuncs {
- subj = r.regExp.ReplaceAllString(subj, r.replace)
- }
-
- var txtFuncs map[string]any = f
- subjTpl, err := txttpl.New(ContentTpl).Funcs(txtFuncs).Parse(subj)
- if err != nil {
- return fmt.Errorf("error compiling subject: %v", err)
- }
- c.SubjectTpl = subjTpl
- }
-
- // Compile the base template.
- body := c.TemplateBody
-
- if body == "" || c.ContentType == CampaignContentTypeVisual {
- body = `{{ template "content" . }}`
- }
-
- for _, r := range regTplFuncs {
- body = r.regExp.ReplaceAllString(body, r.replace)
- }
-
- baseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)
- if err != nil {
- return fmt.Errorf("error compiling base template: %v", err)
- }
-
- // If the format is markdown, convert Markdown to HTML.
- if c.ContentType == CampaignContentTypeMarkdown {
- var b bytes.Buffer
- if err := markdown.Convert([]byte(c.Body), &b); err != nil {
- return err
- }
- body = b.String()
- } else {
- body = c.Body
- }
-
- // Compile the campaign message.
- for _, r := range regTplFuncs {
- body = r.regExp.ReplaceAllString(body, r.replace)
- }
-
- msgTpl, err := template.New(ContentTpl).Funcs(f).Parse(body)
- if err != nil {
- return fmt.Errorf("error compiling message: %v", err)
- }
-
- out, err := baseTPL.AddParseTree(ContentTpl, msgTpl.Tree)
- if err != nil {
- return fmt.Errorf("error inserting child template: %v", err)
- }
- c.Tpl = out
-
- if strings.Contains(c.AltBody.String, "{{") {
- b := c.AltBody.String
- for _, r := range regTplFuncs {
- b = r.regExp.ReplaceAllString(b, r.replace)
- }
- bTpl, err := template.New(ContentTpl).Funcs(f).Parse(b)
- if err != nil {
- return fmt.Errorf("error compiling alt plaintext message: %v", err)
- }
- c.AltBodyTpl = bTpl
- }
-
- return nil
-}
-
-// ConvertContent converts a campaign's body from one format to another,
-// for example, Markdown to HTML.
-func (c *Campaign) ConvertContent(from, to string) (string, error) {
- body := c.Body
- for _, r := range regTplFuncs {
- body = r.regExp.ReplaceAllString(body, r.replace)
- }
-
- // If the format is markdown, convert Markdown to HTML.
- var out string
- if from == CampaignContentTypeMarkdown &&
- (to == CampaignContentTypeHTML || to == CampaignContentTypeRichtext) {
- var b bytes.Buffer
- if err := markdown.Convert([]byte(c.Body), &b); err != nil {
- return out, err
- }
- out = b.String()
- } else {
- return out, errors.New("unknown formats to convert")
- }
-
- return out, nil
-}
-
-// Compile compiles a template body and subject (only for tx templates) and
-// caches the templat references to be executed later.
-func (t *Template) Compile(f template.FuncMap) error {
- tpl, err := template.New(BaseTpl).Funcs(f).Parse(t.Body)
- if err != nil {
- return fmt.Errorf("error compiling transactional template: %v", err)
- }
- t.Tpl = tpl
-
- // If the subject line has a template string, compile it.
- if strings.Contains(t.Subject, "{{") {
- subj := t.Subject
-
- subjTpl, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(f)).Parse(subj)
- if err != nil {
- return fmt.Errorf("error compiling subject: %v", err)
- }
- t.SubjectTpl = subjTpl
- }
-
- return nil
-}
-
-func (m *TxMessage) Render(sub Subscriber, tpl *Template) error {
- data := struct {
- Subscriber Subscriber
- Tx *TxMessage
- }{sub, m}
-
- // Render the body.
- b := bytes.Buffer{}
- if err := tpl.Tpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
- return err
- }
- m.Body = make([]byte, b.Len())
- copy(m.Body, b.Bytes())
- b.Reset()
-
- // Was a subject provided in the message?
- var (
- subjTpl *txttpl.Template
- subject = m.Subject
- )
- if subject != "" {
- if strings.Contains(m.Subject, "{{") {
- // If the subject has a template string, render that.
- s, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(nil)).Parse(m.Subject)
- if err != nil {
- return fmt.Errorf("error compiling subject: %v", err)
- }
- subjTpl = s
- }
- } else {
- // Use the subject from the template.
- subject = tpl.Subject
- subjTpl = tpl.SubjectTpl
- }
-
- // If the subject is also a template, render that.
- if subjTpl != nil {
- if err := subjTpl.ExecuteTemplate(&b, BaseTpl, data); err != nil {
- return err
- }
- m.Subject = b.String()
- b.Reset()
- } else {
- m.Subject = subject
- }
-
- return nil
-}
-
-// FirstName splits the name by spaces and returns the first chunk
-// of the name that's greater than 2 characters in length, assuming
-// that it is the subscriber's first name.
-func (s Subscriber) FirstName() string {
- for _, s := range strings.Split(s.Name, " ") {
- if len(s) > 2 {
- return s
- }
- }
-
- return s.Name
-}
-
-// LastName splits the name by spaces and returns the last chunk
-// of the name that's greater than 2 characters in length, assuming
-// that it is the subscriber's last name.
-func (s Subscriber) LastName() string {
- chunks := strings.Split(s.Name, " ")
- for i := len(chunks) - 1; i >= 0; i-- {
- chunk := chunks[i]
- if len(chunk) > 2 {
- return chunk
- }
- }
-
- return s.Name
-}
-
-// Scan implements the sql.Scanner interface.
-func (h *Headers) Scan(src any) error {
- var b []byte
- switch src := src.(type) {
- case []byte:
- b = src
- case string:
- b = []byte(src)
- case nil:
- return nil
- }
-
- if err := json.Unmarshal(b, h); err != nil {
- return err
- }
-
- return nil
-}
-
-// Value implements the driver.Valuer interface.
-func (h Headers) Value() (driver.Value, error) {
- if h == nil {
- return nil, nil
- }
-
- if n := len(h); n > 0 {
- b, err := json.Marshal(h)
- if err != nil {
- return nil, err
- }
-
- return b, nil
- }
-
- return "[]", nil
-}
diff --git a/models/subscribers.go b/models/subscribers.go
new file mode 100644
index 00000000..0ee42d92
--- /dev/null
+++ b/models/subscribers.go
@@ -0,0 +1,136 @@
+package models
+
+import (
+ "encoding/json"
+ "errors"
+ "strings"
+
+ "github.com/jmoiron/sqlx"
+ "github.com/jmoiron/sqlx/types"
+ "github.com/lib/pq"
+ null "gopkg.in/volatiletech/null.v6"
+)
+
+const (
+ SubscriberStatusEnabled = "enabled"
+ SubscriberStatusDisabled = "disabled"
+ SubscriberStatusBlockListed = "blocklisted"
+
+ SubscriptionStatusUnconfirmed = "unconfirmed"
+ SubscriptionStatusConfirmed = "confirmed"
+ SubscriptionStatusUnsubscribed = "unsubscribed"
+)
+
+// Subscribers represents a slice of Subscriber.
+type Subscribers []Subscriber
+
+// Subscriber represents an e-mail subscriber.
+type Subscriber struct {
+ Base
+
+ UUID string `db:"uuid" json:"uuid"`
+ Email string `db:"email" json:"email" form:"email"`
+ Name string `db:"name" json:"name" form:"name"`
+ Attribs JSON `db:"attribs" json:"attribs"`
+ Status string `db:"status" json:"status"`
+ Lists types.JSONText `db:"lists" json:"lists"`
+}
+
+type subLists struct {
+ SubscriberID int `db:"subscriber_id"`
+ Lists types.JSONText `db:"lists"`
+}
+
+// GetIDs returns the list of subscriber IDs.
+func (subs Subscribers) GetIDs() []int {
+ IDs := make([]int, len(subs))
+ for i, c := range subs {
+ IDs[i] = c.ID
+ }
+
+ return IDs
+}
+
+// LoadLists lazy loads the lists for all the subscribers
+// in the Subscribers slice and attaches them to their []Lists property.
+func (subs Subscribers) LoadLists(stmt *sqlx.Stmt) error {
+ var sl []subLists
+ err := stmt.Select(&sl, pq.Array(subs.GetIDs()))
+ if err != nil {
+ return err
+ }
+
+ if len(subs) != len(sl) {
+ return errors.New("campaign stats count does not match")
+ }
+
+ for i, s := range sl {
+ if s.SubscriberID == subs[i].ID {
+ subs[i].Lists = s.Lists
+ }
+ }
+
+ return nil
+}
+
+// FirstName splits the name by spaces and returns the first chunk
+// of the name that's greater than 2 characters in length, assuming
+// that it is the subscriber's first name.
+func (s Subscriber) FirstName() string {
+ for _, s := range strings.Split(s.Name, " ") {
+ if len(s) > 2 {
+ return s
+ }
+ }
+
+ return s.Name
+}
+
+// LastName splits the name by spaces and returns the last chunk
+// of the name that's greater than 2 characters in length, assuming
+// that it is the subscriber's last name.
+func (s Subscriber) LastName() string {
+ chunks := strings.Split(s.Name, " ")
+ for i := len(chunks) - 1; i >= 0; i-- {
+ chunk := chunks[i]
+ if len(chunk) > 2 {
+ return chunk
+ }
+ }
+
+ return s.Name
+}
+
+// Subscription represents a list attached to a subscriber.
+type Subscription struct {
+ List
+ SubscriptionStatus null.String `db:"subscription_status" json:"subscription_status"`
+ SubscriptionCreatedAt null.String `db:"subscription_created_at" json:"subscription_created_at"`
+ Meta json.RawMessage `db:"meta" json:"meta"`
+}
+
+// SubscriberExport represents a subscriber record that is exported to raw data.
+type SubscriberExport struct {
+ Base
+
+ UUID string `db:"uuid" json:"uuid"`
+ Email string `db:"email" json:"email"`
+ Name string `db:"name" json:"name"`
+ Attribs string `db:"attribs" json:"attribs"`
+ Status string `db:"status" json:"status"`
+}
+
+// SubscriberExportProfile represents a subscriber's collated data in JSON for export.
+type SubscriberExportProfile struct {
+ Email string `db:"email" json:"-"`
+ Profile json.RawMessage `db:"profile" json:"profile,omitempty"`
+ Subscriptions json.RawMessage `db:"subscriptions" json:"subscriptions,omitempty"`
+ CampaignViews json.RawMessage `db:"campaign_views" json:"campaign_views,omitempty"`
+ LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks,omitempty"`
+}
+
+// SubscriberActivity represents a subscriber's campaign views and link clicks for the Activity tab.
+type SubscriberActivity struct {
+ CampaignViews json.RawMessage `db:"campaign_views" json:"campaign_views"`
+ LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks"`
+}
diff --git a/models/templates.go b/models/templates.go
new file mode 100644
index 00000000..2ab1fa23
--- /dev/null
+++ b/models/templates.go
@@ -0,0 +1,81 @@
+package models
+
+import (
+ "fmt"
+ "html/template"
+ "strings"
+ txttpl "text/template"
+ "time"
+
+ null "gopkg.in/volatiletech/null.v6"
+)
+
+const (
+ BaseTpl = "base"
+ ContentTpl = "content"
+ TemplateTypeCampaign = "campaign"
+ TemplateTypeCampaignVisual = "campaign_visual"
+ TemplateTypeTx = "tx"
+)
+
+// Template represents a reusable e-mail template.
+type Template struct {
+ Base
+
+ Name string `db:"name" json:"name"`
+ // Subject is only for type=tx.
+ Subject string `db:"subject" json:"subject"`
+ Type string `db:"type" json:"type"`
+ Body string `db:"body" json:"body,omitempty"`
+ BodySource null.String `db:"body_source" json:"body_source,omitempty"`
+ IsDefault bool `db:"is_default" json:"is_default"`
+
+ // Only relevant to tx (transactional) templates.
+ SubjectTpl *txttpl.Template `json:"-"`
+ Tpl *template.Template `json:"-"`
+}
+
+// Compile compiles a template body and subject (only for tx templates) and
+// caches the templat references to be executed later.
+func (t *Template) Compile(f template.FuncMap) error {
+ tpl, err := template.New(BaseTpl).Funcs(f).Parse(t.Body)
+ if err != nil {
+ return fmt.Errorf("error compiling transactional template: %v", err)
+ }
+ t.Tpl = tpl
+
+ // If the subject line has a template string, compile it.
+ if strings.Contains(t.Subject, "{{") {
+ subj := t.Subject
+
+ subjTpl, err := txttpl.New(BaseTpl).Funcs(txttpl.FuncMap(f)).Parse(subj)
+ if err != nil {
+ return fmt.Errorf("error compiling subject: %v", err)
+ }
+ t.SubjectTpl = subjTpl
+ }
+
+ return nil
+}
+
+type CampaignStats struct {
+ ID int `db:"id" json:"id"`
+ Status string `db:"status" json:"status"`
+ ToSend int `db:"to_send" json:"to_send"`
+ Sent int `db:"sent" json:"sent"`
+ Started null.Time `db:"started_at" json:"started_at"`
+ UpdatedAt null.Time `db:"updated_at" json:"updated_at"`
+ Rate int `json:"rate"`
+ NetRate int `json:"net_rate"`
+}
+
+type CampaignAnalyticsCount struct {
+ CampaignID int `db:"campaign_id" json:"campaign_id"`
+ Count int `db:"count" json:"count"`
+ Timestamp time.Time `db:"timestamp" json:"timestamp"`
+}
+
+type CampaignAnalyticsLink struct {
+ URL string `db:"url" json:"url"`
+ Count int `db:"count" json:"count"`
+}