2021-08-08 02:38:37 +00:00
|
|
|
package api
|
2021-08-02 21:21:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2022-05-14 22:14:11 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutil"
|
2021-08-02 21:21:12 +00:00
|
|
|
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
|
|
|
|
)
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
func (a *api) mailingListSubscribeHandler() http.Handler {
|
2021-08-02 21:21:12 +00:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
email := r.PostFormValue("email")
|
|
|
|
if parts := strings.Split(email, "@"); len(parts) != 2 ||
|
|
|
|
parts[0] == "" ||
|
|
|
|
parts[1] == "" ||
|
|
|
|
len(email) >= 512 {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.BadRequest(rw, r, errors.New("invalid email"))
|
2021-08-03 21:20:32 +00:00
|
|
|
return
|
2021-08-02 21:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
err := a.params.MailingList.BeginSubscription(email)
|
|
|
|
|
|
|
|
if errors.Is(err, mailinglist.ErrAlreadyVerified) {
|
2021-08-02 21:21:12 +00:00
|
|
|
// just eat the error, make it look to the user like the
|
|
|
|
// verification email was sent.
|
|
|
|
} else if err != nil {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.InternalServerError(rw, r, err)
|
2021-08-03 21:20:32 +00:00
|
|
|
return
|
2021-08-02 21:21:12 +00:00
|
|
|
}
|
2021-08-03 21:20:32 +00:00
|
|
|
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.JSONResult(rw, r, struct{}{})
|
2021-08-02 21:21:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
func (a *api) mailingListFinalizeHandler() http.Handler {
|
2021-08-03 22:28:22 +00:00
|
|
|
var errInvalidSubToken = errors.New("invalid subToken")
|
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
subToken := r.PostFormValue("subToken")
|
|
|
|
if l := len(subToken); l == 0 || l > 128 {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.BadRequest(rw, r, errInvalidSubToken)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
err := a.params.MailingList.FinalizeSubscription(subToken)
|
2021-08-03 22:28:22 +00:00
|
|
|
|
|
|
|
if errors.Is(err, mailinglist.ErrNotFound) {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.BadRequest(rw, r, errInvalidSubToken)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
2021-08-03 22:28:22 +00:00
|
|
|
|
|
|
|
} else if errors.Is(err, mailinglist.ErrAlreadyVerified) {
|
|
|
|
// no problem
|
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
} else if err != nil {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.InternalServerError(rw, r, err)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 21:20:32 +00:00
|
|
|
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.JSONResult(rw, r, struct{}{})
|
2021-08-02 21:21:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
func (a *api) mailingListUnsubscribeHandler() http.Handler {
|
2021-08-03 22:28:22 +00:00
|
|
|
var errInvalidUnsubToken = errors.New("invalid unsubToken")
|
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
unsubToken := r.PostFormValue("unsubToken")
|
|
|
|
if l := len(unsubToken); l == 0 || l > 128 {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.BadRequest(rw, r, errInvalidUnsubToken)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:38:37 +00:00
|
|
|
err := a.params.MailingList.Unsubscribe(unsubToken)
|
2021-08-03 22:28:22 +00:00
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
if errors.Is(err, mailinglist.ErrNotFound) {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.BadRequest(rw, r, errInvalidUnsubToken)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
2021-08-03 22:28:22 +00:00
|
|
|
|
2021-08-02 21:21:12 +00:00
|
|
|
} else if err != nil {
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.InternalServerError(rw, r, err)
|
2021-08-02 21:21:12 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-03 21:20:32 +00:00
|
|
|
|
2022-05-14 22:14:11 +00:00
|
|
|
apiutil.JSONResult(rw, r, struct{}{})
|
2021-08-02 21:21:12 +00:00
|
|
|
})
|
|
|
|
}
|