-
Notifications
You must be signed in to change notification settings - Fork 386
/
chore.go
239 lines (204 loc) · 8.06 KB
/
chore.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package emailreminders
import (
"context"
"fmt"
"strings"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
"storj.io/storj/private/post"
"storj.io/storj/satellite/analytics"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consoleauth"
"storj.io/storj/satellite/console/consoleweb/consoleapi"
"storj.io/storj/satellite/mailservice"
)
var mon = monkit.Package()
// Config contains configurations for email reminders.
type Config struct {
FirstVerificationReminder time.Duration `help:"amount of time before sending first reminder to users who need to verify their email" default:"24h"`
SecondVerificationReminder time.Duration `help:"amount of time before sending second reminder to users who need to verify their email" default:"120h"`
TrialExpirationReminder time.Duration `help:"amount of time before trial expiration to send trial expiration reminder" default:"72h"`
ChoreInterval time.Duration `help:"how often to send reminders to users who need to verify their email" default:"24h"`
EnableTrialExpirationReminders bool `help:"enable sending emails about trial expirations" default:"false"`
Enable bool `help:"enable sending emails reminding users to verify their email" default:"true"`
}
// Chore checks whether any emails need to be re-sent.
//
// architecture: Chore
type Chore struct {
log *zap.Logger
Loop *sync2.Cycle
tokens *consoleauth.Service
usersDB console.Users
mailService *mailservice.Service
config Config
address string
supportURL string
scheduleMeetingURL string
useBlockingSend bool
}
// NewChore instantiates Chore.
func NewChore(log *zap.Logger, tokens *consoleauth.Service, usersDB console.Users, mailservice *mailservice.Service, config Config, address, supportURL, scheduleMeetingURL string) *Chore {
if !strings.HasSuffix(address, "/") {
address += "/"
}
return &Chore{
log: log,
Loop: sync2.NewCycle(config.ChoreInterval),
tokens: tokens,
usersDB: usersDB,
config: config,
mailService: mailservice,
address: address,
supportURL: supportURL,
scheduleMeetingURL: scheduleMeetingURL,
useBlockingSend: false,
}
}
// Run starts the chore.
func (chore *Chore) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return chore.Loop.Run(ctx, func(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
err = chore.sendVerificationReminders(ctx)
if err != nil {
chore.log.Error("sending email verification reminders", zap.Error(err))
}
if chore.config.EnableTrialExpirationReminders {
err = chore.sendExpirationNotifications(ctx)
if err != nil {
chore.log.Error("sending trial expiration notices", zap.Error(err))
}
}
return nil
})
}
func (chore *Chore) sendVerificationReminders(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
now := time.Now()
// cutoff to avoid emailing users multiple times due to email duplicates in the DB.
// TODO: remove cutoff once duplicates are removed.
cutoff := now.Add(30 * (-24 * time.Hour))
users, err := chore.usersDB.GetUnverifiedNeedingReminder(ctx, now.Add(-chore.config.FirstVerificationReminder), now.Add(-chore.config.SecondVerificationReminder), cutoff)
if err != nil {
return errs.New("error getting users in need of verification reminder: %w", err)
}
mon.IntVal("unverified_needing_reminder").Observe(int64(len(users)))
for _, u := range users {
token, err := chore.tokens.CreateToken(ctx, u.ID, u.Email)
if err != nil {
return errs.New("error generating activation token: %w", err)
}
authController := consoleapi.NewAuth(chore.log, nil, nil, nil, nil, nil, "", chore.address, "", "", "", "", false, nil)
link := authController.ActivateAccountURL + "?token=" + token
err = chore.sendEmail(ctx, u.Email, &console.AccountActivationEmail{
ActivationLink: link,
Origin: authController.ExternalAddress,
})
if err != nil {
chore.log.Error("error sending verification reminder", zap.Error(err))
continue
}
if err = chore.usersDB.UpdateVerificationReminders(ctx, u.ID); err != nil {
chore.log.Error("error updating user's last email verifcation reminder", zap.Error(err))
}
}
return nil
}
func (chore *Chore) sendExpirationNotifications(ctx context.Context) (err error) {
mon.Task()(&ctx)(&err)
now := time.Now()
expiring := console.TrialExpirationReminder
// get free trial users needing reminder expiration is approaching.
users, err := chore.usersDB.GetExpiresBeforeWithStatus(ctx, console.NoTrialNotification, now.Add(chore.config.TrialExpirationReminder))
if err != nil {
chore.log.Error("error getting users in need of upcoming expiration warning", zap.Error(err))
return nil
}
mon.IntVal("expiring_needing_reminder").Observe(int64(len(users)))
expirationWarning := &console.TrialExpirationReminderEmail{
SignInLink: chore.address + fmt.Sprintf("login?source=%s", analytics.SourceTrialExpiringNotice),
Origin: chore.address,
ContactInfoURL: chore.supportURL,
ScheduleMeetingLink: chore.scheduleMeetingURL,
}
for _, u := range users {
if err := chore.sendEmail(ctx, u.Email, expirationWarning); err != nil {
chore.log.Error("error sending trial expiration reminder", zap.Error(err))
continue
}
if err = chore.usersDB.Update(ctx, u.ID, console.UpdateUserRequest{TrialNotifications: &expiring}); err != nil {
chore.log.Error("error updating user's trial_notifications", zap.Error(err))
}
}
expired := console.TrialExpired
// get free trial users needing notification that trial is expired
users, err = chore.usersDB.GetExpiresBeforeWithStatus(ctx, console.TrialExpirationReminder, now)
if err != nil {
chore.log.Error("error getting users in need of expiration notice", zap.Error(err))
return nil
}
mon.IntVal("expired_needing_notice").Observe(int64(len(users)))
expirationNotice := &console.TrialExpiredEmail{
SignInLink: chore.address + fmt.Sprintf("login?source=%s", analytics.SourceTrialExpiredNotice),
Origin: chore.address,
ContactInfoURL: chore.supportURL,
ScheduleMeetingLink: chore.scheduleMeetingURL,
}
for _, u := range users {
if err := chore.sendEmail(ctx, u.Email, expirationNotice); err != nil {
chore.log.Error("error sending trial expiration reminder", zap.Error(err))
continue
}
if err = chore.usersDB.Update(ctx, u.ID, console.UpdateUserRequest{TrialNotifications: &expired}); err != nil {
chore.log.Error("error updating user's trial_notifications", zap.Error(err))
}
}
return nil
}
// Close closes chore.
func (chore *Chore) Close() error {
chore.Loop.Close()
return nil
}
func (chore *Chore) sendEmail(ctx context.Context, email string, msg mailservice.Message) (err error) {
defer mon.Task()(&ctx)(&err)
// blocking send allows us to verify that links are clicked in tests.
if chore.useBlockingSend {
err = chore.mailService.SendRendered(
ctx,
[]post.Address{{Address: email}},
msg,
)
if err != nil {
return err
}
} else {
chore.mailService.SendRenderedAsync(
ctx,
[]post.Address{{Address: email}},
msg,
)
}
return nil
}
// TestSetLinkAddress allows the email link address to be reconfigured.
// The address points to the satellite web server's external address.
// In the test environment the external address is not set by a config.
// It is an internal address, and we don't know what the port is until after it
// has been assigned. With this method, we get the address from the api in testplanet
// and assign it here.
func (chore *Chore) TestSetLinkAddress(address string) {
chore.address = address
}
// TestUseBlockingSend allows us to set the chore to use a blocking send method.
// Using a blocking send method allows us to test that links are clicked without
// potential race conditions.
func (chore *Chore) TestUseBlockingSend() {
chore.useBlockingSend = true
}