Add new template functions and rename Track to TrackLink

This commit is contained in:
Kailash Nadh 2018-11-01 22:00:07 +05:30
parent ad96a4341c
commit 67d65b3a8b
2 changed files with 39 additions and 13 deletions

View File

@ -50,8 +50,8 @@ const (
// and substituting it with {{ Track "http://link.com" .Campaign.UUID .Subscriber.UUID }}
// before compilation. This string gimmick is to make linking easier for users.
var (
regexpLinkTag = regexp.MustCompile(`{{(\s+)?Track\s+?"(.+?)"(\s+)?}}`)
regexpLinkTagReplace = `{{ Track "$2" .Campaign.UUID .Subscriber.UUID }}`
regexpLinkTag = regexp.MustCompile(`{{(\s+)?TrackLink\s+?"(.+?)"(\s+)?}}`)
regexpLinkTagReplace = `{{ TrackLink "$2" .Campaign.UUID .Subscriber.UUID }}`
)
// Base holds common fields shared across models.

View File

@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"log"
"strings"
"sync"
"time"
@ -195,19 +196,8 @@ func (r *Runner) SpawnWorkers() {
}
}
// TemplateFuncs returns the template functions to be applied into
// compiled campaign templates.
func (r *Runner) TemplateFuncs(c *models.Campaign) template.FuncMap {
return template.FuncMap{
"Track": func(url, campUUID, subUUID string) string {
return r.trackLink(url, campUUID, subUUID)
},
}
}
// addCampaign adds a campaign to the process queue.
func (r *Runner) addCampaign(c *models.Campaign) error {
// Validate messenger.
if _, ok := r.messengers[c.MessengerID]; !ok {
r.src.CancelCampaign(c.ID)
@ -324,3 +314,39 @@ func (r *Runner) trackLink(url, campUUID, subUUID string) string {
return fmt.Sprintf(r.cfg.LinkTrackURL, uu, campUUID, subUUID)
}
// TemplateFuncs returns the template functions to be applied into
// compiled campaign templates.
func (r *Runner) TemplateFuncs(c *models.Campaign) template.FuncMap {
return template.FuncMap{
"TrackLink": func(url, campUUID, subUUID string) string {
return r.trackLink(url, campUUID, subUUID)
},
"FirstName": func(name string) string {
for _, s := range strings.Split(name, " ") {
if len(s) > 2 {
return s
}
}
return name
},
"LastName": func(name string) string {
s := strings.Split(name, " ")
for i := len(s) - 1; i >= 0; i-- {
chunk := s[i]
if len(chunk) > 2 {
return chunk
}
}
return name
},
"Date": func(layout string) string {
if layout == "" {
layout = time.ANSIC
}
return time.Now().Format(layout)
},
}
}