2018-10-25 13:51:47 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-04-11 10:43:43 +00:00
|
|
|
"bytes"
|
2018-10-25 13:51:47 +00:00
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
2019-04-01 11:37:24 +00:00
|
|
|
"errors"
|
2018-10-25 13:51:47 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2018-10-31 12:54:21 +00:00
|
|
|
"regexp"
|
2018-11-02 10:38:54 +00:00
|
|
|
"strings"
|
2021-05-24 17:11:48 +00:00
|
|
|
"time"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2019-01-04 07:06:55 +00:00
|
|
|
"github.com/jmoiron/sqlx/types"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/lib/pq"
|
2021-04-11 10:43:43 +00:00
|
|
|
"github.com/yuin/goldmark"
|
2021-05-08 11:25:48 +00:00
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
2018-10-25 13:51:47 +00:00
|
|
|
null "gopkg.in/volatiletech/null.v6"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Enum values for various statuses.
|
|
|
|
const (
|
|
|
|
// Subscriber.
|
|
|
|
SubscriberStatusEnabled = "enabled"
|
|
|
|
SubscriberStatusDisabled = "disabled"
|
2020-08-01 11:15:29 +00:00
|
|
|
SubscriberStatusBlockListed = "blocklisted"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-12-01 12:18:36 +00:00
|
|
|
// Subscription.
|
|
|
|
SubscriptionStatusUnconfirmed = "unconfirmed"
|
|
|
|
SubscriptionStatusConfirmed = "confirmed"
|
|
|
|
SubscriptionStatusUnsubscribed = "unsubscribed"
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// Campaign.
|
2021-01-30 09:29:21 +00:00
|
|
|
CampaignStatusDraft = "draft"
|
|
|
|
CampaignStatusScheduled = "scheduled"
|
|
|
|
CampaignStatusRunning = "running"
|
|
|
|
CampaignStatusPaused = "paused"
|
|
|
|
CampaignStatusFinished = "finished"
|
|
|
|
CampaignStatusCancelled = "cancelled"
|
|
|
|
CampaignTypeRegular = "regular"
|
|
|
|
CampaignTypeOptin = "optin"
|
|
|
|
CampaignContentTypeRichtext = "richtext"
|
|
|
|
CampaignContentTypeHTML = "html"
|
2021-04-11 10:43:43 +00:00
|
|
|
CampaignContentTypeMarkdown = "markdown"
|
2021-01-30 09:29:21 +00:00
|
|
|
CampaignContentTypePlain = "plain"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// List.
|
|
|
|
ListTypePrivate = "private"
|
|
|
|
ListTypePublic = "public"
|
2019-12-01 12:18:36 +00:00
|
|
|
ListOptinSingle = "single"
|
|
|
|
ListOptinDouble = "double"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// User.
|
|
|
|
UserTypeSuperadmin = "superadmin"
|
|
|
|
UserTypeUser = "user"
|
|
|
|
UserStatusEnabled = "enabled"
|
|
|
|
UserStatusDisabled = "disabled"
|
2018-10-31 12:54:21 +00:00
|
|
|
|
|
|
|
// BaseTpl is the name of the base template.
|
|
|
|
BaseTpl = "base"
|
|
|
|
|
|
|
|
// ContentTpl is the name of the compiled message.
|
|
|
|
ContentTpl = "content"
|
2021-05-24 17:11:48 +00:00
|
|
|
|
|
|
|
// Headers attached to e-mails for bounce tracking.
|
|
|
|
EmailHeaderSubscriberUUID = "X-Listmonk-Subscriber"
|
|
|
|
EmailHeaderCampaignUUID = "X-Listmonk-Campaign"
|
|
|
|
|
|
|
|
BounceTypeHard = "hard"
|
|
|
|
BounceTypeSoft = "soft"
|
2018-10-31 12:54:21 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 17:10:30 +00:00
|
|
|
// 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{
|
2021-09-26 10:33:05 +00:00
|
|
|
// 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" }}>.
|
|
|
|
{
|
|
|
|
regExp: regexp.MustCompile(`(https?://.+?)@TrackLink`),
|
|
|
|
replace: `{{ TrackLink "$1" . }}`,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Regular expression for matching {{ TrackLink "http://link.com" }} in the template
|
|
|
|
// and substituting it with {{ Track "http://link.com" . }} (the dot context)
|
|
|
|
// before compilation. This is to make linking easier for users.
|
|
|
|
{
|
2019-12-06 17:10:30 +00:00
|
|
|
regExp: regexp.MustCompile("{{(\\s+)?TrackLink\\s+?(\"|`)(.+?)(\"|`)(\\s+)?}}"),
|
|
|
|
replace: `{{ TrackLink "$3" . }}`,
|
|
|
|
},
|
2021-09-26 10:33:05 +00:00
|
|
|
{
|
2020-04-26 10:21:43 +00:00
|
|
|
regExp: regexp.MustCompile(`{{(\s+)?(TrackView|UnsubscribeURL|OptinURL|MessageURL)(\s+)?}}`),
|
2019-12-06 17:10:30 +00:00
|
|
|
replace: `{{ $2 . }}`,
|
|
|
|
},
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-11-28 07:59:57 +00:00
|
|
|
// AdminNotifCallback is a callback function that's called
|
|
|
|
// when a campaign's status changes.
|
2019-12-01 12:18:36 +00:00
|
|
|
type AdminNotifCallback func(subject string, data interface{}) error
|
2018-11-28 07:59:57 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// User represents an admin user.
|
|
|
|
type User struct {
|
|
|
|
Base
|
|
|
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Password string `json:"-"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber represents an e-mail subscriber.
|
|
|
|
type Subscriber struct {
|
|
|
|
Base
|
|
|
|
|
2021-08-14 11:53:05 +00:00
|
|
|
UUID string `db:"uuid" json:"uuid"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Attribs SubscriberAttribs `db:"attribs" json:"attribs"`
|
|
|
|
Status string `db:"status" json:"status"`
|
|
|
|
Lists types.JSONText `db:"lists" json:"lists"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
2019-04-01 11:37:24 +00:00
|
|
|
type subLists struct {
|
|
|
|
SubscriberID int `db:"subscriber_id"`
|
|
|
|
Lists types.JSONText `db:"lists"`
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// SubscriberAttribs is the map of key:value attributes of a subscriber.
|
|
|
|
type SubscriberAttribs map[string]interface{}
|
|
|
|
|
|
|
|
// Subscribers represents a slice of Subscriber.
|
|
|
|
type Subscribers []Subscriber
|
|
|
|
|
2021-01-23 12:53:29 +00:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// 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"`
|
2019-12-01 12:18:36 +00:00
|
|
|
Optin string `db:"optin" json:"optin"`
|
2018-10-25 13:51:47 +00:00
|
|
|
Tags pq.StringArray `db:"tags" json:"tags"`
|
|
|
|
SubscriberCount int `db:"subscriber_count" json:"subscriber_count"`
|
|
|
|
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"`
|
2019-05-14 11:11:05 +00:00
|
|
|
|
|
|
|
// Pseudofield for getting the total number of subscribers
|
|
|
|
// in searches and queries.
|
|
|
|
Total int `db:"total" json:"-"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Campaign represents an e-mail campaign.
|
|
|
|
type Campaign struct {
|
|
|
|
Base
|
|
|
|
CampaignMeta
|
|
|
|
|
|
|
|
UUID string `db:"uuid" json:"uuid"`
|
2020-02-03 07:48:26 +00:00
|
|
|
Type string `db:"type" json:"type"`
|
2018-10-25 13:51:47 +00:00
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Subject string `db:"subject" json:"subject"`
|
|
|
|
FromEmail string `db:"from_email" json:"from_email"`
|
2020-06-06 18:03:55 +00:00
|
|
|
Body string `db:"body" json:"body"`
|
2021-01-30 09:29:21 +00:00
|
|
|
AltBody null.String `db:"altbody" json:"altbody"`
|
2018-10-25 13:51:47 +00:00
|
|
|
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"`
|
|
|
|
TemplateID int `db:"template_id" json:"template_id"`
|
2020-09-20 11:01:24 +00:00
|
|
|
Messenger string `db:"messenger" json:"messenger"`
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
// TemplateBody is joined in from templates by the next-campaigns query.
|
|
|
|
TemplateBody string `db:"template_body" json:"-"`
|
|
|
|
Tpl *template.Template `json:"-"`
|
2020-07-05 13:09:24 +00:00
|
|
|
SubjectTpl *template.Template `json:"-"`
|
2021-01-30 09:29:21 +00:00
|
|
|
AltBodyTpl *template.Template `json:"-"`
|
2019-03-28 11:47:51 +00:00
|
|
|
|
|
|
|
// Pseudofield for getting the total number of subscribers
|
|
|
|
// in searches and queries.
|
|
|
|
Total int `db:"total" json:"-"`
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CampaignMeta contains fields tracking a campaign's progress.
|
|
|
|
type CampaignMeta struct {
|
2020-06-06 18:03:55 +00:00
|
|
|
CampaignID int `db:"campaign_id" json:"-"`
|
2019-04-01 11:37:24 +00:00
|
|
|
Views int `db:"views" json:"views"`
|
|
|
|
Clicks int `db:"clicks" json:"clicks"`
|
2021-05-24 17:11:48 +00:00
|
|
|
Bounces int `db:"bounces" json:"bounces"`
|
2019-04-01 11:37:24 +00:00
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
StartedAt null.Time `db:"started_at" json:"started_at"`
|
|
|
|
ToSend int `db:"to_send" json:"to_send"`
|
|
|
|
Sent int `db:"sent" json:"sent"`
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
// Campaigns represents a slice of Campaigns.
|
2018-10-25 13:51:47 +00:00
|
|
|
type Campaigns []Campaign
|
|
|
|
|
|
|
|
// Template represents a reusable e-mail template.
|
|
|
|
type Template struct {
|
|
|
|
Base
|
|
|
|
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Body string `db:"body" json:"body,omitempty"`
|
|
|
|
IsDefault bool `db:"is_default" json:"is_default"`
|
|
|
|
}
|
|
|
|
|
2021-05-24 17:11:48 +00:00
|
|
|
// 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"`
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
}
|
|
|
|
|
2021-05-08 11:25:48 +00:00
|
|
|
// markdown is a global instance of Markdown parser and renderer.
|
|
|
|
var markdown = goldmark.New(
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithXHTML(),
|
|
|
|
html.WithUnsafe(),
|
|
|
|
),
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.Table,
|
|
|
|
extension.Strikethrough,
|
|
|
|
extension.TaskList,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
// 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 {
|
2019-04-01 11:37:24 +00:00
|
|
|
var sl []subLists
|
|
|
|
err := stmt.Select(&sl, pq.Array(subs.GetIDs()))
|
2018-10-25 13:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
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
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the JSON marshalled SubscriberAttribs.
|
|
|
|
func (s SubscriberAttribs) Value() (driver.Value, error) {
|
|
|
|
return json.Marshal(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan unmarshals JSON into SubscriberAttribs.
|
|
|
|
func (s SubscriberAttribs) Scan(src interface{}) error {
|
|
|
|
if data, ok := src.([]byte); ok {
|
|
|
|
return json.Unmarshal(data, &s)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Could not not decode type %T -> %T", src, s)
|
|
|
|
}
|
2018-10-31 12:54:21 +00:00
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
// 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
|
2021-05-24 17:11:48 +00:00
|
|
|
camps[i].Bounces = c.Bounces
|
2019-04-01 11:37:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
// CompileTemplate compiles a campaign body template into its base
|
2018-11-26 12:06:05 +00:00
|
|
|
// template and sets the resultant template to Campaign.Tpl.
|
2018-10-31 12:54:21 +00:00
|
|
|
func (c *Campaign) CompileTemplate(f template.FuncMap) error {
|
|
|
|
// Compile the base template.
|
2019-12-06 17:10:30 +00:00
|
|
|
body := c.TemplateBody
|
|
|
|
for _, r := range regTplFuncs {
|
|
|
|
body = r.regExp.ReplaceAllString(body, r.replace)
|
|
|
|
}
|
|
|
|
baseTPL, err := template.New(BaseTpl).Funcs(f).Parse(body)
|
2018-10-31 12:54:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error compiling base template: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-11 10:43:43 +00:00
|
|
|
// If the format is markdown, convert Markdown to HTML.
|
|
|
|
if c.ContentType == CampaignContentTypeMarkdown {
|
|
|
|
var b bytes.Buffer
|
2021-05-08 11:25:48 +00:00
|
|
|
if err := markdown.Convert([]byte(c.Body), &b); err != nil {
|
2021-04-11 10:43:43 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
body = b.String()
|
|
|
|
} else {
|
|
|
|
body = c.Body
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
// Compile the campaign message.
|
2019-12-06 17:10:30 +00:00
|
|
|
for _, r := range regTplFuncs {
|
|
|
|
body = r.regExp.ReplaceAllString(body, r.replace)
|
|
|
|
}
|
|
|
|
msgTpl, err := template.New(ContentTpl).Funcs(f).Parse(body)
|
2018-10-31 12:54:21 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-01-30 09:29:21 +00:00
|
|
|
c.Tpl = out
|
2018-10-31 12:54:21 +00:00
|
|
|
|
2020-07-05 13:09:24 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
subjTpl, err := template.New(ContentTpl).Funcs(f).Parse(subj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error compiling subject: %v", err)
|
|
|
|
}
|
|
|
|
c.SubjectTpl = subjTpl
|
|
|
|
}
|
|
|
|
|
2021-01-30 09:29:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-02 10:38:54 +00:00
|
|
|
|
2021-05-09 10:06:31 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-11-02 10:38:54 +00:00
|
|
|
// 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.
|
2019-12-01 12:18:36 +00:00
|
|
|
func (s Subscriber) FirstName() string {
|
2018-11-02 10:38:54 +00:00
|
|
|
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.
|
2019-12-01 12:18:36 +00:00
|
|
|
func (s Subscriber) LastName() string {
|
2018-11-02 10:38:54 +00:00
|
|
|
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
|
|
|
|
}
|