/
email.go
579 lines (489 loc) · 13.7 KB
/
email.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package main
import (
"bytes"
"fmt"
"net/url"
"text/template"
"github.com/dajohi/goemail"
"github.com/HcashOrg/hcAutonomy/hcAutonomywww/api/v1"
"github.com/HcashOrg/hcAutonomy/hcAutonomywww/database"
)
const (
fromAddress = "autonomy@h.cash"
hcAutonomyMailName = "HcAutonomy"
)
func createBody(tpl *template.Template, tplData interface{}) (string, error) {
var buf bytes.Buffer
err := tpl.Execute(&buf, tplData)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (b *backend) createEmailLink(path, email, token string) (string, error) {
l, err := url.Parse(b.cfg.WebServerAddress + path)
if err != nil {
return "", err
}
q := l.Query()
if email != "" {
q.Set("email", email)
}
if token != "" {
q.Set("verificationtoken", token)
}
l.RawQuery = q.Encode()
return l.String(), nil
}
// sendEmail sends an email with the given subject and body, and the caller
// must supply a function which is used to add email addresses to send the
// email to.
func (b *backend) sendEmail(
subject, body string,
addToAddressesFn func(*goemail.Message) error,
) error {
//msg := goemail.NewMessage(fromAddress, subject, body)
msg := goemail.NewHTMLMessage(fromAddress, subject, body)
err := addToAddressesFn(msg)
if err != nil {
return err
}
msg.SetName(hcAutonomyMailName)
err = b.cfg.SMTP.Send(msg)
if err.Error() != "250 Data Ok: queued as freedom" {
return err
}
return nil
}
// sendEmailTo sends an email with the given subject and body to a
// single address.
func (b *backend) sendEmailTo(subject, body, toAddress string) error {
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
msg.AddTo(toAddress)
return nil
})
}
// emailNewUserVerificationLink emails the link with the new user verification token
// if the email server is set up.
func (b *backend) emailNewUserVerificationLink(email, token, username string) error {
if b.cfg.SMTP == nil {
return nil
}
link, err := b.createEmailLink(v1.RouteVerifyNewUser, email,
token)
if err != nil {
return err
}
tplData := newUserEmailTemplateData{
Username: username,
Email: email,
Link: link,
}
subject := "Verify Your Email"
body, err := createBody(templateNewUserEmail, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, email)
}
// emailResetPasswordVerificationLink emails the link with the reset password
// verification token if the email server is set up.
func (b *backend) emailResetPasswordVerificationLink(email, token string) error {
if b.cfg.SMTP == nil {
return nil
}
link, err := b.createEmailLink(v1.RouteResetPassword, email,
token)
if err != nil {
return err
}
tplData := resetPasswordEmailTemplateData{
Email: email,
Link: link,
}
subject := "Reset Your Password"
body, err := createBody(templateResetPasswordEmail, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, email)
}
// emailAuthorForVettedProposal sends an email notification for a new
// proposal becoming vetted to the proposal's author.
func (b *backend) emailAuthorForVettedProposal(
proposal *v1.ProposalRecord,
authorUser *database.User,
adminUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" +
proposal.CensorshipRecord.Token)
if err != nil {
return err
}
if authorUser.EmailNotifications&
uint64(v1.NotificationEmailMyProposalStatusChange) == 0 {
return nil
}
tplData := proposalStatusChangeTemplateData{
Link: l.String(),
Name: proposal.Name,
StatusChangeReason: proposal.StatusChangeMessage,
}
subject := "Your Proposal Has Been Published"
body, err := createBody(templateProposalVettedForAuthor, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, authorUser.Email)
}
// emailAuthorForCensoredProposal sends an email notification for a new
// proposal becoming censored to the proposal's author.
func (b *backend) emailAuthorForCensoredProposal(
proposal *v1.ProposalRecord,
authorUser *database.User,
adminUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" +
proposal.CensorshipRecord.Token)
if err != nil {
return err
}
if authorUser.EmailNotifications&
uint64(v1.NotificationEmailMyProposalStatusChange) == 0 {
return nil
}
tplData := proposalStatusChangeTemplateData{
Link: l.String(),
Name: proposal.Name,
StatusChangeReason: proposal.StatusChangeMessage,
}
subject := "Your Proposal Has Been Censored"
body, err := createBody(templateProposalCensoredForAuthor, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, authorUser.Email)
}
// emailUsersForVettedProposal sends an email notification for a new
// proposal becoming vetted.
func (b *backend) emailUsersForVettedProposal(
proposal *v1.ProposalRecord,
authorUser *database.User,
adminUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
// Create the template data.
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" +
proposal.CensorshipRecord.Token)
if err != nil {
return err
}
tplData := proposalStatusChangeTemplateData{
Link: l.String(),
Name: proposal.Name,
Username: authorUser.Username,
}
// Send email to users.
subject := "New Proposal Published"
body, err := createBody(templateProposalVetted, &tplData)
if err != nil {
return err
}
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
// Add user emails to the goemail.Message
return b.db.AllUsers(func(user *database.User) {
// Don't notify the user under certain conditions.
if user.NewUserPaywallTx == "" || user.Deactivated ||
user.ID == adminUser.ID || user.ID == authorUser.ID ||
(user.EmailNotifications&
uint64(v1.NotificationEmailRegularProposalVetted)) == 0 {
return
}
msg.AddBCC(user.Email)
})
})
}
// emailUsersForEditedProposal sends an email notification for a proposal
// being edited.
func (b *backend) emailUsersForEditedProposal(
proposal *v1.ProposalRecord,
authorUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
// Create the template data.
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" +
proposal.CensorshipRecord.Token)
if err != nil {
return err
}
tplData := proposalEditedTemplateData{
Link: l.String(),
Name: proposal.Name,
Version: proposal.Version,
Username: authorUser.Username,
}
// Send email to users.
subject := "Proposal Edited"
body, err := createBody(templateProposalEdited, &tplData)
if err != nil {
return err
}
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
// Add user emails to the goemail.Message
return b.db.AllUsers(func(user *database.User) {
// Don't notify the user under certain conditions.
if user.NewUserPaywallTx == "" || user.Deactivated ||
user.ID == authorUser.ID ||
(user.EmailNotifications&
uint64(v1.NotificationEmailRegularProposalEdited)) == 0 {
return
}
msg.AddBCC(user.Email)
})
})
}
// emailUsersForProposalVoteStarted sends an email notification for a proposal
// entering the voting state.
func (b *backend) emailUsersForProposalVoteStarted(
proposal *v1.ProposalRecord,
authorUser *database.User,
adminUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
// Create the template data.
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" +
proposal.CensorshipRecord.Token)
if err != nil {
return err
}
tplData := proposalVoteStartedTemplateData{
Link: l.String(),
Name: proposal.Name,
Username: authorUser.Username,
}
// Send email to author.
if authorUser.EmailNotifications&
uint64(v1.NotificationEmailMyProposalVoteStarted) != 0 {
subject := "Your Proposal Has Started Voting"
body, err := createBody(templateProposalVoteStartedForAuthor, &tplData)
if err != nil {
return err
}
err = b.sendEmailTo(subject, body, authorUser.Email)
if err != nil {
return err
}
}
subject := "Voting Started for Proposal"
body, err := createBody(templateProposalVoteStarted, &tplData)
if err != nil {
return err
}
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
// Add user emails to the goemail.Message
return b.db.AllUsers(func(user *database.User) {
// Don't notify the user under certain conditions.
if user.NewUserPaywallTx == "" || user.Deactivated ||
user.ID == adminUser.ID ||
user.ID == authorUser.ID ||
(user.EmailNotifications&
uint64(v1.NotificationEmailRegularProposalVoteStarted)) == 0 {
return
}
msg.AddBCC(user.Email)
})
})
}
func (b *backend) emailAdminsForNewSubmittedProposal(token string, propName string, username string, userEmail string) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" + token)
if err != nil {
return err
}
tplData := newProposalSubmittedTemplateData{
Link: l.String(),
Name: propName,
Username: username,
Email: userEmail,
}
subject := "New Proposal Submitted"
body, err := createBody(templateNewProposalSubmitted, &tplData)
if err != nil {
return err
}
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
// Add admin emails to the goemail.Message
return b.db.AllUsers(func(user *database.User) {
if !user.Admin || user.Deactivated ||
(user.EmailNotifications&
uint64(v1.NotificationEmailAdminProposalNew) == 0) {
return
}
msg.AddBCC(user.Email)
})
})
}
func (b *backend) emailAdminsForProposalVoteAuthorized(
proposal *v1.ProposalRecord,
authorUser *database.User,
) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(b.cfg.WebServerAddress + "/proposals/" + proposal.CensorshipRecord.Token)
if err != nil {
return err
}
tplData := proposalVoteAuthorizedTemplateData{
Link: l.String(),
Name: proposal.Name,
Username: authorUser.Username,
Email: authorUser.Email,
}
subject := "Proposal Authorized To Start Voting"
body, err := createBody(templateProposalVoteAuthorized, &tplData)
if err != nil {
return err
}
return b.sendEmail(subject, body, func(msg *goemail.Message) error {
// Add admin emails to the goemail.Message
return b.db.AllUsers(func(user *database.User) {
if !user.Admin || user.Deactivated ||
(user.EmailNotifications&
uint64(v1.NotificationEmailAdminProposalVoteAuthorized) == 0) {
return
}
msg.AddBCC(user.Email)
})
})
}
// emailAuthorForCommentOnProposal sends an email notification to a proposal
// author for a new comment.
func (b *backend) emailAuthorForCommentOnProposal(
proposal *v1.ProposalRecord,
authorUser *database.User,
commentID, username string,
) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(fmt.Sprintf("%v/proposals/%v/comments/%v",
b.cfg.WebServerAddress, proposal.CensorshipRecord.Token, commentID))
if err != nil {
return err
}
if authorUser.EmailNotifications&
uint64(v1.NotificationEmailCommentOnMyProposal) == 0 {
return nil
}
// Don't send email when author comments on own proposal
if username == authorUser.Username {
return nil
}
tplData := commentReplyOnProposalTemplateData{
Commenter: username,
ProposalName: proposal.Name,
CommentLink: l.String(),
}
subject := "New Comment On Your Proposal"
body, err := createBody(templateCommentReplyOnProposal, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, authorUser.Email)
}
// emailAuthorForCommentOnComment sends an email notification to a comment
// author for a new comment reply.
func (b *backend) emailAuthorForCommentOnComment(
proposal *v1.ProposalRecord,
authorUser *database.User,
commentID, username string,
) error {
if b.cfg.SMTP == nil {
return nil
}
l, err := url.Parse(fmt.Sprintf("%v/proposals/%v/comments/%v",
b.cfg.WebServerAddress, proposal.CensorshipRecord.Token, commentID))
if err != nil {
return err
}
if authorUser.EmailNotifications&
uint64(v1.NotificationEmailCommentOnMyComment) == 0 {
return nil
}
// Don't send email when author replies to his own comment
if username == authorUser.Username {
return nil
}
tplData := commentReplyOnCommentTemplateData{
Commenter: username,
ProposalName: proposal.Name,
CommentLink: l.String(),
}
subject := "New Comment On Your Comment"
body, err := createBody(templateCommentReplyOnComment, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, authorUser.Email)
}
// emailUpdateUserKeyVerificationLink emails the link with the verification
// token used for setting a new key pair if the email server is set up.
func (b *backend) emailUpdateUserKeyVerificationLink(email, publicKey, token string) error {
if b.cfg.SMTP == nil {
return nil
}
link, err := b.createEmailLink(v1.RouteVerifyUpdateUserKey,
"", token)
if err != nil {
return err
}
tplData := updateUserKeyEmailTemplateData{
Email: email,
PublicKey: publicKey,
Link: link,
}
subject := "Verify Your New Identity"
body, err := createBody(templateUpdateUserKeyEmail, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, email)
}
// emailUserLocked notifies the user its account has been locked and emails the
// link with the reset password verification token if the email server is set
// up.
func (b *backend) emailUserLocked(email string) error {
if b.cfg.SMTP == nil {
return nil
}
link, err := b.createEmailLink(ResetPasswordGuiRoute,
email, "")
if err != nil {
return err
}
tplData := userLockedResetPasswordEmailTemplateData{
Email: email,
Link: link,
}
subject := "Locked Account - Reset Your Password"
body, err := createBody(templateUserLockedResetPassword, &tplData)
if err != nil {
return err
}
return b.sendEmailTo(subject, body, email)
}