]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/emailer.ts
Add more filters to admin follows table
[github/Chocobozzz/PeerTube.git] / server / lib / emailer.ts
CommitLineData
ecb4e35f
C
1import { createTransport, Transporter } from 'nodemailer'
2import { isTestInstance } from '../helpers/core-utils'
05e67d62 3import { bunyanLogger, logger } from '../helpers/logger'
6dd9de95 4import { CONFIG } from '../initializers/config'
ecb4e35f
C
5import { JobQueue } from './job-queue'
6import { EmailPayload } from './job-queue/handlers/email'
c9d5c64f 7import { readFileSync } from 'fs-extra'
6dd9de95 8import { WEBSERVER } from '../initializers/constants'
8424c402
C
9import {
10 MCommentOwnerVideo,
11 MVideo,
12 MVideoAbuseVideo,
13 MVideoAccountLight,
14 MVideoBlacklistLightVideo,
15 MVideoBlacklistVideo
16} from '../typings/models/video'
17import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models'
453e83ea 18import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import'
ecb4e35f 19
dee77e76
C
20type SendEmailOptions = {
21 to: string[]
22 subject: string
23 text: string
24
25 fromDisplayName?: string
26 replyTo?: string
27}
28
ecb4e35f
C
29class Emailer {
30
31 private static instance: Emailer
32 private initialized = false
33 private transporter: Transporter
34
35 private constructor () {}
36
37 init () {
38 // Already initialized
39 if (this.initialized === true) return
40 this.initialized = true
41
d3e56c0c 42 if (Emailer.isEnabled()) {
ecb4e35f
C
43 logger.info('Using %s:%s as SMTP server.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT)
44
45 let tls
46 if (CONFIG.SMTP.CA_FILE) {
47 tls = {
48 ca: [ readFileSync(CONFIG.SMTP.CA_FILE) ]
49 }
50 }
51
f076daa7
C
52 let auth
53 if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) {
54 auth = {
55 user: CONFIG.SMTP.USERNAME,
56 pass: CONFIG.SMTP.PASSWORD
57 }
58 }
59
ecb4e35f
C
60 this.transporter = createTransport({
61 host: CONFIG.SMTP.HOSTNAME,
62 port: CONFIG.SMTP.PORT,
63 secure: CONFIG.SMTP.TLS,
05e67d62
C
64 debug: CONFIG.LOG.LEVEL === 'debug',
65 logger: bunyanLogger as any,
bebf2d89 66 ignoreTLS: CONFIG.SMTP.DISABLE_STARTTLS,
ecb4e35f 67 tls,
f076daa7 68 auth
ecb4e35f
C
69 })
70 } else {
71 if (!isTestInstance()) {
72 logger.error('Cannot use SMTP server because of lack of configuration. PeerTube will not be able to send mails!')
73 }
74 }
75 }
76
d3e56c0c
C
77 static isEnabled () {
78 return !!CONFIG.SMTP.HOSTNAME && !!CONFIG.SMTP.PORT
3b3b1820
C
79 }
80
ecb4e35f
C
81 async checkConnectionOrDie () {
82 if (!this.transporter) return
83
3d3441d6
C
84 logger.info('Testing SMTP server...')
85
ecb4e35f
C
86 try {
87 const success = await this.transporter.verify()
88 if (success !== true) this.dieOnConnectionFailure()
89
90 logger.info('Successfully connected to SMTP server.')
91 } catch (err) {
92 this.dieOnConnectionFailure(err)
93 }
94 }
95
453e83ea 96 addNewVideoFromSubscriberNotification (to: string[], video: MVideoAccountLight) {
cef534ed 97 const channelName = video.VideoChannel.getDisplayName()
6dd9de95 98 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
cef534ed 99
ecb4e35f 100 const text = `Hi dear user,\n\n` +
cef534ed
C
101 `Your subscription ${channelName} just published a new video: ${video.name}` +
102 `\n\n` +
103 `You can view it on ${videoUrl} ` +
104 `\n\n` +
ecb4e35f 105 `Cheers,\n` +
b5bfadf0 106 `${CONFIG.EMAIL.BODY.SIGNATURE}`
ecb4e35f
C
107
108 const emailPayload: EmailPayload = {
cef534ed 109 to,
916937d7 110 subject: CONFIG.EMAIL.SUBJECT.PREFIX + channelName + ' just published a new video',
ecb4e35f
C
111 text
112 }
113
114 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
115 }
116
8424c402 117 addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') {
f7cc67b4
C
118 const followerName = actorFollow.ActorFollower.Account.getDisplayName()
119 const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName()
120
121 const text = `Hi dear user,\n\n` +
122 `Your ${followType} ${followingName} has a new subscriber: ${followerName}` +
123 `\n\n` +
124 `Cheers,\n` +
b5bfadf0 125 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
126
127 const emailPayload: EmailPayload = {
128 to,
916937d7 129 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New follower on your channel ' + followingName,
f7cc67b4
C
130 text
131 }
132
133 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
134 }
135
453e83ea 136 addNewInstanceFollowerNotification (to: string[], actorFollow: MActorFollowActors) {
883993c8
C
137 const awaitingApproval = actorFollow.state === 'pending' ? ' awaiting manual approval.' : ''
138
139 const text = `Hi dear admin,\n\n` +
140 `Your instance has a new follower: ${actorFollow.ActorFollower.url}${awaitingApproval}` +
141 `\n\n` +
142 `Cheers,\n` +
b5bfadf0 143 `${CONFIG.EMAIL.BODY.SIGNATURE}`
883993c8
C
144
145 const emailPayload: EmailPayload = {
146 to,
916937d7 147 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New instance follower',
883993c8
C
148 text
149 }
150
151 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
152 }
153
8424c402
C
154 addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) {
155 const text = `Hi dear admin,\n\n` +
156 `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` +
157 `\n\n` +
158 `Cheers,\n` +
159 `${CONFIG.EMAIL.BODY.SIGNATURE}`
160
161 const emailPayload: EmailPayload = {
162 to,
163 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following',
164 text
165 }
166
167 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
168 }
169
453e83ea 170 myVideoPublishedNotification (to: string[], video: MVideo) {
6dd9de95 171 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
dc133480
C
172
173 const text = `Hi dear user,\n\n` +
174 `Your video ${video.name} has been published.` +
175 `\n\n` +
176 `You can view it on ${videoUrl} ` +
177 `\n\n` +
178 `Cheers,\n` +
b5bfadf0 179 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
180
181 const emailPayload: EmailPayload = {
182 to,
916937d7 183 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video ${video.name} is published`,
dc133480
C
184 text
185 }
186
187 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
188 }
189
453e83ea 190 myVideoImportSuccessNotification (to: string[], videoImport: MVideoImportVideo) {
6dd9de95 191 const videoUrl = WEBSERVER.URL + videoImport.Video.getWatchStaticPath()
dc133480
C
192
193 const text = `Hi dear user,\n\n` +
194 `Your video import ${videoImport.getTargetIdentifier()} is finished.` +
195 `\n\n` +
196 `You can view the imported video on ${videoUrl} ` +
197 `\n\n` +
198 `Cheers,\n` +
b5bfadf0 199 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
200
201 const emailPayload: EmailPayload = {
202 to,
916937d7 203 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} is finished`,
dc133480
C
204 text
205 }
206
207 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
208 }
209
453e83ea 210 myVideoImportErrorNotification (to: string[], videoImport: MVideoImport) {
6dd9de95 211 const importUrl = WEBSERVER.URL + '/my-account/video-imports'
dc133480
C
212
213 const text = `Hi dear user,\n\n` +
214 `Your video import ${videoImport.getTargetIdentifier()} encountered an error.` +
215 `\n\n` +
216 `See your videos import dashboard for more information: ${importUrl}` +
217 `\n\n` +
218 `Cheers,\n` +
b5bfadf0 219 `${CONFIG.EMAIL.BODY.SIGNATURE}`
dc133480
C
220
221 const emailPayload: EmailPayload = {
222 to,
916937d7 223 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Your video import ${videoImport.getTargetIdentifier()} encountered an error`,
dc133480
C
224 text
225 }
226
227 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
228 }
229
453e83ea 230 addNewCommentOnMyVideoNotification (to: string[], comment: MCommentOwnerVideo) {
cef534ed
C
231 const accountName = comment.Account.getDisplayName()
232 const video = comment.Video
6dd9de95 233 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
cef534ed
C
234
235 const text = `Hi dear user,\n\n` +
236 `A new comment has been posted by ${accountName} on your video ${video.name}` +
237 `\n\n` +
238 `You can view it on ${commentUrl} ` +
239 `\n\n` +
d9eaee39 240 `Cheers,\n` +
b5bfadf0 241 `${CONFIG.EMAIL.BODY.SIGNATURE}`
d9eaee39
JM
242
243 const emailPayload: EmailPayload = {
cef534ed 244 to,
916937d7 245 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New comment on your video ' + video.name,
d9eaee39
JM
246 text
247 }
248
249 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
250 }
251
453e83ea 252 addNewCommentMentionNotification (to: string[], comment: MCommentOwnerVideo) {
f7cc67b4
C
253 const accountName = comment.Account.getDisplayName()
254 const video = comment.Video
6dd9de95 255 const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
f7cc67b4
C
256
257 const text = `Hi dear user,\n\n` +
258 `${accountName} mentioned you on video ${video.name}` +
259 `\n\n` +
260 `You can view the comment on ${commentUrl} ` +
261 `\n\n` +
262 `Cheers,\n` +
b5bfadf0 263 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
264
265 const emailPayload: EmailPayload = {
266 to,
916937d7 267 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Mention on video ' + video.name,
f7cc67b4
C
268 text
269 }
270
271 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
272 }
273
453e83ea 274 addVideoAbuseModeratorsNotification (to: string[], videoAbuse: MVideoAbuseVideo) {
6dd9de95 275 const videoUrl = WEBSERVER.URL + videoAbuse.Video.getWatchStaticPath()
ba75d268
C
276
277 const text = `Hi,\n\n` +
6dd9de95 278 `${WEBSERVER.HOST} received an abuse for the following video ${videoUrl}\n\n` +
ba75d268 279 `Cheers,\n` +
b5bfadf0 280 `${CONFIG.EMAIL.BODY.SIGNATURE}`
ba75d268 281
ba75d268
C
282 const emailPayload: EmailPayload = {
283 to,
916937d7 284 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Received a video abuse',
ba75d268
C
285 text
286 }
287
288 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
289 }
290
8424c402 291 addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) {
6dd9de95 292 const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list'
8424c402 293 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
7ccddd7b
JM
294
295 const text = `Hi,\n\n` +
296 `A recently added video was auto-blacklisted and requires moderator review before publishing.` +
297 `\n\n` +
298 `You can view it and take appropriate action on ${videoUrl}` +
299 `\n\n` +
300 `A full list of auto-blacklisted videos can be reviewed here: ${VIDEO_AUTO_BLACKLIST_URL}` +
301 `\n\n` +
302 `Cheers,\n` +
b5bfadf0 303 `${CONFIG.EMAIL.BODY.SIGNATURE}`
7ccddd7b
JM
304
305 const emailPayload: EmailPayload = {
306 to,
916937d7 307 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'An auto-blacklisted video is awaiting review',
7ccddd7b
JM
308 text
309 }
310
311 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
312 }
313
453e83ea 314 addNewUserRegistrationNotification (to: string[], user: MUser) {
f7cc67b4 315 const text = `Hi,\n\n` +
6dd9de95 316 `User ${user.username} just registered on ${WEBSERVER.HOST} PeerTube instance.\n\n` +
f7cc67b4 317 `Cheers,\n` +
b5bfadf0 318 `${CONFIG.EMAIL.BODY.SIGNATURE}`
f7cc67b4
C
319
320 const emailPayload: EmailPayload = {
321 to,
916937d7 322 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'New user registration on ' + WEBSERVER.HOST,
f7cc67b4
C
323 text
324 }
325
326 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
327 }
328
453e83ea 329 addVideoBlacklistNotification (to: string[], videoBlacklist: MVideoBlacklistVideo) {
cef534ed 330 const videoName = videoBlacklist.Video.name
6dd9de95 331 const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath()
26b7305a 332
cef534ed 333 const reasonString = videoBlacklist.reason ? ` for the following reason: ${videoBlacklist.reason}` : ''
6dd9de95 334 const blockedString = `Your video ${videoName} (${videoUrl} on ${WEBSERVER.HOST} has been blacklisted${reasonString}.`
26b7305a
C
335
336 const text = 'Hi,\n\n' +
337 blockedString +
338 '\n\n' +
339 'Cheers,\n' +
b5bfadf0 340 `${CONFIG.EMAIL.BODY.SIGNATURE}`
26b7305a 341
26b7305a 342 const emailPayload: EmailPayload = {
cef534ed 343 to,
916937d7 344 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${videoName} blacklisted`,
26b7305a
C
345 text
346 }
347
348 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
349 }
350
453e83ea 351 addVideoUnblacklistNotification (to: string[], video: MVideo) {
6dd9de95 352 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
26b7305a
C
353
354 const text = 'Hi,\n\n' +
6dd9de95 355 `Your video ${video.name} (${videoUrl}) on ${WEBSERVER.HOST} has been unblacklisted.` +
26b7305a
C
356 '\n\n' +
357 'Cheers,\n' +
b5bfadf0 358 `${CONFIG.EMAIL.BODY.SIGNATURE}`
26b7305a 359
26b7305a 360 const emailPayload: EmailPayload = {
cef534ed 361 to,
916937d7 362 subject: CONFIG.EMAIL.SUBJECT.PREFIX + `Video ${video.name} unblacklisted`,
26b7305a
C
363 text
364 }
365
366 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
367 }
368
b426edd4 369 addPasswordResetEmailJob (to: string, resetPasswordUrl: string) {
cef534ed 370 const text = `Hi dear user,\n\n` +
6dd9de95 371 `A reset password procedure for your account ${to} has been requested on ${WEBSERVER.HOST} ` +
cef534ed
C
372 `Please follow this link to reset it: ${resetPasswordUrl}\n\n` +
373 `If you are not the person who initiated this request, please ignore this email.\n\n` +
374 `Cheers,\n` +
b5bfadf0 375 `${CONFIG.EMAIL.BODY.SIGNATURE}`
cef534ed
C
376
377 const emailPayload: EmailPayload = {
378 to: [ to ],
916937d7 379 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Reset your password',
cef534ed
C
380 text
381 }
382
383 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
384 }
385
386 addVerifyEmailJob (to: string, verifyEmailUrl: string) {
387 const text = `Welcome to PeerTube,\n\n` +
6dd9de95 388 `To start using PeerTube on ${WEBSERVER.HOST} you must verify your email! ` +
cef534ed
C
389 `Please follow this link to verify this email belongs to you: ${verifyEmailUrl}\n\n` +
390 `If you are not the person who initiated this request, please ignore this email.\n\n` +
391 `Cheers,\n` +
b5bfadf0 392 `${CONFIG.EMAIL.BODY.SIGNATURE}`
cef534ed
C
393
394 const emailPayload: EmailPayload = {
395 to: [ to ],
916937d7 396 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Verify your email',
cef534ed
C
397 text
398 }
399
400 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
401 }
402
453e83ea 403 addUserBlockJob (user: MUser, blocked: boolean, reason?: string) {
eacb25c4
C
404 const reasonString = reason ? ` for the following reason: ${reason}` : ''
405 const blockedWord = blocked ? 'blocked' : 'unblocked'
6dd9de95 406 const blockedString = `Your account ${user.username} on ${WEBSERVER.HOST} has been ${blockedWord}${reasonString}.`
eacb25c4
C
407
408 const text = 'Hi,\n\n' +
409 blockedString +
410 '\n\n' +
411 'Cheers,\n' +
b5bfadf0 412 `${CONFIG.EMAIL.BODY.SIGNATURE}`
eacb25c4
C
413
414 const to = user.email
415 const emailPayload: EmailPayload = {
416 to: [ to ],
916937d7 417 subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Account ' + blockedWord,
eacb25c4
C
418 text
419 }
420
421 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
422 }
423
4e9fa5b7 424 addContactFormJob (fromEmail: string, fromName: string, subject: string, body: string) {
a4101923
C
425 const text = 'Hello dear admin,\n\n' +
426 fromName + ' sent you a message' +
427 '\n\n---------------------------------------\n\n' +
428 body +
429 '\n\n---------------------------------------\n\n' +
430 'Cheers,\n' +
431 'PeerTube.'
432
433 const emailPayload: EmailPayload = {
4759fedc
C
434 fromDisplayName: fromEmail,
435 replyTo: fromEmail,
a4101923 436 to: [ CONFIG.ADMIN.EMAIL ],
916937d7 437 subject: CONFIG.EMAIL.SUBJECT.PREFIX + subject,
a4101923
C
438 text
439 }
440
441 return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload })
442 }
443
4759fedc 444 sendMail (options: EmailPayload) {
d3e56c0c 445 if (!Emailer.isEnabled()) {
ecb4e35f
C
446 throw new Error('Cannot send mail because SMTP is not configured.')
447 }
448
4759fedc
C
449 const fromDisplayName = options.fromDisplayName
450 ? options.fromDisplayName
6dd9de95 451 : WEBSERVER.HOST
4759fedc 452
ecb4e35f 453 return this.transporter.sendMail({
4759fedc
C
454 from: `"${fromDisplayName}" <${CONFIG.SMTP.FROM_ADDRESS}>`,
455 replyTo: options.replyTo,
456 to: options.to.join(','),
457 subject: options.subject,
458 text: options.text
ecb4e35f
C
459 })
460 }
461
462 private dieOnConnectionFailure (err?: Error) {
d5b7d911 463 logger.error('Failed to connect to SMTP %s:%d.', CONFIG.SMTP.HOSTNAME, CONFIG.SMTP.PORT, { err })
ecb4e35f
C
464 process.exit(-1)
465 }
466
467 static get Instance () {
468 return this.instance || (this.instance = new this())
469 }
470}
471
472// ---------------------------------------------------------------------------
473
474export {
dee77e76
C
475 Emailer,
476 SendEmailOptions
ecb4e35f 477}