/
notify.go
288 lines (224 loc) · 5.36 KB
/
notify.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bytes"
"fmt"
"log"
"strings"
"text/template"
"time"
"github.com/jordan-wright/email"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/yaml.v2"
)
// Template tree of notification templates.
var notifyTemplate = template.New("notify")
func init() {
// Template of the email body when new a object is created.
newObject := string(MustAsset("email/new_object_email_body.txt"))
// Template of the email body when an object has changed.
changedObject := string(MustAsset("email/changed_object_email_body.txt"))
// Compile the email bodies.
template.Must(notifyTemplate.New("new-object").Parse(newObject))
template.Must(notifyTemplate.New("changed-object").Parse(changedObject))
}
// EmailContext is the template EmailContext for email bodies.
type EmailContext struct {
Time time.Time
Key string
Version int
URL string
VersionURL string
Object string
Additions string
Removals string
Changes string
}
func newObjectEmail(cfg *Config, o *Object) (*email.Email, error) {
var (
err error
byt []byte
buff bytes.Buffer
)
cxt := EmailContext{
Time: time.Unix(o.Time, 0).Local(),
Key: o.Key,
Version: o.Version,
URL: fmt.Sprintf("http://%s/objects/%s", cfg.HTTP.Addr(), o.Key),
VersionURL: fmt.Sprintf("http://%s/objects/%s/v/%d", cfg.HTTP.Addr(), o.Key, o.Version),
}
byt, _ = yaml.Marshal(o.Value)
cxt.Object = string(byt)
if err = notifyTemplate.Lookup("new-object").Execute(&buff, &cxt); err != nil {
return nil, err
}
e := email.NewEmail()
e.Subject = "[SCDS] New Object"
e.Text = buff.Bytes()
return e, nil
}
func changedObjectEmail(cfg *Config, o *Object, r *Revision) (*email.Email, error) {
var (
err error
byt []byte
buff bytes.Buffer
)
cxt := EmailContext{
Time: time.Unix(r.Time, 0).Local(),
Key: o.Key,
Version: r.Version,
URL: fmt.Sprintf("http://%s/objects/%s", cfg.HTTP.Addr(), o.Key),
VersionURL: fmt.Sprintf("http://%s/objects/%s/v/%d", cfg.HTTP.Addr(), o.Key, r.Version),
}
if r.Changes != nil {
byt, _ = yaml.Marshal(r.Changes)
cxt.Changes = string(byt)
}
if r.Additions != nil {
byt, _ = yaml.Marshal(r.Additions)
cxt.Additions = string(byt)
}
if r.Removals != nil {
byt, _ = yaml.Marshal(r.Removals)
cxt.Removals = string(byt)
}
if err = notifyTemplate.Lookup("changed-object").Execute(&buff, &cxt); err != nil {
return nil, err
}
e := email.NewEmail()
e.Subject = "[SCDS] Object Changed"
e.Text = buff.Bytes()
return e, nil
}
func NotifyEmail(cfg *Config, o *Object, r *Revision) error {
var (
e *email.Email
err error
)
// First version.
if r.Version == 1 {
e, err = newObjectEmail(cfg, o)
} else {
e, err = changedObjectEmail(cfg, o, r)
}
if err != nil {
return err
}
q := bson.M{
"subscribed": true,
}
p := bson.M{
"email": 1,
}
var subs []*Subscriber
if err = cfg.Mongo.Subscribers().Find(q).Select(p).All(&subs); err != nil {
return err
}
// No subscribers.
if len(subs) == 0 {
return nil
}
e.From = cfg.SMTP.From
e.To = make([]string, 1)
for _, sub := range subs {
e.To[0] = sub.Email
if err = e.Send(cfg.SMTP.Addr(), cfg.SMTP.Auth()); err != nil {
log.Printf("error sending email: %s", err)
}
}
return nil
}
// Subscriber
type Subscriber struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"_id,omitempty"`
Email string `json:"email"`
Time time.Time `json:"time"`
}
func AllSubscribers(cfg *Config) ([]*Subscriber, error) {
c := cfg.Mongo.Subscribers()
var subs []*Subscriber
if err := c.Find(nil).All(&subs); err != nil {
return nil, err
}
return subs, nil
}
// SubscribeEmail subscribes one or more email addresses to receive notification
// emails when object events occurs. Returned are the new subscribers or an error
// if one occurred.
func SubscribeEmail(cfg *Config, emails ...string) ([]*Subscriber, error) {
c := cfg.Mongo.Subscribers()
var (
q bson.M
err error
chg mgo.Change
info *mgo.ChangeInfo
sub *Subscriber
subs []*Subscriber
)
// Upsert the subscribers based on the email address. Email
// addresses to lowercased for consistency.
for _, email := range emails {
sub = &Subscriber{
Email: strings.ToLower(email),
Time: time.Now().UTC(),
}
q = bson.M{
"email": sub.Email,
}
chg = mgo.Change{
Upsert: true,
ReturnNew: true,
Update: bson.M{
"$setOnInsert": sub,
},
}
if info, err = c.Find(q).Apply(chg, sub); err != nil {
break
}
// If the document was not updated, it was inserted.
if info.Updated == 0 {
subs = append(subs, sub)
}
}
return subs, err
}
// UnsubscribeEmail unsubscribes email addresses from receiving notifications.
func UnsubscribeEmail(cfg *Config, emails ...string) (int, error) {
c := cfg.Mongo.Subscribers()
var (
n int
err error
q bson.M
)
for _, email := range emails {
email = strings.ToLower(email)
q = bson.M{
"email": email,
}
err = c.Remove(q)
// No subscriber with the email.
if err == mgo.ErrNotFound {
continue
}
if err != nil {
break
}
n++
}
return n, err
}
func UnsubscribeID(cfg *Config, id bson.ObjectId) (bool, error) {
c := cfg.Mongo.Subscribers()
q := bson.M{
"_id": id,
}
err := c.Remove(q)
// No subscriber with the id.
if err == mgo.ErrNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}