1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package http
import (
"errors"
"net/http"
"strings"
"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
"github.com/mediocregopher/blog.mediocregopher.com/srv/mailinglist"
)
func (a *api) mailingListSubscribeHandler() http.Handler {
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 {
apiutil.BadRequest(rw, r, errors.New("invalid email"))
return
} else if strings.ToLower(parts[1]) == "gmail.com" {
apiutil.BadRequest(rw, r, errors.New("gmail does not allow its users to receive email from me, sorry"))
return
}
err := a.params.MailingList.BeginSubscription(email)
if errors.Is(err, mailinglist.ErrAlreadyVerified) {
// just eat the error, make it look to the user like the
// verification email was sent.
} else if err != nil {
apiutil.InternalServerError(rw, r, err)
return
}
apiutil.JSONResult(rw, r, struct{}{})
})
}
func (a *api) mailingListFinalizeHandler() http.Handler {
var errInvalidSubToken = errors.New("invalid subToken")
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
subToken := r.PostFormValue("subToken")
if l := len(subToken); l == 0 || l > 128 {
apiutil.BadRequest(rw, r, errInvalidSubToken)
return
}
err := a.params.MailingList.FinalizeSubscription(subToken)
if errors.Is(err, mailinglist.ErrNotFound) {
apiutil.BadRequest(rw, r, errInvalidSubToken)
return
} else if errors.Is(err, mailinglist.ErrAlreadyVerified) {
// no problem
} else if err != nil {
apiutil.InternalServerError(rw, r, err)
return
}
apiutil.JSONResult(rw, r, struct{}{})
})
}
func (a *api) mailingListUnsubscribeHandler() http.Handler {
var errInvalidUnsubToken = errors.New("invalid unsubToken")
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
unsubToken := r.PostFormValue("unsubToken")
if l := len(unsubToken); l == 0 || l > 128 {
apiutil.BadRequest(rw, r, errInvalidUnsubToken)
return
}
err := a.params.MailingList.Unsubscribe(unsubToken)
if errors.Is(err, mailinglist.ErrNotFound) {
apiutil.BadRequest(rw, r, errInvalidUnsubToken)
return
} else if err != nil {
apiutil.InternalServerError(rw, r, err)
return
}
apiutil.JSONResult(rw, r, struct{}{})
})
}
|