-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
286 lines (248 loc) · 10.5 KB
/
tasks.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
package tasks
import (
"fmt"
"html/template"
"strings"
"time"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/email"
"github.com/getfider/fider/app/pkg/markdown"
"github.com/getfider/fider/app/pkg/worker"
)
func describe(name string, job worker.Job) worker.Task {
return worker.Task{Name: name, Job: job}
}
func link(baseURL, path string, args ...interface{}) template.HTML {
return template.HTML(fmt.Sprintf("<a href='%[1]s%[2]s'>%[1]s%[2]s</a>", baseURL, fmt.Sprintf(path, args...)))
}
func linkWithText(text, baseURL, path string, args ...interface{}) template.HTML {
return template.HTML(fmt.Sprintf("<a href='%s%s'>%s</a>", baseURL, fmt.Sprintf(path, args...), text))
}
//SendSignUpEmail is used to send the sign up email to requestor
func SendSignUpEmail(model *models.CreateTenant, baseURL string) worker.Task {
return describe("Send sign up email", func(c *worker.Context) error {
to := email.NewRecipient(model.Name, model.Email, email.Params{
"logo": "https://getfider.com/images/logo-100x100.png",
"link": link(baseURL, "/signup/verify?k=%s", model.VerificationKey),
})
return c.Services().Emailer.Send(c, "signup_email", email.Params{}, "Fider", to)
})
}
//SendSignInEmail is used to send the sign in email to requestor
func SendSignInEmail(model *models.SignInByEmail) worker.Task {
return describe("Send sign in email", func(c *worker.Context) error {
to := email.NewRecipient("", model.Email, email.Params{
"tenantName": c.Tenant().Name,
"link": link(c.BaseURL(), "/signin/verify?k=%s", model.VerificationKey),
})
return c.Services().Emailer.Send(c, "signin_email", email.Params{}, c.Tenant().Name, to)
})
}
//SendChangeEmailConfirmation is used to send the change email confirmation email to requestor
func SendChangeEmailConfirmation(model *models.ChangeUserEmail) worker.Task {
return describe("Send change email confirmation", func(c *worker.Context) error {
previous := c.User().Email
if previous == "" {
previous = "(empty)"
}
to := email.NewRecipient(model.Requestor.Name, model.Email, email.Params{
"name": c.User().Name,
"oldEmail": previous,
"newEmail": model.Email,
"link": link(c.BaseURL(), "/change-email/verify?k=%s", model.VerificationKey),
})
return c.Services().Emailer.Send(c, "change_emailaddress_email", email.Params{}, c.Tenant().Name, to)
})
}
//NotifyAboutNewPost sends a notification (web and email) to subscribers
func NotifyAboutNewPost(post *models.Post) worker.Task {
return describe("Notify about new post", func(c *worker.Context) error {
// Web notification
users, err := c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelWeb, models.NotificationEventNewPost)
if err != nil {
return c.Failure(err)
}
title := fmt.Sprintf("New post: **%s**", post.Title)
link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
for _, user := range users {
if user.ID != c.User().ID {
if _, err = c.Services().Notifications.Insert(user, title, link, post.ID); err != nil {
return c.Failure(err)
}
}
}
// Email notification
users, err = c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelEmail, models.NotificationEventNewPost)
if err != nil {
return c.Failure(err)
}
to := make([]email.Recipient, 0)
for _, user := range users {
if user.ID != c.User().ID {
to = append(to, email.NewRecipient(user.Name, user.Email, email.Params{}))
}
}
params := email.Params{
"title": post.Title,
"tenantName": c.Tenant().Name,
"userName": c.User().Name,
"content": markdown.Simple(post.Description),
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
}
return c.Services().Emailer.BatchSend(c, "new_post", params, c.User().Name, to)
})
}
//NotifyAboutNewComment sends a notification (web and email) to subscribers
func NotifyAboutNewComment(post *models.Post, comment *models.NewComment) worker.Task {
return describe("Notify about new comment", func(c *worker.Context) error {
// Web notification
users, err := c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelWeb, models.NotificationEventNewComment)
if err != nil {
return c.Failure(err)
}
title := fmt.Sprintf("**%s** left a comment on **%s**", c.User().Name, post.Title)
link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
for _, user := range users {
if user.ID != c.User().ID {
if _, err = c.Services().Notifications.Insert(user, title, link, post.ID); err != nil {
return c.Failure(err)
}
}
}
// Email notification
users, err = c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelEmail, models.NotificationEventNewComment)
if err != nil {
return c.Failure(err)
}
to := make([]email.Recipient, 0)
for _, user := range users {
if user.ID != c.User().ID {
to = append(to, email.NewRecipient(user.Name, user.Email, email.Params{}))
}
}
params := email.Params{
"title": post.Title,
"tenantName": c.Tenant().Name,
"userName": c.User().Name,
"content": markdown.Simple(comment.Content),
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"unsubscribe": linkWithText("unsubscribe from it", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
}
return c.Services().Emailer.BatchSend(c, "new_comment", params, c.User().Name, to)
})
}
//NotifyAboutStatusChange sends a notification (web and email) to subscribers
func NotifyAboutStatusChange(post *models.Post, prevStatus models.PostStatus) worker.Task {
return describe("Notify about post status change", func(c *worker.Context) error {
//Don't notify if previous status is the same
if prevStatus == post.Status {
return nil
}
// Web notification
users, err := c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelWeb, models.NotificationEventChangeStatus)
if err != nil {
return c.Failure(err)
}
title := fmt.Sprintf("**%s** changed status of **%s** to **%s**", c.User().Name, post.Title, post.Status.Name())
link := fmt.Sprintf("/posts/%d/%s", post.Number, post.Slug)
for _, user := range users {
if user.ID != c.User().ID {
if _, err = c.Services().Notifications.Insert(user, title, link, post.ID); err != nil {
return c.Failure(err)
}
}
}
// Email notification
users, err = c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelEmail, models.NotificationEventChangeStatus)
if err != nil {
return c.Failure(err)
}
var duplicate template.HTML
if post.Status == models.PostDuplicate {
originalPost, err := c.Services().Posts.GetByNumber(post.Response.Original.Number)
if err != nil {
return c.Failure(err)
}
duplicate = linkWithText(originalPost.Title, c.BaseURL(), "/posts/%d/%s", originalPost.Number, originalPost.Slug)
}
to := make([]email.Recipient, 0)
for _, user := range users {
if user.ID != c.User().ID {
to = append(to, email.NewRecipient(user.Name, user.Email, email.Params{}))
}
}
params := email.Params{
"title": post.Title,
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"tenantName": c.Tenant().Name,
"content": markdown.Simple(post.Response.Text),
"status": post.Status.Name(),
"duplicate": duplicate,
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"unsubscribe": linkWithText("unsubscribe from it", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
}
return c.Services().Emailer.BatchSend(c, "change_status", params, c.User().Name, to)
})
}
//NotifyAboutDeletedPost sends a notification (web and email) to subscribers of the post that has been deleted
func NotifyAboutDeletedPost(post *models.Post) worker.Task {
return describe("Notify about deleted post", func(c *worker.Context) error {
// Web notification
users, err := c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelWeb, models.NotificationEventChangeStatus)
if err != nil {
return c.Failure(err)
}
title := fmt.Sprintf("**%s** deleted **%s**", c.User().Name, post.Title)
for _, user := range users {
if user.ID != c.User().ID {
if _, err = c.Services().Notifications.Insert(user, title, "", post.ID); err != nil {
return c.Failure(err)
}
}
}
// Email notification
users, err = c.Services().Posts.GetActiveSubscribers(post.Number, models.NotificationChannelEmail, models.NotificationEventChangeStatus)
if err != nil {
return c.Failure(err)
}
to := make([]email.Recipient, 0)
for _, user := range users {
if user.ID != c.User().ID {
to = append(to, email.NewRecipient(user.Name, user.Email, email.Params{}))
}
}
params := email.Params{
"title": post.Title,
"tenantName": c.Tenant().Name,
"content": markdown.Simple(post.Response.Text),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
}
return c.Services().Emailer.BatchSend(c, "delete_post", params, c.User().Name, to)
})
}
//SendInvites sends one email to each invited recipient
func SendInvites(subject, message string, invitations []*models.UserInvitation) worker.Task {
return describe("Send invites", func(c *worker.Context) error {
to := make([]email.Recipient, len(invitations))
for i, invite := range invitations {
err := c.Services().Tenants.SaveVerificationKey(invite.VerificationKey, 15*24*time.Hour, invite)
if err != nil {
return c.Failure(err)
}
url := fmt.Sprintf("%s/invite/verify?k=%s", c.BaseURL(), invite.VerificationKey)
toMessage := strings.Replace(message, app.InvitePlaceholder, string(url), -1)
to[i] = email.NewRecipient("", invite.Email, email.Params{
"message": markdown.Full(toMessage),
})
}
return c.Services().Emailer.BatchSend(c, "invite_email", email.Params{
"subject": subject,
}, c.User().Name, to)
})
}