-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailchimp.go
More file actions
155 lines (129 loc) · 3.79 KB
/
mailchimp.go
File metadata and controls
155 lines (129 loc) · 3.79 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
type MailchimpClient struct {
ApiKey string
// for example "us16"
Server string
ListId string
}
type MailchimpMemberResponse struct {
Status string `json:"status"`
}
func (m *MailchimpClient) CheckIfMemberOnList(email string) (bool, error) {
if m == nil {
return false, fmt.Errorf("mailchimp not configured")
}
subscriberHash := emailToSubscriberHash(email)
url := fmt.Sprintf("https://%s.api.mailchimp.com/3.0/lists/%s/members/%s", m.Server, m.ListId, subscriberHash)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("Mailchimp: check if member on list:", err)
return false, nil
}
req.SetBasicAuth("anystring", m.ApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Mailchimp: check if member on list:", err)
return false, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode == http.StatusOK {
var response MailchimpMemberResponse
if err := json.Unmarshal(body, &response); err != nil {
return false, fmt.Errorf("failed to parse JSON: %v", err)
}
return response.Status == "subscribed", nil
} else {
return false, nil
}
}
type AddMemberToListRequestData struct {
EmailAddress string `json:"email_address"`
Status string `json:"status"`
}
func (m *MailchimpClient) AddMemberToList(email string) error {
if m == nil {
return fmt.Errorf("mailchimp not configured")
}
data := AddMemberToListRequestData{
EmailAddress: email,
Status: "subscribed",
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Println("Mailchimp: add member to list:", err)
return err
}
subscriberHash := emailToSubscriberHash(email)
url := fmt.Sprintf("https://%s.api.mailchimp.com/3.0/lists/%s/members/%s", m.Server, m.ListId, subscriberHash)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
if err != nil {
log.Println("Mailchimp: add member to list:", err)
return err
}
req.SetBasicAuth("anystring", m.ApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Mailchimp: add member to list:", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mailchimp: add member to list:", err)
return fmt.Errorf("failed to read response body: %v", err)
}
log.Println("Mailchimp: add member to list:", string(body))
return fmt.Errorf("failed to add member to list: %s", body)
}
return nil
}
// The subscriber hash is "The MD5 hash of the lowercase version of the list member's email address."
func emailToSubscriberHash(email string) string {
lowerEmail := strings.ToLower(email)
hash := md5.Sum([]byte(lowerEmail))
subscriberHash := hex.EncodeToString(hash[:])
return subscriberHash
}
func (r *Router) SetMailchimp(w http.ResponseWriter, req *http.Request) {
userId, _ := getUserIDFromContext(req.Context())
row := r.db.QueryRow("SELECT name_num FROM users WHERE buck_id = ?1", userId)
var nameNum string
err := row.Scan(&nameNum)
if err != nil {
log.Println("Failed to get user:", err, userId)
http.Redirect(w, req, "/signout", http.StatusTemporaryRedirect)
return
}
email := nameNum + "@osu.edu"
err = r.mailchimp.AddMemberToList(email)
if err != nil {
http.Error(w, "Failed to add member to list", http.StatusInternalServerError)
return
}
err = Templates.ExecuteTemplate(w, "mailchimp.html.tpl", map[string]any{
"isOnMailingList": true,
})
if err != nil {
log.Println("Failed to render template:", err)
http.Error(w, "Failed to render template", http.StatusInternalServerError)
return
}
}