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