2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2018-10-31 12:52:54 +00:00
|
|
|
// Template wraps a template.Template for echo.
|
2018-10-25 13:51:47 +00:00
|
|
|
type Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
type publicTpl struct {
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
type unsubTpl struct {
|
|
|
|
publicTpl
|
2018-10-31 12:52:54 +00:00
|
|
|
Unsubscribe bool
|
|
|
|
Blacklist bool
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type errorTpl struct {
|
|
|
|
publicTpl
|
|
|
|
|
|
|
|
ErrorTitle string
|
|
|
|
ErrorMessage string
|
|
|
|
}
|
|
|
|
|
|
|
|
var regexValidUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
|
|
|
|
|
2018-10-31 12:52:54 +00:00
|
|
|
// Render executes and renders a template for echo.
|
2018-10-25 13:51:47 +00:00
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUnsubscribePage unsubscribes a subscriber and renders a view.
|
|
|
|
func handleUnsubscribePage(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
campUUID = c.Param("campUUID")
|
|
|
|
subUUID = c.Param("subUUID")
|
2018-10-31 12:52:54 +00:00
|
|
|
unsub, _ = strconv.ParseBool(c.FormValue("unsubscribe"))
|
2018-10-25 13:51:47 +00:00
|
|
|
blacklist, _ = strconv.ParseBool(c.FormValue("blacklist"))
|
|
|
|
|
|
|
|
out = unsubTpl{}
|
|
|
|
)
|
2018-10-31 12:52:54 +00:00
|
|
|
out.Unsubscribe = unsub
|
|
|
|
out.Blacklist = blacklist
|
2018-10-25 13:51:47 +00:00
|
|
|
out.Title = "Unsubscribe from mailing list"
|
|
|
|
|
|
|
|
if !regexValidUUID.MatchString(campUUID) ||
|
|
|
|
!regexValidUUID.MatchString(subUUID) {
|
2018-10-31 12:52:54 +00:00
|
|
|
return c.Render(http.StatusBadRequest, "error",
|
|
|
|
makeErrorTpl("Invalid request", "",
|
|
|
|
`The unsubscription request contains invalid IDs.
|
|
|
|
Please click on the correct link.`))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe.
|
2018-10-31 12:52:54 +00:00
|
|
|
if unsub {
|
|
|
|
res, err := app.Queries.Unsubscribe.Exec(campUUID, subUUID, blacklist)
|
|
|
|
if err != nil {
|
|
|
|
app.Logger.Printf("Error unsubscribing : %v", err)
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "There was an internal error while unsubscribing you.")
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-31 12:52:54 +00:00
|
|
|
if !blacklist {
|
|
|
|
num, _ := res.RowsAffected()
|
|
|
|
if num == 0 {
|
|
|
|
return c.Render(http.StatusBadRequest, "error",
|
|
|
|
makeErrorTpl("Already unsubscribed", "",
|
|
|
|
`Looks like you are not subscribed to this mailing list.
|
|
|
|
You may have already unsubscribed.`))
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render(http.StatusOK, "unsubscribe", out)
|
|
|
|
}
|
2018-10-31 12:52:54 +00:00
|
|
|
|
|
|
|
// handleLinkRedirect handles link UUID to real link redirection.
|
|
|
|
func handleLinkRedirect(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
linkUUID = c.Param("linkUUID")
|
|
|
|
campUUID = c.Param("campUUID")
|
|
|
|
subUUID = c.Param("subUUID")
|
|
|
|
)
|
|
|
|
if !regexValidUUID.MatchString(linkUUID) ||
|
|
|
|
!regexValidUUID.MatchString(campUUID) ||
|
|
|
|
!regexValidUUID.MatchString(subUUID) {
|
|
|
|
return c.Render(http.StatusBadRequest, "error",
|
|
|
|
makeErrorTpl("Invalid link", "", "The link you clicked is invalid."))
|
|
|
|
}
|
|
|
|
|
|
|
|
var url string
|
|
|
|
if err := app.Queries.RegisterLinkClick.Get(&url, linkUUID, campUUID, subUUID); err != nil {
|
|
|
|
app.Logger.Printf("error fetching redirect link: %s", err)
|
|
|
|
return c.Render(http.StatusInternalServerError, "error",
|
|
|
|
makeErrorTpl("Error opening link", "", "There was an error opening the link. Please try later."))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusTemporaryRedirect, url)
|
|
|
|
}
|