Serve mailing list finalize and unsubscribe endpoints

This commit is contained in:
Brian Picciano 2022-05-21 14:03:38 -06:00
parent 7335295dc0
commit 4dc1683d3e
4 changed files with 93 additions and 2 deletions

View File

@ -216,6 +216,8 @@ func (a *api) blogHandler() http.Handler {
mux.Handle("/static/", http.FileServer(http.FS(staticFS))) mux.Handle("/static/", http.FileServer(http.FS(staticFS)))
mux.Handle("/follow", a.renderDumbTplHandler("follow.html")) mux.Handle("/follow", a.renderDumbTplHandler("follow.html"))
mux.Handle("/mailinglist/unsubscribe", a.renderDumbTplHandler("unsubscribe.html"))
mux.Handle("/mailinglist/finalize", a.renderDumbTplHandler("finalize.html"))
mux.Handle("/feed.xml", a.renderFeedHandler()) mux.Handle("/feed.xml", a.renderFeedHandler())
mux.Handle("/", a.renderIndexHandler()) mux.Handle("/", a.renderIndexHandler())

View File

@ -0,0 +1,45 @@
{{ define "body" }}
<script async type="module" src="{{ StaticURL "api.js" }}"></script>
<style>
#result.success { color: green; }
#result.fail { color: red; }
</style>
<span id="result"></span>
<script>
(async () => {
const resultSpan = document.getElementById("result");
try {
const urlParams = new URLSearchParams(window.location.search);
const subToken = urlParams.get('subToken');
if (!subToken) throw "No subscription token provided";
const api = await import("{{ StaticURL "api.js" }}");
await api.call('/api/mailinglist/finalize', {
body: { subToken },
});
resultSpan.className = "success";
resultSpan.innerHTML = "Your email subscription has been finalized! Please go on about your day.";
} catch (e) {
resultSpan.className = "fail";
resultSpan.innerHTML = e;
}
})();
</script>
{{ end }}
{{ template "base.html" . }}

View File

@ -0,0 +1,44 @@
{{ define "body" }}
<script async type="module" src="{{ StaticURL "api.js" }}"></script>
<style>
#result.success { color: green; }
#result.fail { color: red; }
</style>
<span id="result"></span>
<script>
(async () => {
const resultSpan = document.getElementById("result");
try {
const urlParams = new URLSearchParams(window.location.search);
const unsubToken = urlParams.get('unsubToken');
if (!unsubToken) throw "No unsubscribe token provided";
const api = await import("{{ StaticURL "api.js" }}");
await api.call('/api/mailinglist/unsubscribe', {
body: { unsubToken },
});
resultSpan.className = "success";
resultSpan.innerHTML = "You have been unsubscribed! Please go on about your day.";
} catch (e) {
resultSpan.className = "fail";
resultSpan.innerHTML = e;
}
})();
</script>
{{ end }}
{{ template "base.html" . }}

View File

@ -126,7 +126,7 @@ func (m *mailingList) BeginSubscription(email string) error {
SubLink string SubLink string
}{ }{
SubLink: fmt.Sprintf( SubLink: fmt.Sprintf(
"%s/mailinglist/finalize.html?subToken=%s", "%s/mailinglist/finalize?subToken=%s",
m.params.PublicURL.String(), m.params.PublicURL.String(),
emailRecord.SubToken, emailRecord.SubToken,
), ),
@ -242,7 +242,7 @@ func (m *mailingList) Publish(postTitle, postURL string) error {
PostTitle: postTitle, PostTitle: postTitle,
PostURL: postURL, PostURL: postURL,
UnsubURL: fmt.Sprintf( UnsubURL: fmt.Sprintf(
"%s/mailinglist/unsubscribe.html?unsubToken=%s", "%s/mailinglist/unsubscribe?unsubToken=%s",
m.params.PublicURL.String(), m.params.PublicURL.String(),
emailRecord.UnsubToken, emailRecord.UnsubToken,
), ),