Skip to content

Commit

Permalink
Add support for hosted messages and {{ MessageURL }} tpl tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Apr 26, 2020
1 parent 3a9a2ef commit f498cdd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 869 deletions.
2 changes: 2 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func registerHTTPHandlers(e *echo.Echo) {
"subUUID"))
e.GET("/link/:linkUUID/:campUUID/:subUUID", validateUUID(handleLinkRedirect,
"linkUUID", "campUUID", "subUUID"))
e.GET("/campaign/:campUUID/:subUUID", validateUUID(handleViewCampaignMessage,
"campUUID", "subUUID"))
e.GET("/campaign/:campUUID/:subUUID/px.png", validateUUID(handleRegisterCampaignView,
"campUUID", "subUUID"))

Expand Down
5 changes: 5 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type constants struct {
LinkTrackURL string
ViewTrackURL string
OptinURL string
MessageURL string
}

func initConstants() *constants {
Expand All @@ -165,6 +166,9 @@ func initConstants() *constants {
// url.com/link/{campaign_uuid}/{subscriber_uuid}/{link_uuid}
c.LinkTrackURL = fmt.Sprintf("%s/link/%%s/%%s/%%s", c.RootURL)

// url.com/link/{campaign_uuid}/{subscriber_uuid}
c.MessageURL = fmt.Sprintf("%s/campaign/%%s/%%s", c.RootURL)

// url.com/campaign/{campaign_uuid}/{subscriber_uuid}/px.png
c.ViewTrackURL = fmt.Sprintf("%s/campaign/%%s/%%s/px.png", c.RootURL)

Expand Down Expand Up @@ -193,6 +197,7 @@ func initCampaignManager(q *Queries, cs *constants, app *App) *manager.Manager {
OptinURL: cs.OptinURL,
LinkTrackURL: cs.LinkTrackURL,
ViewTrackURL: cs.ViewTrackURL,
MessageURL: cs.MessageURL,
}, newManagerDB(q), campNotifCB, lo)

}
Expand Down
4 changes: 4 additions & 0 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Config struct {
LinkTrackURL string
UnsubURL string
OptinURL string
MessageURL string
ViewTrackURL string
}

Expand Down Expand Up @@ -312,6 +313,9 @@ func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {
// TODO: Show private lists list on optin e-mail
return fmt.Sprintf(m.cfg.OptinURL, msg.Subscriber.UUID, "")
},
"MessageURL": func(msg *CampaignMessage) string {
return fmt.Sprintf(m.cfg.MessageURL, c.UUID, msg.Subscriber.UUID)
},
"Date": func(layout string) string {
if layout == "" {
layout = time.ANSIC
Expand Down
2 changes: 1 addition & 1 deletion models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var regTplFuncs = []regTplFunc{
replace: `{{ TrackLink "$3" . }}`,
},
regTplFunc{
regExp: regexp.MustCompile(`{{(\s+)?(TrackView|UnsubscribeURL|OptinURL)(\s+)?}}`),
regExp: regexp.MustCompile(`{{(\s+)?(TrackView|UnsubscribeURL|OptinURL|MessageURL)(\s+)?}}`),
replace: `{{ $2 . }}`,
},
}
Expand Down
52 changes: 52 additions & 0 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,58 @@ func (t *tplRenderer) Render(w io.Writer, name string, data interface{}, c echo.
})
}

// handleViewCampaignMessage renders the HTML view of a campaign message.
func handleViewCampaignMessage(c echo.Context) error {
var (
app = c.Get("app").(*App)
campUUID = c.Param("campUUID")
subUUID = c.Param("subUUID")
)

// Get the campaign.
var camp models.Campaign
if err := app.queries.GetCampaign.Get(&camp, 0, campUUID); err != nil {
if err == sql.ErrNoRows {
return c.Render(http.StatusNotFound, tplMessage,
makeMsgTpl("Not found", "", `The e-mail campaign was not found.`))
}

app.log.Printf("error fetching campaign: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl("Error", "", `Error fetching e-mail campaign.`))
}

// Get the subscriber.
var sub models.Subscriber
if err := app.queries.GetSubscriber.Get(&sub, 0, subUUID); err != nil {
if err == sql.ErrNoRows {
return c.Render(http.StatusNotFound, tplMessage,
makeMsgTpl("Not found", "", `The e-mail message was not found.`))
}

app.log.Printf("error fetching campaign subscriber: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl("Error", "", `Error fetching e-mail message.`))
}

// Compile the template.
if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
app.log.Printf("error compiling template: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl("Error", "", `Error compiling e-mail template.`))
}

// Render the message body.
m := app.manager.NewCampaignMessage(&camp, sub)
if err := m.Render(); err != nil {
app.log.Printf("error rendering message: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl("Error", "", `Error rendering e-mail message.`))
}

return c.HTML(http.StatusOK, string(m.Body()))
}

// handleSubscriptionPage renders the subscription management page and
// handles unsubscriptions.
func handleSubscriptionPage(c echo.Context) error {
Expand Down
Loading

0 comments on commit f498cdd

Please sign in to comment.