Change unsubscribe (click instead of onload) behaviour and language

This commit is contained in:
Kailash Nadh 2018-10-31 18:22:54 +05:30
parent 52f8217b77
commit 0fb9c6aed1
4 changed files with 90 additions and 53 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
) )
// Template wraps a template.Template for echo.
type Template struct { type Template struct {
templates *template.Template templates *template.Template
} }
@ -21,7 +22,8 @@ type publicTpl struct {
type unsubTpl struct { type unsubTpl struct {
publicTpl publicTpl
Blacklisted bool Unsubscribe bool
Blacklist bool
} }
type errorTpl struct { type errorTpl struct {
@ -33,8 +35,8 @@ type errorTpl struct {
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}$") 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}$")
// Render executes and renders a template for echo.
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// fmt.Println(t.templates.ExecuteTemplate(os.Stdout, name, nil))
return t.templates.ExecuteTemplate(w, name, data) return t.templates.ExecuteTemplate(w, name, data)
} }
@ -44,37 +46,66 @@ func handleUnsubscribePage(c echo.Context) error {
app = c.Get("app").(*App) app = c.Get("app").(*App)
campUUID = c.Param("campUUID") campUUID = c.Param("campUUID")
subUUID = c.Param("subUUID") subUUID = c.Param("subUUID")
unsub, _ = strconv.ParseBool(c.FormValue("unsubscribe"))
blacklist, _ = strconv.ParseBool(c.FormValue("blacklist")) blacklist, _ = strconv.ParseBool(c.FormValue("blacklist"))
out = unsubTpl{} out = unsubTpl{}
) )
out.Blacklisted = blacklist out.Unsubscribe = unsub
out.Blacklist = blacklist
out.Title = "Unsubscribe from mailing list" out.Title = "Unsubscribe from mailing list"
if !regexValidUUID.MatchString(campUUID) || if !regexValidUUID.MatchString(campUUID) ||
!regexValidUUID.MatchString(subUUID) { !regexValidUUID.MatchString(subUUID) {
err := errorTpl{} return c.Render(http.StatusBadRequest, "error",
err.Title = "Invalid request" makeErrorTpl("Invalid request", "",
err.ErrorTitle = err.Title `The unsubscription request contains invalid IDs.
err.ErrorMessage = "The unsubscription request contains invalid IDs. Please make sure to follow the correct link." Please click on the correct link.`))
return c.Render(http.StatusBadRequest, "error", err)
} }
// Unsubscribe. // Unsubscribe.
if unsub {
res, err := app.Queries.Unsubscribe.Exec(campUUID, subUUID, blacklist) res, err := app.Queries.Unsubscribe.Exec(campUUID, subUUID, blacklist)
if err != nil { if err != nil {
app.Logger.Printf("Error unsubscribing : %v", err) app.Logger.Printf("Error unsubscribing : %v", err)
return echo.NewHTTPError(http.StatusBadRequest, "Subscription doesn't exist") return echo.NewHTTPError(http.StatusBadRequest, "There was an internal error while unsubscribing you.")
} }
num, err := res.RowsAffected() if !blacklist {
num, _ := res.RowsAffected()
if num == 0 { if num == 0 {
err := errorTpl{} return c.Render(http.StatusBadRequest, "error",
err.Title = "Invalid subscription" makeErrorTpl("Already unsubscribed", "",
err.ErrorTitle = err.Title `Looks like you are not subscribed to this mailing list.
err.ErrorMessage = "Looks like you are not subscribed to this mailing list." You may have already unsubscribed.`))
return c.Render(http.StatusBadRequest, "error", err) }
}
} }
return c.Render(http.StatusOK, "unsubscribe", out) return c.Render(http.StatusOK, "unsubscribe", out)
} }
// 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)
}

View File

@ -18,18 +18,25 @@ h1, h2, h3, h4 {
} }
.button { .button {
border: 0; background: #7f2aff;
background: transparent;
padding: 10px 30px; padding: 10px 30px;
border-radius: 3px; border-radius: 3px;
border: 1px solid #ddd; border: 0;
cursor: pointer; cursor: pointer;
text-decoration: none; text-decoration: none;
color: #111; color: #ffff;
display: inline-block; display: inline-block;
} }
.button:hover { .button:hover {
background: #f3f3f3; background: #333;
}
.button .button-outline {
background: transparent;
border: 1px solid #ddd;
color: #444'
}
.button .button-outline:hover {
color: #111;
} }
.wrap { .wrap {

View File

@ -27,20 +27,6 @@
Powered by <a target="_blank" href="https://listmonk.app">listmonk</a> Powered by <a target="_blank" href="https://listmonk.app">listmonk</a>
</footer> </footer>
</div> </div>
<script>
function unsubAll(e) {
if(!confirm("Are you sure?")) {
e.preventDefault();
return false;
}
}
(function() {
document.querySelector("#btn-unsuball").onclick = unsubAll
})();
</script>
</body> </body>
</html> </html>
{{ end }} {{ end }}

View File

@ -1,22 +1,35 @@
{{ define "unsubscribe" }} {{ define "unsubscribe" }}
{{ template "header" .}} {{ template "header" .}}
{{ if not .Unsubscribe }}
<h2>Unsubscribe</h2>
<p>Do you wish to unsubscribe from this mailing list?</p>
<form method="post">
<div>
<input type="hidden" name="unsubscribe" value="true" />
<button type="submit" class="button" id="btn-unsub">Unsubscribe</button>
</div>
</form>
{{ else }}
<h2>You have been unsubscribed</h2> <h2>You have been unsubscribed</h2>
{{ if not .Blacklisted }}
{{ if not .Blacklist }}
<div class="unsub-all"> <div class="unsub-all">
<p> <p>
Unsubscribe from all future communications? Unsubscribe from all future communications?
</p> </p>
<form method="post"> <form method="post">
<div> <div>
<input type="hidden" name="unsubscribe" value="true" />
<input type="hidden" name="blacklist" value="true" /> <input type="hidden" name="blacklist" value="true" />
<button type="submit" class="button" id="btn-unsuball">Unsubscribe all</button> <button type="submit" class="button button-inline" id="btn-unsuball">Unsubscribe all</button>
</div> </div>
</form> </form>
</div> </div>
{{ else }} {{ else }}
<p>You've been unsubscribed from all future communications.</p> <p>You've been unsubscribed from all future communications.</p>
{{ end }} {{ end }}
{{ end }}
{{ template "footer" .}} {{ template "footer" .}}
{{ end }} {{ end }}